登月游戏。控件不工作
Lunar Landing game. Controls aren't working
我必须重新创建登月游戏以处理任务。控件根本不工作,我找不到我的代码中的错误。另外,我是处理的初学者 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;
}
预计宇宙飞船将降落在着陆区(红色),届时游戏应该重新开始。但是,到目前为止我只使用了重力功能,无法移动飞船。
我发现您的代码中存在一些问题。
首先,Processing 无法识别用户按住的键,程序只是重复调用 keyPressed()。使用布尔值来存储有关箭头键的信息是个好主意。您缺少的是方法 keyReleased(),它会将布尔值重新设置为 false。
其次,如果你想让你的游戏看起来逼真,你需要把实时时间放在那里。 Processing 有非常有用的方法 millis(),即从程序开始后的 returns 时间(以毫秒为单位)。因此,无论何时更新速度或位置,都需要将所需的变化乘以时间步长。
我在您的代码中看到的其他一些不好的地方是例如 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;
}
}
}
我必须重新创建登月游戏以处理任务。控件根本不工作,我找不到我的代码中的错误。另外,我是处理的初学者 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;
}
预计宇宙飞船将降落在着陆区(红色),届时游戏应该重新开始。但是,到目前为止我只使用了重力功能,无法移动飞船。
我发现您的代码中存在一些问题。
首先,Processing 无法识别用户按住的键,程序只是重复调用 keyPressed()。使用布尔值来存储有关箭头键的信息是个好主意。您缺少的是方法 keyReleased(),它会将布尔值重新设置为 false。
其次,如果你想让你的游戏看起来逼真,你需要把实时时间放在那里。 Processing 有非常有用的方法 millis(),即从程序开始后的 returns 时间(以毫秒为单位)。因此,无论何时更新速度或位置,都需要将所需的变化乘以时间步长。
我在您的代码中看到的其他一些不好的地方是例如 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;
}
}
}