【问题标题】:How to make a front page of a game in processing?如何在处理中制作一个游戏的首页?
【发布时间】:2018-11-10 01:42:36
【问题描述】:

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

为了做到这一点,我使用了“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++;
                }
            } 
        }
    }
}

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

【问题讨论】:

    标签: java processing


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 请不要使用== 来比较String 的值。请改用equals() 函数。
    • 抱歉,我最近做的 JavaScript 比 Java 多。
    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多