Search code examples
processing

Game Over bug due to if statement conditions


The language used is called 'Processo' a weaker and simpler version of Processing used at my school to introduce Java syntax in a visual way. The overall purpose is similar to crossy road, but the cars are moving down the screen as the player avoids them. The player can only move horizontally, and the enemies only move vertically. Using if statements, once the enemy cars get below the screen that the user can see, the enemy car's y-variable is reset to a random y-coordinate between -1000 to -100 (above the screen).

At first the program runs fine, but immediately runs into a bug which causes the "Game Over" screen, despite the player not colliding with the enemies. I think the issue is with the collision conditions (the last three if statements in the gameScreen function).

size (300, 400);

double playerX = mouseX-20;
double playerY = 320;
double playerWidth = 40;
double playerHeight = 40;

double enemy1X = 10;
double enemy1Y = random(-1000, -100);
double enemy1Width = 80;
double enemy1Height = 80;

double enemy2X = 110;
double enemy2Y = random(-1000, -100);
double enemy2Width = 80;
double enemy2Height = 80;

double enemy3X = 210;
double enemy3Y = random(-1000, -100);
double enemy3Width = 80;
double enemy3Height = 80;

int gamePage = 0;

void player() {
    noStroke();
    fill(rgb(0, 255, 0));
    rect(mouseX-20, 320, 40, 40);
}

void enemy1() {
    fill(rgb(255, 0, 0));
    rect(enemy1X, enemy1Y, enemy1Width, enemy1Height);
}

void enemy2() {
    rect(enemy2X, enemy2Y, enemy2Width, enemy2Height);
}

void enemy3() {
    rect(enemy3X, enemy3Y, enemy3Width, enemy3Height);
}

void initalScreen() {
    background(rgb(0, 0, 0));
    text("Click to start", width/2, height/2);
}
void gameScreen() {
    background(rgb(255, 255, 255));
    
    if(enemy1Y >= 400) {
        enemy1Y = random(-1000, -100);
    } else {
        enemy1Y += 5;
    }
    if(enemy2Y >= 400) {
        enemy2Y = random(-1000, -100);
    } else {
        enemy2Y += 5;
    }
    if(enemy3Y >= 400) {
        enemy3Y = random(-1000, -100);
    } else {
        enemy3Y += 5;
    }

    //collision
    if (playerX + playerWidth > enemy1X && playerX < enemy1X + enemy1Width && playerY + playerHeight > enemy1Y && playerY < enemy1Y + enemy1Height) {
        gamePage = 2;
    } 
    if (playerX + playerWidth > enemy2X && playerX < enemy2X + enemy2Width && playerY + playerHeight > enemy2Y && playerY < enemy2Y + enemy2Height) {
        gamePage = 2;
    } 
    if (playerX + playerWidth > enemy3X && playerX < enemy3X + enemy3Width && playerY + playerHeight > enemy3Y && playerY < enemy3Y + enemy3Height) {
        gamePage = 2;
    } 
    
    player();
    enemy1();
    enemy2();
    enemy3();
}
void gameOverScreen() {
    background(rgb(0, 0, 0));
    textSize(30);
    text("Game Over", width/2, height/2);
}

void startGame() {
    gamePage = 1;
}


void mousePressed() {
    if(gamePage == 0) {
        startGame();
    }
}

void draw() {
    background(rgb(226, 225, 225));
    if (gamePage == 0) {
        initalScreen();
    } else if (gamePage == 1) {
        gameScreen();
    } else if (gamePage == 2) {
        gameOverScreen();
    }

}


Solution

  • So the issue is

    double playerX = mouseX-20;
    

    is not being updated with the loop instead your actually just calling it once making the program think its still in the same place. You can fix this by moving the code above into the gameScreen() method and that will update its location the next time you move your player. So it should look something like this.

    void gameScreen() {
    background(rgb(255, 255, 255));
    double playerX = mouseX-20; // Moved Variable
    
    if(enemy1Y >= 400) {
        enemy1Y = random(-1000, -100);
    } else {
        enemy1Y += 5;
    }
    if(enemy2Y >= 400) {
        enemy2Y = random(-1000, -100);
    } else {
        enemy2Y += 5;
    }
    if(enemy3Y >= 400) {
        enemy3Y = random(-1000, -100);
    } else {
        enemy3Y += 5;
    }
    
    //collision
    if (playerX + playerWidth > enemy1X && playerX < enemy1X + enemy1Width && playerY + playerHeight > enemy1Y && playerY < enemy1Y + enemy1Height) {
        gamePage = 2;
    } 
    if (playerX + playerWidth > enemy2X && playerX < enemy2X + enemy2Width && playerY + playerHeight > enemy2Y && playerY < enemy2Y + enemy2Height) {
        gamePage = 2;
    } 
    if (playerX + playerWidth > enemy3X && playerX < enemy3X + enemy3Width && playerY + playerHeight > enemy3Y && playerY < enemy3Y + enemy3Height) {
        gamePage = 2;
    } 
    
    player();
    enemy1();
    enemy2();
    enemy3();
    
    }
    

    Then Just remove the variable from the top so you don't have two of the same variable. Hope this helps!