Trying to change states using the dist function

Essentially what I want to happen is when the player hits a certain point the game will change to the next state at the moment I have this code:

void checkWinLose(){  
    if (dist(x, y, 500.0 ,0.0)  < 45  ){
    state =  "WIN";
1 Like

Hello!

I don’t how many states you you want to change through but using a switch statement, reference here:
https://processing.org/reference/switch.html
May be of interest?

Switching between states isn’t the issue because I can handle that fine the issue is that I specifically want the state to switch when the player hits a certain point, i’m working with 4 states rn

Mmmm, I’m not sure I completely understand. If you can show more code, that would be helpful.
Also, make sure you format it. It’ll be easier for people to read. :nerd_face:
How to do that is here: https://discourse.processing.org/faq#format-your-code

//global variables
Float x = 150; 
Float y = 100;
Float speed = 5.5;
String state;
 Float e = 5.0;
  Float z = 7.0;
//  Float v =
  //Float s =
 // Float j =
   //loat c =

void setup(){
  size(500,500);
  state  = "START";
}
void draw() {

 background(0);
 if (state == "START") {
   startGame();
 } else if (state == "PLAY"){
   playGame();
 } else if (state == "WIN") {
   winGame();
 } else if (state == "LOSE") {
   loseGame();
 }
}

void startGame(){
  textAlign(CENTER);
  textSize(18);
  fill(255,0,0);
  text("Press H to play!" , width/2, height/2);
  if (key == 'h') {
   state = "PLAY";
  }
}

void playGame(){
  fill (245,400,93);
  ellipse (x,y,50,50);
  rect( l, 20, 30, 30);
  rect( 20,e, 30, 30);
  rect( z, 20, 30, 30);
  ellipse(500,0,100,100);
   l = l + 1;
   e = e + 2;
   z = z + 3;
   if(z>width){
     z= 0.0;
   if(e>height){
     e= 4.0;
   }
  if(l>width){
  l= .5;
  }
   }
checkWinLose(); 
    if (dist(x, y, 500.0 ,0.0)  < 45  ){

}

void keyPressed(){

if(key == 'w')
{
  y = y - speed;
}

  {
if(key == 's'){
  y = y + speed;
}
if(key == 'a'){
  x = x - speed;
}
if(key == 'd'){
  x = x + speed;
}
  }
}

void checkWinLose(){  
    if (dist(x, y, 500.0 ,0.0)  < 45  ){
    state =  "WIN";
//    if (dist(a, c, 650, 500) < 250/2 && mousePressed) {
//    state = 3;
  //  if (dist(b, d, 650, 500) < 250/2 && mousePressed) {
//    state = 3;
  //    }
 //     }
   }
}
  

void winGame(){
  
      background(0, 255, 200);
    fill(0);
    text("You did a great job", 775, 50);    
    text("Press the 'P' key to play again.",775, 425);  
    if (keyPressed && key == 'P') {
      state = "START";
    }  
}


void loseGame(){
  background(220, 0, 225);
    text("You did a poor job",775, 50);
    text("Press the 'R' key to retry", 775,425);
    if (keyPressed && key == 'R') {
      state = "PLAY";
}
}
1 Like

Wait I stopped trying to run the code on openprocessing and it worked lool

Thank you! Much easier on the eyes! :grinning:

So, after reading through, unfortunately I cannot answer the specifics of your question. However there are a lot of other members who can!
:slight_smile:

But now this brings up another issue the previous scene doesn’t refresh itself now, how would I go about refreshing the previous state to it’s default?

after losing you’d probably want the player to START a new game, not just PLAY it?

Well this is really just an example so i’d rather it just refresh the previous level I was attempting to use the redraw function but it’s not really working out the way I intended any advice? Same goes for when the player “wins” as well I want the scene to redraw itself

I could be completely misunderstanding what you are trying to do but if you use the switch statements instead of if-else, then you’d be able to direct to the specific level you want to return to. ??

Attention, == won’t work well with Strings; instead use if(state.equals(“START”)) {

equals is important here

2 Likes

When you want to restart the game:
Please set values back to initial values,
especially for x,y,z, l, e

I don’t really think there is a difference between switch and if, concerning what you can do with it in terms of state


Consider pressing ctrl-t in processing to get auto-format

Do this prior to posting


Here you need keyPressed


Sometimes you check the keys with capital letters, sometimes lower case. That could confuse the user. Or you do it intentionally?

2 Likes