Making player flee

I am trying to make Luigi flee from mario once mario gets to close to him. Do I just a boolean or float distance?

My code so far with missing pieces:

float marioX = 100;
float marioY = 425;
float luigiX = 400;
float luigiY = 425;
boolean flee;



void setup() {
  size (600, 600);
  smooth();
  flee = false;
}

void draw(){
  background(41, 194, 252);

   fill(54, 252, 41);
stroke(54, 252, 41); 
 rect(0,500, 600, 100);

 fill(254, 255, 36);
 stroke(245, 255, 36);
ellipse(475, 100, 120, 120);

    if (keyPressed && key == 'z') {
    marioX-=5;
    } else if (keyPressed && key == 'x') {
      marioX+=5;
    }

 if (keyPressed && key == CODED) {
  if (keyCode == LEFT) {
    luigiX-=5;
  } else if (keyPressed && keyCode == RIGHT) {
    luigiX+=5;
  }
 }
mario(marioX, marioY, 1.5);
luigiLeft(luigiX, luigiY, 1.5);

if( flee == false){

}
if( flee == true){

 }

 if( dist(x1,y1,x2,y2)<50)
 flee == true;

} 



 void mario(float x, float y, float s) { 
  pushMatrix();
  translate(x-s*.75*50, y-s*50);
  scale(s*100/16);
  strokeWeight(.1);
  stroke(159, 89, 39); // Light brown color 
  fill(159, 89, 39); // Light brown color 
  rect(2, 7, 6, 1);
  rect(1, 8, 10, 1);
  rect(0, 9, 12, 1);
  rect(2, 10, 1, 1);
  rect(9, 10, 1, 1);
  rect(1, 14, 3, 1);
  rect(8, 14, 3, 1);
  rect(0, 15, 4, 1);
  rect(8, 15, 4, 1);
  stroke(244, 164, 96); // Peach color 
  fill(244, 164, 96); // Peach color 
  rect(5, 2, 4, 1);  
  rect(2, 3, 9, 1);
  rect(2, 4, 10, 1);
  rect(3, 5, 4, 1);
  rect(3, 6, 7, 1);
  rect(0, 10, 2, 1);
  rect(10, 10, 2, 1);
  rect(0, 11, 3, 1);
  rect(9, 11, 3, 1);
  rect(0, 12, 2, 1);
  rect(10, 12, 2, 1);
  stroke(220, 0, 0); // Red color 
  fill(220, 0, 0); // Red color 
  rect(4, 7, 1, 1);
  rect(4, 8, 1, 1);
  rect(7, 8, 1, 1);
  rect(4, 9, 4, 1);
  rect(3, 10, 1, 1);
  rect(5, 10, 2, 1);
  rect(8, 10, 1, 1);
  rect(3, 11, 6, 1);
  rect(2, 12, 8, 1);
  rect(2, 13, 3, 1);
  rect(7, 13, 3, 1);
  stroke(139, 69, 19); // Dark brown color 
  fill(139, 69, 19); // Dark brown color 
  rect(2, 2, 3, 1);
  rect(7, 2, 1, 1);
  rect(1, 3, 1, 1);
  rect(3, 3, 1, 1);
  rect(7, 3, 1, 1);
  rect(1, 4, 1, 1);
  rect(3, 4, 2, 1);
  rect(8, 4, 1, 1);
  rect(1, 5, 2, 1);
  rect(7, 5, 4, 1);
  stroke(255, 215, 0); // Gold color 
  fill(255, 215, 0); // Gold color 
  rect(4, 10, 1, 1);
  rect(7, 10, 1, 1);
  stroke(220, 0, 0); // Red color 
  fill(220, 0, 0); // Red color 
  rect(3, 0, 5, 1);
  rect(2, 1, 9, 1);
  popMatrix();
}

