How to make game say you won?

I want to make it so when the ball hits the white finish box it says you win and restarts the code.

//Varaibles for images
PImage frankenstein,ghost;
PImage startscreen;
//variables for start screen
int screensizex,screensizey,stage;
//Variables for walls
wall[]walls; 
//circle variables
int x=180;
int y=60;
float start=30;
float stop=30;
//wall color
color colorOfWall = color(255);
color c;
//rectangle stopping point variables
int xr=275,yr=50,wr=90,hr=40;


//Sets the enviroment for the code
void setup() {
//background setup
size(1200, 790);
startscreen=loadImage("maze.jpg");
image (startscreen,0,0,1200,790);
//walls setup
walls=new wall[16];//creates an array(list) for the walls array starts at 0
walls[0]=new wall(150, 30, 10, 730);
walls[1]=new wall(200, 250, 750, 10);  
walls[2]=new wall(250, 150, 10, 500);
walls[3]=new wall(200, 250, 10, 300);
walls[4]=new wall(150, 30, 850, 10);
walls[5]=new wall(150, 750, 860, 10);
walls[6]=new wall(300, 450, 10, 300);
walls[7]=new wall(200, 650, 84, 10);
walls[8]=new wall(300, 300, 80, 10);
walls[9]=new wall(370, 250, 10, 300);
walls[10]=new wall(400, 450, 10, 300);
walls[10]=new wall(370, 630, 300, 10);
walls[11]=new wall(370, 700, 300, 10);
walls[12]=new wall(370, 500, 300, 10);
walls[13]=new wall(400, 380, 10, 300);
walls[14]=new wall(1000, 30, 10, 720);
walls[15]=new wall(750, 100, 10, 500);
}



void draw() {
  if(stage==1);{
    textAlign(CENTER);
    textSize(60);
    fill(0);
    text("Haunted Maze",550,150);
    textSize(18);
    fill(255,255,255);
    text("Press any key to start game",550,175);
    text("Instructions:",1000,30);
    textSize(15);
    text("Reach the end of the Maze by using ",1000,60);
    text("arrow keys or W,A,S,D to move the ball",1000,75);
    if (keyPressed==true){
      stage=2;
    }
  }
  if (stage==2){
//for background 
background (225, 225, 225);//refreshing the background
background(255,116,0);
//image(frankenstein, 1050, 650, 150, 150);
//image(ghost, 20, 20, 120, 120);
textSize(32);
fill(0);
text(millis()/1000, 1100, 30);//sets up a timer in millaseconds so divided by 1000 to get it in seconds
for (int i = 0; i < walls.length; i++) {
  walls[i].draw();
}
Circle();//cirlce function
stoprect();
}
}


//creats a class for the walls of the maze
class wall {
float x;
float y;
float w;
float h;
wall(float x1, float y1, float w1, float h1) {
x = x1;
y = y1;
w = w1;
h = h1;
}


//draws the walls
void draw() {
  fill(0);
  rect(x, y, w, h);
}
}


//user defined function for Circle
void Circle(){
  fill(255);
  ellipse(x, y, start, stop);
if(keyPressed==true) {
  if((keyCode == LEFT || key == 'a')&&get(x-16,y)!=color(0,0,0)) {
    x += -2;
  }
  else if (c==colorOfWall){
    x+=0;
  }
  if ((keyCode == RIGHT || key == 'd')&&get(x+16,y)!=color(0,0,0)) {
    x += 2;
  }
  if ((keyCode == UP || key == 'w')&&get(x,y-16)!=color(0,0,0)) {
    y += -2;
  }
  if ((keyCode == DOWN || key == 's')&&get(x,y+16)!=color(0,0,0)) {
    y += 2;
  }}}
  
  void stoprect(){
    
    fill(255);
    rect(xr,yr,wr,hr);
    fill(0);
    textSize(18);
    text("FINISH",317,70);
    
   
  }
  
   
1 Like

You know the size and position of the ball.
You know the size and position of the finish box.

What can you say about their positions, in relation to their sizes, when they are touching?

If you can work out when they are touching, can you make something happen when they touch? perhaps your game can go to a third state, a win screen, that says you’ve won. Then, when in the win stte, when they press a key, you can go back to state 0 (your menu). What other variables will need their values reset when this happens? Maybe you could put those in a reset() function that both setup() and your go-back-to-state-0 code calls.

1 Like

I had a stage zero but when I included it, it broke the circle and stoprect code in void draw().

This is what I tried doing.

