Java Eclipse游戏 - 重新启动游戏



我已经用Java语言在Eclipse霓虹灯中制作了一个游戏,但是我想知道如何制作游戏,以便如果用户按r,他或她可以返回第一个屏幕,上面显示按空格键开始。 如果您需要它来找出解决方案,这是我的 3 个代码文件。

//This is the game file (main)
import processing.core.PApplet;
public class Game extends PApplet{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PApplet.main("Game");
    }
    James james1;
    Wall wall1;
    Wall wall2;
    Wall wall3;
    public void settings() {
        size(500, 700);
    }
    public void setup() {
        frameRate(40);
        james1 = new James(this, 250, 650, 50);
        wall1 = new Wall(this, 0);
        wall2 = new Wall(this, 250);
        wall3 = new Wall(this, -250);
    }
    public void draw() {
        if(key == ' ') {
        clear();
        background(0, 255, 255);
        boolean loser = wall1.col(james1.getX(), james1.getY()) || wall2.col(james1.getX(), james1.getY()) ||wall3.col(james1.getX(), james1.getY());
        james1.show();
        move();
        if (!loser){
            wall1.update();
            wall2.update();
            wall3.update();
        }
        else {
            reset();
        }
        wall1.display();
        wall2.display();
        wall3.display();
        }
        else {
            background(255, 0, 255);
            textSize(45);
            text("Press Space to Start!", 50, 350);
        }
    }
    public void reset() {
        clear();
        frameRate(0);
        textSize(40);
        text("You Lose, R to restart", 25, 550);
    }
    public void restart() {
    }
    public void move() {
        james1.setX(mouseX);
        if (mouseX <= 25) {
            james1.setX(25);
        }
        else if (mouseX >= 475) {
            james1.setX(475);
        }

    }

}

//This is the character file (James)
import processing.core.PApplet;
public class James {
    PApplet p;
    private float x;
    private float y;
    private float size;

    public James(PApplet np, float nx, float ny, float nsize) {
        p = np;
        x = nx;
        y = ny;
        size = nsize;
    }
    public void show() {
        p.fill(255,20,147);
        p.ellipse(x, y, size, size);
        p.fill(128,128,0);
        p.ellipse(x - 10, y - 10, size - 30, size - 30);
        p.ellipse(x + 10, y - 10, size -30, size - 30);
    }

    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }
    public float getSize() {
        return size;
    }

    public void setX(float gx) {
        x = gx;
    }
    public void setY(float gy) {
        y = gy;
    }
    public void setSize(float gSize) {
        size = gSize;
    }

}

//This is obstacle class file (walls)
import processing.core.PApplet;
public class Wall {
    PApplet p;
    private float x;
    private float y;
    private float w;
    private float xw;
    private float yw;
    private float h;
    private float wh;
    private float xspeed;
    private float yspeed;
    public Wall (PApplet np, float ny) {
        p = np;
        x = 0;
        y = ny;
        wh = 0;
        xw = p.random(0, 450);
        yw = p.random(0, 450);
        w = p.random(0, 450);
        h = 30;
        yspeed = 10;
    }
    public void display() {
        p.rect(0, y, w, h);
        p.rect(w + 100, y, 500-(w+100), h);

    }
    public void update() {
        if(y > p.height) {
            y = 0;
            w = p.random(0,450);
            xw = p.random(0, 450);
            yw = p.random(0, 450);
        }
        y = yspeed + y;
    }   
    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }
    public float getW() {
        return w;
    }
    public float getXW() {
        return xw;
    }
    public float getYW() {
        return yw;
    }
    public float getWH() {
        return wh;
    }
    public float getH() {
        return h;
    }
    public float getxSpeed() {
        return xspeed;
    }
    public float getySpeed() {
        return yspeed;
    }
    public void setX(float gx) {
        x = gx;
    }
    public void setYW(float gyw) {
        yw = gyw;
    }
    public void setW(float gw) {
        w = gw;
    }
    public void setY(float gy) {
        y = gy;
    }
    public void setH(float gh) {
        h = gh;
    }
    public void setxSpeed(float gxSpeed) {
        xspeed = gxSpeed;
    }
    public void setySpeed(float gySpeed) {
        yspeed = gySpeed;
    }
    public boolean col(float ballX, float ballY) {
        if (y + h >= ballY - 25 && y <= ballY + 25 ) {
            if(ballX - 25 <= w || ballX+25 >= w+100) {
                return true;
            }
        }
        return false;
    }

}

希望对于一个进球来说不会太长;)

将方法设置中的所有内容复制到方法重新启动,然后从方法设置调用重新启动。

你会得到这样的东西:

public void setup()
{
    restart();
}
public void restart()
{
    frameRate(40);
    james1 = new James(this, 250, 650, 50);
    wall1 = new Wall(this, 0);
    wall2 = new Wall(this, 250);
    wall3 = new Wall(this, -250);
}

现在,只要您希望游戏重新启动,只需调用 restart 即可。在您的示例中:当用户按下"r"按钮时。

在游戏中,拥有一个处理此类事情的 Map 类确实是个好主意。

您基本上可以复制安装程序并使用新方法名称粘贴它,甚至可以使用相同的方法来使其在按"r"时,它只是再次调用该方法。但只是为了让它更干净、更具可读性。你可以这样做。

public void setup() {
    if(key.r) { //of course this key.r will have to be created as a KeyListener.
      restart();
    }
}
public void restart() {
    frameRate(40);
    james1 = new James(this, 250, 650, 50);
    wall1 = new Wall(this, 0);
    wall2 = new Wall(this, 250);
    wall3 = new Wall(this, -250);
}

您可以为 KeyInput 创建一个新类,也可以在同一个类中执行此操作,但它应该有点像这样:

public boolean[] keys = new boolean[120];
public boolean r;
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    
    f3 = (key == KeyEvent.VK_F3);
}
public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
    
}
public void keyTyped(KeyEvent e) {      
    keys[e.getKeyCode()] = false;
}
}

有好的一天!:D

最新更新