如何在加工中制作游戏首页?

How to make a front page of a game in processing?

我正在尝试为作业编写游戏。我已经完成了它的胆量,但我需要在开始时说一些规则等。

为了做到这一点,我使用了 'if' 语句,所以它会检查何时按下 space 栏,从而开始游戏。但是,当按下另一个键时,它只会返回到起始页面。

有办法阻止吗?

这是我的代码:

int direction = 1;
float points = 0;
float speed = 1;
float distance;

PImage bg;
//https://opengameart.org/sites/default/files/Preview-Cave-Parallax.png

goal[] goals = new goal[10];
goal[] level2 = new goal[15];



player p;
void setup() {
    bg = loadImage("bg.png");
    size(800,800);
    background(bg);
    smooth();
    noStroke();

    goals[0] = new goal(40, 100, 60, 1, -1);
    goals[1] = new goal(60, 200, 40, 2, 1);
    goals[2] = new goal(50, 200, 30, 4, -1);
    goals[3] = new goal(16, 250, 30, 3, 1);
    goals[4] = new goal(30, 300, 50, 1.5, -1);
    goals[5] = new goal(40, 350, 60, 5, -1);
    goals[6] = new goal(60, 400, 40, 2, 1);
    goals[7] = new goal(50, 450, 30, 2.5, -1);
    goals[8] = new goal(16, 500, 30, 3, 1);
    goals[9] = new goal(30, 550, 50, 2, -1);

    level2[0] = new goal(40, 100, 60, 1, -1);

    p = new player();
}

void draw() {

    //The starting page

    background(bg);
    fill(0);
    rect(0, 325, 800, 340);
    fill(255);
    textAlign(CENTER, CENTER);
    textSize(50);
    text("Welcome", 400, 350);
    textSize(30);
    text("Rules: Don't let the red balls touch you!", 400, 450);
    text("Controls: Use arrows to move to the left or right.", 400, 500);
    text("Press Space to start.", 400, 550);



    //The Game

    if(key == ' '){
         background(bg);
        fill(255);
        textSize(30);
        text("Score:", 50, 50);
        text(points, 140, 50);
        p.show();
        p.move();
        for(int i =0; i<10; i++){
            if (i%2 == 0){
                goals[i].show();
                goals[i].move();
            }
        }

        if(points >= 2){
            for(int i = 0; i<10; i++){
                if(i%3 == 0){
                    goals[i].show();
                    goals[i].move();
                }
            }
        }

        if(points >= 3) {
            for(int i = 0; i<10; i++){
                goals[i].show();
                goals[i].move();
            }
        }

        for(int i = 0; i<10; i++){
            distance = dist((p.x), (p.y), (goals[i].x), (goals[i].y));
            if(distance <=(15+goals[i].w/2)){

                background(0);
                fill(255);
                textSize(50);
                textAlign(CENTER, CENTER);
                text("GAME OVER", 400, 400);
                noLoop();
            }
        }

        if(p.y == 15 || p.y == 785){
            points = points + 0.5;
            fill(255);
            textSize(30);
            text(points, 140, 50);

        }
    }
    //else {

    // }
}

class goal {
    float x;
    float y;
    float w;
    float c;
    float speed;
    float direction;
    goal(float xpos, float ypos, float wd, float sp,  float dr) {
        x = xpos;
        y = ypos;
        w = wd;
        speed = sp;
        direction = dr;  
    }

    void show() {
        fill(255,0,0);
        ellipse(x,y,w,w);
    }
    void move() {
        x = x + (speed * direction);
        if ((x > 800-w/2) || (x <w/2)) {
            direction = direction * -1;
        } 
    }
}

class player {
    float x = 400;
    float y = 750;
    float speed = 1;
    int direction = 1;
    void show() {
        fill(255);
        ellipse(x, y, 30, 30);
    }
    void move() {
        y = y +(speed * direction);
        if ((y>785)||(y<15)){
            direction = direction*-1;
        }


        if (key == CODED){

            if (keyCode == LEFT){
                if (x >15){
                    x--;
                }
            }
            else if (keyCode == RIGHT){
                if (x <785){
                    x++;
                }
            } 
        }
    }
}

我是新手,所以我不太确定该怎么做。

执行此操作的一种简单方法是使用一个变量来保存游戏的当前状态。例如:String state = "title";.

然后,当玩家按下 space 时,您将状态从 "title" 更改为 "game"(或任何您想要命名的名称)。您需要在绘制函数的开头检查它。

绘制时,检查状态以确定是绘制标题画面还是绘制游戏。例如:if(state.equals("title")) { drawTitle(); }。等等

我还建议将您的逻辑放在主 gameLoop() 中,而实际绘制到屏幕的方法是在游戏循环中适当调用的单独方法,但这超出了本文的范围问题。