//Varaibles for images
PImage frankenstein,ghost;
PImage startscreen;
//variables for start screen
int screensizex,screensizey,stage;
//Variables for walls
wall[]walls; 
//circle variables
int x=180;
int y=60;
float start=30;
float stop=30;
//wall color
color colorOfWall = color(0);
color c;
//rectangle stopping point variables
int xr=275,yr=50,wr=90,hr=40;


//Sets the enviroment for the code
void setup() {
//background setup
size(1200, 790);
startscreen=loadImage("maze.jpg");
image (startscreen,0,0,1200,790);
//walls setup
walls=new wall[16];//creates an array(list) for the walls array starts at 0
walls[0]=new wall(150, 30, 10, 730);
walls[1]=new wall(200, 250, 750, 10);  
walls[2]=new wall(250, 150, 10, 500);
walls[3]=new wall(200, 250, 10, 300);
walls[4]=new wall(150, 30, 850, 10);
walls[5]=new wall(150, 750, 860, 10);
walls[6]=new wall(300, 450, 10, 300);
walls[7]=new wall(200, 650, 84, 10);
walls[8]=new wall(300, 300, 80, 10);
walls[9]=new wall(370, 250, 10, 300);
walls[10]=new wall(400, 450, 10, 300);
walls[10]=new wall(370, 630, 300, 10);
walls[11]=new wall(370, 700, 300, 10);
walls[12]=new wall(370, 500, 300, 10);
walls[13]=new wall(400, 380, 10, 300);
walls[14]=new wall(1000, 30, 10, 720);
walls[15]=new wall(750, 100, 10, 500);
}



void draw() {
  if(stage==1);{
    textAlign(CENTER);
    textSize(60);
    fill(0);
    text("Haunted Maze",550,150);
    textSize(18);
    fill(255,255,255);
    text("Press any key to start game",550,175);
    text("Instructions:",1000,30);
    textSize(15);
    text("Reach the end of the Maze by using ",1000,60);
    text("arrow keys or W,A,S,D to move the ball",1000,75);
    if (keyPressed==true){
      stage=2;
    }
  }
  if (stage==2){
//for background 
background (225, 225, 225);//refreshing the background
background(255,116,0);
//image(frankenstein, 1050, 650, 150, 150);
//image(ghost, 20, 20, 120, 120);
textSize(32);
fill(0);
text(millis()/1000, 1100, 30);//sets up a timer in millaseconds so divided by 1000 to get it in seconds
for (int i = 0; i < walls.length; i++) {
  walls[i].draw();
}
if (xr==x){
  stage=3;
}
}
  if (stage==3){
    text("Game Over",1000,60);
    
}
Circle();//cirlce function
stoprect();
}
}



//creats a class for the walls of the maze
class wall {
float x;
float y;
float w;
float h;
wall(float x1, float y1, float w1, float h1) {
x = x1;
y = y1;
w = w1;
h = h1;
}


//draws the walls
void draw() {
  fill(0);
  rect(x, y, w, h);
}
}


//user defined function for Circle
void Circle(){
  fill(255);
  ellipse(x, y, start, stop);
if(keyPressed==true) {
  if((keyCode == LEFT || key == 'a')&&get(x-16,y)!=color(0,0,0)) {
    x += -2;
  }
  else if (c==colorOfWall){
    x+=0;
  }
  if ((keyCode == RIGHT || key == 'd')&&get(x+16,y)!=color(0,0,0)) {
    x += 2;
  }
  if ((keyCode == UP || key == 'w')&&get(x,y-16)!=color(0,0,0)) {
    y += -2;
  }
  if ((keyCode == DOWN || key == 's')&&get(x,y+16)!=color(0,0,0)) {
    y += 2;
  }}}
  
  void stoprect(){
    
    fill(255);
    rect(xr,yr,wr,hr);
    fill(0);
    textSize(18);
    text("FINISH",317,70);
    
   
  }
  
   

Tried making the ball reset back to its original position and can’t get it to work. Any suggestions?

//Varaibles for images
PImage frankenstein,ghost;
PImage startscreen;
//variables for start screen
int screensizex,screensizey,stage;
//Variables for walls
wall[]walls; 
//circle variables
int x=180;
int y=60;
float start=30;
float stop=30;
//wall color
color colorOfWall = color(255);
color c;
//rectangle stopping point variables
int xr=275,yr=50,wr=90,hr=40;


