gpt4 book ai didi

java - 登月游戏。控件不起作用

转载 作者:行者123 更新时间:2023-11-30 01:52:59 24 4
gpt4 key购买 nike

我必须在处理任务时重新创建登月游戏。控件根本不起作用,我在代码中找不到错误。另外,我是处理和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()。使用 boolean 值来存储有关箭头键的信息是个好主意。您缺少的是 keyReleased() 方法,该方法会将 boolean 值重新设置为 false。

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

我在您的代码中看到的其他一些不好的东西是例如 posx 和 posy 整数。它们需要是 float 才能正常工作。船的绘图并不是很精确 - 实际上我不认为 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 - 登月游戏。控件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55426950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com