Help Me With Maze Levels

edited December 2015 in Questions about Code

I've created a simple maze game. I'm trying to get it so after you reach the designated checkpoint and beat the first level, it loads the next picture and you start the next level. If anyone could help me that would be great. (P.S. the green square on the left is the checkpoint, the one on the right is just to throw people off).

PImage Maze;
PImage Maze2;
PImage Maze3;

int page = 1;
final int maxlevel = 3;

int x = 15;
int y = 40;

void setup()
{
 Maze = loadImage("Maze design 1.png");
 Maze2 = loadImage("Maze Design 2.png");
 Maze3 = loadImage("Maze Design 3.png");
 size(640,480);
}
 void draw(){
 image(Maze,0,0);
  if((x > 111) && (x < 131) && (y > 250) && (y < 270))
   if(page==1) {

 textSize(48);
 textAlign(CENTER);
 fill(250,0,0);
 text("YOU WIN!",width/2,height/2);
 }
 fill(250,0,0);
 noStroke();
 float touch = red(get(x,y));
 ellipse(x,y,10,10);
  fill(0, 250, 0);
  stroke(0);
 rect(320,110,20,20);
 rect(111,250,20,20);

 if(touch <= 200)
 {
 x = 15;
 y = 40;
 }
 println(mouseX + "," + mouseY);
 }
void keyPressed(){
 if((key == CODED) && (keyCode == UP))
 {
 y-=20;
 }
 if((key == CODED) && (keyCode == DOWN))
 {
 y+=20;
 }
 if((key == CODED) && (keyCode == RIGHT))
 {
 x+=20;
 }
 if((key == CODED) && (keyCode == LEFT))
 {
 x-=20;
 }
}
Tagged:

Answers

  • edited December 2015

    you already have a var page

    in the forum it is often called state

    the idea is that draw() runs 60 times per second so you need a way to tell whether you want to display the message you won or loose or a Maze. The var page tells you what to do.

    to do so in draw() say

    void draw() {
    
        switch(page) {
    
            case 0:
            // here all stuff for maze
            break;
    
            case 1:
            // here all stuff for message YOU WIN
            break;
    
            case 2:
            // here all stuff for message YOU LOOSE
            break;
    
            case 3:
            // here all stuff for start / welcome screen if you like 
            break;
    
            case 4:
            // here all stuff for help screen 
            break;
    
        }
    
    }
    

    for the maze 0,1,2 I recommend a second similar var mazeNumber

    now in case 0: above say

    if (mazeNumber == 0) 
    display maze
    
    else if (mazeNumber == 1) 
    display maze1
    
    if (mazeNumber == 2) 
    display maze2
    

    so when the maze is finished mazeNumber++; and page=1;

    so when keyPressed in page 1 say page = 0;

  • Thank you, I will give this a try

  • in fact you need the same thing you have in draw in keyPressed as well

    void keyPressed() {
    
        switch(page) {
    
            case 0:
            // here all stuff for maze
            // what you have in keyPressed now 
            break;
    
            case 1:
            // here all stuff for message YOU WIN
            state = 0; 
            break;
    
            case 2:
            // here all stuff for message YOU LOOSE
            // state = 0; // ???????
            break;
    
            case 3:
            // here all stuff for start / welcome screen if you like 
            // state = 0; // ???????
            break;
    
            case 4:
            // here all stuff for help screen 
            // state = 0; // ???????
            break;
    
        }
    
    }
    
  • i would define constants for each state because 0, 1, 2, 3, 4 are meaningless.

    public static final int STATE_WELCOME = 0;
    public static final int STATE_YOU_WIN = 1;
    public static final int STATE_YOU_LOSE = 2;
    
    ...
    
    switch (page) {
      case STATE_WELCOME:
        ...
        break;
    
      case STATE_YOU_WIN:
        ...
        break;
    
      case STATE_YOU_LOSE:
        ...
        break;
      ....
    }
    
  • Thank you both for your imput, it's really helped

Sign In or Register to comment.