Lunar Landing游戏。控件不起作用

问题描述 投票:1回答:1

我必须在处理任务时重新创建登月游戏。控件根本不起作用,我在代码中找不到错误。另外,我是处理和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;
}

预计宇宙飞船降落在着陆区(红色),那时游戏应该重新启动。然而,到目前为止,我只使用了引力特征,无法移动宇宙飞船。

java awt processing keylistener
1个回答
2
投票

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

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

其次,如果你想让你的游戏看起来更逼真,你需要把它放在某个地方。 Processing具有非常有用的方法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;    
        }
    }  
}
© www.soinside.com 2019 - 2024. All rights reserved.