在处理任务时,我必须重新创建登月游戏。控件根本不起作用,我无法在代码中找到错误。另外,我还是处理和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;
}
预计太空飞船将降落在着陆区(红色),此时游戏应重新开始。但是,到目前为止,我只使用了重力功能,无法四处移动飞船。
答案 0 :(得分:2)
我在您的代码中看到了一些问题。
首先,Processing无法识别用户在按住键,程序只是重复调用keyPressed()。使用布尔值存储有关箭头键的信息是一个好主意。您缺少的是方法keyReleased(),该方法会将布尔值重新设置为false。
其次,如果您想使游戏看起来逼真,则需要实时放置在某个地方。处理具有非常有用的方法millis(),该方法从程序开始返回的时间以毫秒为单位。因此,无论何时更新速度或位置,都需要将所需的更改乘以时间步长。
我在您的代码中看到的其他一些不好的东西是例如posx和posy整数。它们必须是浮点数才能正常工作。绘制船舶并不是很精确-实际上,我不认为posx和posy是预期的船舶左下角。看看processing site处的绘制方法,看看它们在做什么。
这是重制的代码:
// 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;
}
}
}