//Sets the enviroment for the code
void setup() {
//background setup
size(1200, 790);
startscreen=loadImage("maze.jpg");
image (startscreen,0,0,1200,790);
//walls setup
walls=new wall[16];//creates an array(list) for the walls array starts at 0
walls[0]=new wall(150, 30, 10, 730);
walls[1]=new wall(200, 250, 750, 10);  
walls[2]=new wall(250, 150, 10, 500);
walls[3]=new wall(200, 250, 10, 300);
walls[4]=new wall(150, 30, 850, 10);
walls[5]=new wall(150, 750, 860, 10);
walls[6]=new wall(300, 450, 10, 300);
walls[7]=new wall(200, 650, 84, 10);
walls[8]=new wall(300, 300, 80, 10);
walls[9]=new wall(370, 250, 10, 300);
walls[10]=new wall(400, 450, 10, 300);
walls[10]=new wall(370, 630, 300, 10);
walls[11]=new wall(370, 700, 300, 10);
walls[12]=new wall(370, 500, 300, 10);
walls[13]=new wall(400, 380, 10, 300);
walls[14]=new wall(1000, 30, 10, 720);
walls[15]=new wall(750, 100, 10, 500);

}

void reset(){
  
  

}

  



void draw() {
  if(stage==1);{
    textAlign(CENTER);
    textSize(60);
    fill(0);
    text("Haunted Maze",550,150);
    textSize(18);
    fill(255,255,255);
    text("Press any key to start game",550,175);
    text("Instructions:",1000,30);
    textSize(15);
    text("Reach the end of the Maze by using ",1000,60);
    text("arrow keys or W,A,S,D to move the ball",1000,75);
    if (keyPressed==true){
      stage=2;
    }
  }
  if (stage==2){
//for background 
background (225, 225, 225);//refreshing the background
background(255,116,0);
//image(frankenstein, 1050, 650, 150, 150);
//image(ghost, 20, 20, 120, 120);
textSize(32);
fill(0);
text(millis()/1000, 1100, 30);//sets up a timer in millaseconds so divided by 1000 to get it in seconds
for (int i = 0; i < walls.length; i++) {
  walls[i].draw();
}
Circle();//cirlce function
stoprect();
}
}


//creats a class for the walls of the maze
class wall {
float x;
float y;
float w;
float h;
wall(float x1, float y1, float w1, float h1) {
x = x1;
y = y1;
w = w1;
h = h1;
}


//draws the walls
void draw() {
  fill(0);
  rect(x, y, w, h);
}
}


//user defined function for Circle
void Circle(){
  fill(255);
  ellipse(x, y, start, stop);
if(keyPressed==true) {
  if((keyCode == LEFT || key == 'a')&&get(x-16,y)!=color(0,0,0)) {
    x += -2;
  }
  else if (c==colorOfWall){
    x+=0;
  }
  if ((keyCode == RIGHT || key == 'd')&&get(x+16,y)!=color(0,0,0)) {
    x += 2;
  }
  if ((keyCode == UP || key == 'w')&&get(x,y-16)!=color(0,0,0)) {
    y += -2;
  }
  if ((keyCode == DOWN || key == 's')&&get(x,y+16)!=color(0,0,0)) {
    y += 2;
  }}}
  
  void stoprect(){
    
    fill(255);
    rect(xr,yr,wr,hr);
    fill(0);
    textSize(18);
    text("FINISH",317,70);
    if (key==('r')){
      reset();
    }
    
   
  }
  

Here is some example code that uses states and a reset() function. Make sure you understand the concepts this presents, and then apply them to your own code:

int state;
final int state_menu=0, state_playing=1, state_end=64330737;
int score;

void setup() {
  size(600, 400);
  textSize(16);
  reset();
}

void reset(){
  score = 0;
}

void draw() {
  if ( state == state_menu ) {
    background(100, 0, 0);
    text( "Mouse Click Game!", 20, 20);
    text( "Click to start. Click for points. Press any key to game over!", 20, 40);
  }
  if ( state == state_playing ) {
    background(0, 100, 0);
    text( "Score: " + score, 20, 20 );
  }
  if ( state == state_end ) {
    background(0, 0, 100);
    text( "Game over.", 20, 20);
    text( "Click again to go back to menu!", 20, 40);
  }
}

void mousePressed() {
  if ( state == state_menu ) {
    state = state_playing;
    return;
  }
  if ( state == state_playing ) {
    score++;
    return;
  }
  if ( state == state_end ) {
    state = state_menu;
    reset();
    return;
  }
}

void keyPressed() {
  if ( state == state_playing ) {
    state = state_end;
    return;
  }
}
1 Like

I figured out I was putting the code in the wrong order so it wasn’t reading stuff at the right time. I got the win game stage working but now im not sure about how to get it to detect the finish box.

