java - 如何制作正在处理的游戏首页?

标签 java processing

我正在尝试为一项作业编写一个游戏。我有勇气完成它,但我需要在开始时说一些规则等。

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

有办法阻止这种情况吗?

这是我的代码:

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";

然后,当玩家按下空格键时,您可以将状态从 "title" 更改为 "game" (或任何您想命名的名称)。您需要在绘图函数开始时检查这一点。

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

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

关于java - 如何制作正在处理的游戏首页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226280/

相关文章:

java - java 中具有 List<string> 参数的方法应使用什么数据类型?

javascript - 在javascript中使用Processing的loadTable

javascript - 全局变量在 jquery/javascript 中如何工作? (初学者级)

java - Processing(二维图形)是否适合大型项目?

javascript - loadPixels() 不适用于 p5.js

java - 从图库中选择一张图片并将其显示在另一个 Activity 中

java - 使用 Properties 动态读取/添加值到 conf 文件的参数

java - 累计金额问题

java - 使用 Appium 并行运行 2 个真实的 Android 设备

java - Leap Motion震颤识别?