void luigiLeft(float x, float y, float s) { 
  pushMatrix();
  translate(x+s*.75*50, y-s*50);
  scale(-s*100/16,s*100/16);
  strokeWeight(.1);
  stroke(46, 139, 87); // Green shirt
  fill(46, 139, 87); // Green shirt
  rect(2, 7, 6, 1);
  rect(1, 8, 10, 1);
  rect(0, 9, 12, 1);
  rect(2, 10, 1, 1);
  rect(9, 10, 1, 1);
  stroke(109, 49, 9); // Dark brown shoes
  fill(109, 49, 9); // Dark brown shoes
  rect(1, 14, 3, 1);
  rect(8, 14, 3, 1);
  rect(0, 15, 4, 1);
  rect(8, 15, 4, 1);
  stroke(244, 164, 96); // Peach color 
  fill(244, 164, 96); // Peach color 
  rect(5, 2, 4, 1);  
  rect(2, 3, 9, 1);
  rect(2, 4, 10, 1);
  rect(3, 5, 4, 1);
  rect(3, 6, 7, 1);
  rect(0, 10, 2, 1);
  rect(10, 10, 2, 1);
  rect(0, 11, 3, 1);
  rect(9, 11, 3, 1);
  rect(0, 12, 2, 1);
  rect(10, 12, 2, 1);
  stroke(45, 40, 240); // Blue overalls
  fill(45, 40, 240); // Blue overalls
  rect(4, 7, 1, 1);
  rect(4, 8, 1, 1);
  rect(7, 8, 1, 1);
  rect(4, 9, 4, 1);
  rect(3, 10, 1, 1);
  rect(5, 10, 2, 1);
  rect(8, 10, 1, 1);
  rect(3, 11, 6, 1);
  rect(2, 12, 8, 1);
  rect(2, 13, 3, 1);
  rect(7, 13, 3, 1);
  stroke(109, 49, 9); // Dark brown facial features
  fill(109, 49, 9); // Dark brown facial features
  rect(2, 2, 3, 1);
  rect(7, 2, 1, 1);
  rect(1, 3, 1, 1);
  rect(3, 3, 1, 1);
  rect(7, 3, 1, 1);
  rect(1, 4, 1, 1);
  rect(3, 4, 2, 1);
  rect(8, 4, 1, 1);
  rect(1, 5, 2, 1);
  rect(7, 5, 4, 1);
  stroke(255, 215, 0); // Gold color 
  fill(255, 215, 0); // Gold color 
  rect(4, 10, 1, 1);
  rect(7, 10, 1, 1);
  stroke(46, 139, 87); // Green hat
  fill(46, 139, 87); // Green hat
  rect(3, 0, 5, 1);
  rect(2, 1, 9, 1);
  popMatrix();
}
Tagged:

Answers

  • I want to make Luigi "flee" to the right once mario gets to close

  • == != = (Learn the difference between assignment (=) and comparison for equal (==))

    Your sketch doesn't have any variables called x1, x2, y1, or y2. But it does have other variables.

  • edited May 2015

    The idea would be to use the distance between two points (variables) and using a conditional. So, if this, then that type of thing.

    In this case, the condition would be the distance and the action would be changing the coordinates of the fleeing character on the X axis.

    From what I see you're doing it but using the wrong parameters. You should re-read your code and figure out what is is you want to do exactly.

  • edited May 2015 Answer ✓

    hello,

    a small remark:

    Luigi could have different modes of behaviour like

    • flee,

    • hide,

    • attack,

    • stand around...

    when the dist() to Mario is too small, set the behaviour of Luigi to flee.

    The modes of behaviour are similar to boolean but allow for different types of behaviour.

    in flee mode Luigi just moves away from Mario (after 3 seconds or so in flee mode check if Mario is still close, if not, go back to stand around mode)

    but this approach (with 3 seconds in a different mode) could look better than something like "if close to mario then flee" because probably Luigi would then move, stop, move again which would look silly maybe...

    you have to test it...

    Best, Chrisir ;-)

  • edited May 2015

    this:

     if( dist(x1,y1,x2,y2)<50)
         flee == true;     
     } 
    

    why?

    which distance do you want to check here really????

    then do it...

  • So the distance would just be the MarioX,MarioY,LuigiX, and LuigiY points?

  • Because I want to check the distance between mario and luigi

  • yes!

    did you try it?

  • edited May 2015

    now you set flee correctly

    but what if flee is true?

    you have these lines

    if ( flee == true) {
    
     }
    

    they can be written as

    if (flee) {
    
     }
    

    but one line is missing here

    hint: you want Luigi move right now....

    ;-)

  • So from this:
    
    if( flee == false){
     float LuigiX = + 0;
    
    }
    if( flee == true){
     float LuigiX = + 50;
     }
    
     if( dist(100, 425, 400, 425)<50) {
     flee = true ;
     }
    
    To:
    
    if(flee){
    luigiX = + 5 
    }
    
    if(list(100, 425, 400, 425)<50){
    flee = true }
    something along those lines?
    
  • edited May 2015 Answer ✓

    oh dear....

    you can just test your stuff, you don't have to ask me...

    it's not if(list it's if(dist with d

    also,

    you wrote

    So the distance would just be the MarioX,MarioY,LuigiX, and LuigiY points?

    well, yes. Why don't you put it in?? I just see if(dist(100, 425, 400, 425)<50){

    Then

    if (flee) {
         float LuigiX = + 50;
    }
    

    to add 50 to LuigiX

    say LuigiX = LuigiX + 50;

    or LuigiX += 50;

    yours won't work I guess (it just sets LuigiX to 50...)

    ;-)

  • edited May 2015

    ok I figured it out before you posted this. My fault. Sorry for the hassle, I really appreciate your time and patience!

  • great!

Sign In or Register to comment.