//Varaibles for images
PImage frankenstein,ghost;
PImage startscreen;
//variables for start screen
int screensizex,screensizey,stage;
//Variables for walls
wall[]walls; 
//circle variables
int x=180;
int y=60;
float start=30;
float stop=30;
//wall color
color colorOfWall = color(255);
color c;
//rectangle stopping point variables
int xr=275,yr=50,wr=90,hr=40;



//Sets the enviroment for the code
void setup() {
//background setup
size(1200, 790);
startscreen=loadImage("maze.jpg");
image (startscreen,0,0,1200,790);
//walls setup
walls=new wall[16];//creates an array(list) for the walls array starts at 0
walls[0]=new wall(150, 30, 10, 730);
walls[1]=new wall(200, 250, 750, 10);  
walls[2]=new wall(250, 150, 10, 500);
walls[3]=new wall(200, 250, 10, 300);
walls[4]=new wall(150, 30, 850, 10);
walls[5]=new wall(150, 750, 860, 10);
walls[6]=new wall(300, 450, 10, 300);
walls[7]=new wall(200, 650, 84, 10);
walls[8]=new wall(300, 300, 80, 10);
walls[9]=new wall(370, 250, 10, 300);
walls[10]=new wall(400, 450, 10, 300);
walls[10]=new wall(370, 630, 300, 10);
walls[11]=new wall(370, 700, 300, 10);
walls[12]=new wall(370, 500, 300, 10);
walls[13]=new wall(400, 380, 10, 300);
walls[14]=new wall(1000, 30, 10, 720);
walls[15]=new wall(750, 100, 10, 500);
loop();

}

void win(){
  size(1200, 790);
startscreen=loadImage("maze.jpg");
image (startscreen,0,0,1200,790);
fill(0);
textSize(24);
text("Haunted Maze made by Anthony",550,150);
textSize(30);
text("You Win!",550,300);
  
  

}

  



void draw() {
  if(stage==1);{
    textAlign(CENTER);
    textSize(60);
    fill(0);
    text("Haunted Maze",550,150);
    textSize(18);
    fill(255,255,255);
    text("Press any key to start game",550,175);
    text("Instructions:",1000,30);
    textSize(15);
    text("Reach the end of the Maze by using ",1000,60);
    text("arrow keys or W,A,S,D to move the ball",1000,75);
    if (keyPressed==true){
      stage=2;
    }
  }
  if (stage==2){
//for background 
background (225, 225, 225);//refreshing the background
background(255,116,0);
//image(frankenstein, 1050, 650, 150, 150);
//image(ghost, 20, 20, 120, 120);
textSize(32);
fill(0);
text(millis()/1000, 1100, 30);//sets up a timer in millaseconds so divided by 1000 to get it in seconds
for (int i = 0; i < walls.length; i++) {
  walls[i].draw();
}
Circle();//cirlce function
stoprect();
}
}


//creats a class for the walls of the maze
class wall {
float x;
float y;
float w;
float h;
wall(float x1, float y1, float w1, float h1) {
x = x1;
y = y1;
w = w1;
h = h1;
}


//draws the walls
void draw() {
  fill(0);
  rect(x, y, w, h);
}
}


//user defined function for Circle
void Circle(){
  fill(255);
  ellipse(x, y, start, stop);
if(keyPressed==true) {
  if((keyCode == LEFT || key == 'a')&&get(x-16,y)!=color(0,0,0)) {
    x += -2;
  }
  else if (c==colorOfWall){
    x+=0;
  }
  if ((keyCode == RIGHT || key == 'd')&&get(x+16,y)!=color(0,0,0)) {
    x += 2;
  }
  if ((keyCode == UP || key == 'w')&&get(x,y-16)!=color(0,0,0)) {
    y += -2;
  }
  if ((keyCode == DOWN || key == 's')&&get(x,y+16)!=color(0,0,0)) {
    y += 2;
  }}}
  
  void stoprect(){
    
    fill(255);
    rect(xr,yr,wr,hr);
    fill(0);
    textSize(18);
    text("FINISH",317,70);
    if(**Don't know how to detect the finish box here**){
      win();
      noLoop();
    }
    
   
  }
  
1 Like

A circle and a rectangle touch when…

  • The center of the circle is vertically between the top side of the box and the bottom side of the box and is horizontally between the left side of the box minus the radius and the right side of the box plus the radius.
    -OR-
  • The center of the circle is horizontally between the left side of the box and the right side of the box and is vertically between the top side of the box minus the radius and the bottom side of the box plus the radius.
    -OR-
  • The distance between the center of the circle and a corner of the box is less than the radius.

Convert the above to code. A diagram would help immensely.

1 Like