我最近开始开发一个小的JavaScript游戏,只是为了好玩.我的想法是你在屏幕上的一个方框内用箭头键(或者awsd或者我不关心什么)控制了一个小点.然后,小矩形会随机产生在盒子的所有边缘上并在其上方前进.你必须避免与他们接触.事实证明这个项目比我预期的要困难,我无法让这项运动正常运转.如果你可以帮助我那将是伟大的.此外,随意采取这个概念,到目前为止我做了什么,并做任何你想做的事情.我很想看到你的结果.下面是我用于没有任何移动脚本的产生的代码.我正在使用此代码的基本思想来进行运动:
var x = 5; //Starting Location - left
var y = 5; //Starting Location - top
var dest_x = 300; //Ending Location - left
var dest_y = 300; //Ending Location - top
var interval = 2; //Move 2px every initialization
function moveImage() {
//Keep on moving the image till the target is achieved
if(x<dest_x) x = x + interval;
if(y<dest_y) y = y + interval;
//Move the image to the new location
document.getElementById("ufo").style.top = y+'px';
document.getElementById("ufo").style.left = x+'px';
if …Run Code Online (Sandbox Code Playgroud)