登月游戏。控件不起作用



我必须在处理作业时重新创建登月游戏。控件根本不起作用,我在代码中找不到错误。另外,我是处理和Java的初学者。

int posx, posy;
//initial velocity
float vx,vy;
// gravity
float gravity = 0.05; 
boolean moveLeft, moveRight, moveUp;
void setup() {
size(500, 500);
background(0);
//inital position
posx =int(random(width)); //position of the left vertex of our ship
posy =20; // position of the bottom vertex of our ship
moveLeft = moveRight = moveUp= false;
//initial velocity
vx=vy=1;
}
void draw() {
noStroke();
background(0);
fill(255,255,255);
rect(0,470,width,30);
fill(255,0,0);
rect(200,470,100,30);
moveKeys();
moveShip();
drawShip();
}
void drawShip() {
fill(169,169,169);
rect(posx,posy,50,25);
fill(255,255,255);
rect(posx+20,posy-10,10,10);
fill(105,105,105);
rect(posx+20,posy+25,10,5);
stroke(255,255,255);
strokeWeight(2);
line(posx+2,posy+25,posx+2, posy+40);
stroke(255,255,255);
strokeWeight(2);
line(posx+48,posy+25,posx+48, posy+40);
}
void moveShip() {
// Detecting collisions
if (posx + 25 > width - 1 ||
posx - 25 < 0)
vx *= -1;
if ( posy - 12.5 < 0)
vy *= -1;
if ( posy + 50 > height-1) 
vx=vy=0;
//update position      
posx += vx;
posy += vy;
}
void update() {
posx += vx;
posy += vy;
}
void keyPressed() {
if (key==CODED) {
switch (keyCode) {
case UP:
moveUp = true; break;
case LEFT:
moveLeft = true; break;
case RIGHT:
moveRight = true;  break;    
}
}  
}
void moveKeys() {
if (moveLeft) translate(posx-= vx, 0);     
if (moveRight) translate(posx+= vx, 0);
if (moveUp) { thrust(); }
}
void thrust() {
if (vy  < 1) vy += 0.1;
}

预计宇宙飞船降落在着陆区(红色),这就是游戏应该重新启动的时候。但是,到目前为止,我只使用了重力功能,无法移动宇宙飞船。

我在你的代码中看到了一些问题。

首先,处理无法识别持有密钥的用户,程序只是重复调用 keyPressed()。使用布尔值来存储有关箭头键的信息是个好主意。你缺少的是方法keyRelease(),它会把布尔值放回false。

其次,如果你想让你的游戏看起来逼真,你需要把实时时间放在某个地方。处理有非常有用的方法millis(),它从程序开始返回以毫秒为单位的时间。因此,每当您更新速度或位置时,都需要将其所需的变化乘以时间步长。

我在您的代码中看到的其他一些不好的东西是例如 posx 和 posy 整数。它们必须是浮点数才能正常工作。船的绘制并不是很精确 - 实际上我不认为 posx 和 posy 是船的左顶点和底部顶点。看看加工现场的绘图方法,看看他们在做什么。

这是重新制作的代码:

// ship position
float posx, posy;
// ship velocity
float vx,vy;
// gravity
float gravity;
// user input
boolean moveLeft, moveRight, moveUp;
// time
float timelast = 0, timenow, timeelapsed;
void setup() {
size(500, 500);
background(0);
//inital position
posx = 225;//int(random(width - 50)); //position of the left vertex of our ship
posy = 200; // position of the bottom vertex of our ship
// initial user input
moveLeft = moveRight = moveUp= false;
// initial velocity
vx = vy = 10;
// gravity
gravity = 10;
timelast = millis();
}
void draw() {
noStroke();
background(0);
fill(255,255,255);
rect(0,470,width,30);
fill(255,0,0);
rect(200,470,100,30);
updateTime();
userInput();
moveShip();
drawShip();
}
void updateTime() {
timelast = timenow;
timenow = millis();
timeelapsed = timenow - timelast;
}
void drawShip() {
fill(169,169,169);
rect(posx,posy,50,25);
fill(255,255,255);
rect(posx+20,posy-10,10,10);
fill(105,105,105);
rect(posx+20,posy+25,10,5);
stroke(255,255,255);
strokeWeight(2);
line(posx+2,posy+25,posx+2, posy+40);
stroke(255,255,255);
strokeWeight(2);
line(posx+48,posy+25,posx+48, posy+40);
}
void userInput() {
if (moveLeft)
vx -= 100 * timeelapsed / 1000;
if (moveRight)
vx += 100 * timeelapsed / 1000;
if (moveUp)
vy -= 100 * timeelapsed / 1000;
}
void moveShip() {
vy += gravity * timeelapsed / 1000;
posx += vx * timeelapsed / 1000;
posy += vy * timeelapsed / 1000;
// Detecting collisions
if (posx + 50 >= width || posx <= 0)
vx *= -1;
if (posy - 25 <= 0)
vy *= -1;
if (posy + 50 >= height) 
vx=vy=0;
}
void keyPressed() {
if (key==CODED) {
switch (keyCode) {
case UP:
moveUp = true;
break;
case LEFT:
moveLeft = true;
break;
case RIGHT:
moveRight = true;
break;    
}
}  
}
void keyReleased() {
if (key==CODED) {
switch (keyCode) {
case UP:
moveUp = false;
break;
case LEFT:
moveLeft = false;
break;
case RIGHT:
moveRight = false;
break;    
}
}  
}

最新更新