Frogger game (make the frog not to go outside)

hi, guys, Iā€™m trying to make a new game. the basic idea is if my green frog will go around the canvas but it will not go outside of it. I created a objectInCanvas() function to see if the frog goes outside or not. so right now I check when the frog moves to anywhere it will pass that x,y value and see if the next move is outside or not. my question is how can I modify the movefrog() funtion to base on the objectInCanvas() function to make the frog not to go outside?

float bandHeight;
float frogx, frogy;
float frog_move, frog_moveX, frog_moveY;

float frog_size, frogX_now, frogY_now;
void setup()
{
 size(1000,500); 
}

void draw()
{
  background(0);
   bandHeight=height/(1+4);
  frog_move=bandHeight;
  frog_size=bandHeight/3;
 frog(); 
}

void frog()
{

  moveFrog(frogx, frogy);
  drawFrog(frogX_now, frogY_now, frog_size);
}
void drawFrog(float x, float y, float diam)
{
  fill(#05FF52);
  ellipse(x, y, diam, diam);
}


void moveFrog(float xChange, float yChange)
{
     frogX_now=width/2+frog_moveX;
  frogY_now=height-bandHeight/2+frog_moveY;
  println(xChange, yChange, objectInCanvas(frogX_now-bandHeight/3, frogY_now, frog_size));

  if (objectInCanvas(frogX_now-bandHeight/3, frogY_now, frog_size))
  {
    frog_moveX=xChange*bandHeight/3;
    frog_moveY=yChange*bandHeight;
   
  } 
  else if (objectInCanvas(width/2*xChange-bandHeight/3, frogY_now, frog_size))
{
  frog_moveX=xChange*bandHeight/3;
    frog_moveY=yChange*bandHeight;
}}

void keyPressed()
{

  if (key=='a')
  {
    frogx+=-1;
  } else if (key=='w')
  {
    frogy+=-1;
  } else if (key=='s')
  {
    frogy+=1;
  } else if (key=='d')
  {
    frogx+=1;
  }
}

boolean objectInCanvas(float x, float y, float diam)
{

  if (x+diam/2<0 || x-diam/2>width|| y<0||y>height)
  {
    return false;
  }

  return true;
}     

thank guys;

1 Like

Hello,

here is my version
the naming is not so good

  • float frogx, frogy; is movement
  • float frog_moveX, frog_moveY; is position

Regards, Chrisir


// float bandHeight;
float frogx, frogy;  // movement 

// float frog_move,
float frog_moveX, frog_moveY;  // position 

float frog_size;  // , frogX_now, frogY_now;

void setup()
{
  size(1000, 500);
  frog_moveX=width/2; 
  frog_moveY=height/2;
}

void draw()
{
  background(0);
  float bandHeight=height/(1+4);
  //  frog_move=bandHeight;
  frog_size=bandHeight/3;
  frog();
}

// ------------------------------------------------------------------------

void frog()
{

  moveFrog( frogx, frogy); // movement 
  drawFrog( frog_moveX, frog_moveY, frog_size); // position
}

void drawFrog(float x, float y, float diam)
{
  fill(#05FF52);
  ellipse(x, y, diam, diam);
  ellipse(x-4, y-4, diam/7, diam/7);
  ellipse(x+4, y-4, diam/7, diam/7);
}

void moveFrog(float xChange, float yChange)
{

  if (objectInCanvas(frog_moveX+xChange, frog_moveY+yChange, frog_size))
  {
    frog_moveX=frog_moveX+xChange;
    frog_moveY=frog_moveY+yChange;
  }
}

void keyPressed()
{
  frogx=0; 
  frogy=0; 

  if (key=='a')
  {
    frogx+=-1;
  } else if (key=='w')
  {
    frogy+=-1;
  } else if (key=='s')
  {
    frogy+=1;
  } else if (key=='d')
  {
    frogx+=1;
  }
}

boolean objectInCanvas(float x, float y, float diam) 
{

  if (x < diam/2 || x+diam/2>width|| y<diam/2 || y+diam/2>height)
  {
    return false; // NOT in canvas
  }

  return true;
}     
//

thank you so much but how can I change it llike: move only 1 step bandHeight every time I pressed 1 key?

replace 1 with bandHeight

I did this but it still move like the last time

void keyPressed()
{
  frogx=0; 
  frogy=0; 
println(bandHeight);
  if (key=='a')
  {
    frogx+=-bandHeight/9;
  } else if (key=='w')
  {
    frogy+=-bandHeight;
  } else if (key=='s')
  {
    frogy+=bandHeight;
  } else if (key=='d')
  {
    frogx+=bandHeight/9;
  }
}

Can you please post your entire code?

// float bandHeight;
float frogx, frogy;  // movement 

// float frog_move,
float frog_moveX, frog_moveY;  // position 

float frog_size;  // , frogX_now, frogY_now;
float bandHeight;
void setup()
{
  size(1000, 500);
  frog_moveX=width/2; 
  frog_moveY=height/2;
}

void draw()
{println(bandHeight);
  background(0);
  bandHeight=height/(1+4);
  //  frog_move=bandHeight;
  frog_size=bandHeight/3;
  frog();
}

// ------------------------------------------------------------------------

void frog()
{

  moveFrog( frogx, frogy); // movement 
  drawFrog( frog_moveX, frog_moveY, frog_size); // position
}

void drawFrog(float x, float y, float diam)
{
  fill(#05FF52);
  ellipse(x, y, diam, diam);
 
}

void moveFrog(float xChange, float yChange)
{

  if (objectInCanvas(frog_moveX+xChange, frog_moveY+yChange, frog_size))
  {
    frog_moveX=frog_moveX+xChange;
    frog_moveY=frog_moveY+yChange;
  }
}

void keyPressed()
{
  frogx=0; 
  frogy=0; 

  if (key=='a')
  {
    frogx=-bandHeight;
  } else if (key=='w')
  {
    frogy=-bandHeight;
  } else if (key=='s')
  {
    frogy=bandHeight;
  } else if (key=='d')
  {
    frogx=bandHeight;
  }
}

boolean objectInCanvas(float x, float y, float diam) 
{

  if (x < diam/2 || x+diam/2>width|| y<diam/2 || y+diam/2>height)
  {
    return false; // NOT in canvas
  }

  return true;
}     
//

like this

// float bandHeight;
float frogx, frogy;  // movement 

// float frog_move,
float frog_moveX, frog_moveY;  // position 

float frog_size;  // , frogX_now, frogY_now;
float bandHeight;
void setup()
{
  size(1000, 500);
  frog_moveX=width/2; 
  frog_moveY=height/2;
}

void draw()
{
  println(bandHeight);
  background(0);
  bandHeight=height/(1+4);
  //  frog_move=bandHeight;
  frog_size=bandHeight/3;
  //  bandHeight=14; 
  frog();
}

// ------------------------------------------------------------------------

void frog()
{

  moveFrog( frogx, frogy); // movement 
  drawFrog( frog_moveX, frog_moveY, frog_size); // position
}

void drawFrog(float x, float y, float diam)
{
  fill(#05FF52);
  ellipse(x, y, diam, diam);
}

void moveFrog(float xChange, float yChange)
{
  frogx=0; 
  frogy=0; 
  if (objectInCanvas(frog_moveX+xChange, frog_moveY+yChange, frog_size))
  {
    frog_moveX=frog_moveX+xChange;
    frog_moveY=frog_moveY+yChange;
  }
}

void keyPressed()
{
  frogx=0; 
  frogy=0; 

  if (key=='a') {
    frogx=-bandHeight;
  } else if (key=='w')
  {
    frogy=-bandHeight;
  } else if (key=='s')
  {
    frogy=bandHeight;
  } else if (key=='d')
  {
    frogx=bandHeight;
  }
}

boolean objectInCanvas(float x, float y, float diam) 
{

  if (x < diam/2 || x+diam/2>width|| y<diam/2 || y+diam/2>height)
  {
    return false; // NOT in canvas
  }

  return true;
}     
//

when frogger stops on a screen border, it stops far before it (since bandHeight is so high)

you could set frogger to position directly on the screen border if you want

1 Like