# Frogger game (make the frog not to go outside)

**URL:** <https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825>\
**Category:** Coding Questions\
**Created:** [March 20, 2020, 1:02am UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825 "2020-03-20T01:02:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhghi](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nhghi](https://discourse.processing.org/u/nhghi)\
**Post date:** [March 20, 2020, 1:02am UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/1 "2020-03-20T01:02:59Z")

</div>

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?

```auto
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;

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 20, 2020, 12:57pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/2 "2020-03-20T12:57:56Z")

</div>

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

```auto

// 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;
}     
//

```

---

<div class="post-metadata">

**Author:** ![nhghi](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nhghi](https://discourse.processing.org/u/nhghi)\
**Post date:** [March 20, 2020, 4:58pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/3 "2020-03-20T16:58:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 20, 2020, 5:01pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/4 "2020-03-20T17:01:34Z")

</div>

> [@Chrisir](#):
>
> 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; } }

replace 1 with `bandHeight`

---

<div class="post-metadata">

**Author:** ![nhghi](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nhghi](https://discourse.processing.org/u/nhghi)\
**Post date:** [March 20, 2020, 5:07pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/5 "2020-03-20T17:07:16Z")

</div>

I did this but it still move like the last time

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 20, 2020, 5:16pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/6 "2020-03-20T17:16:49Z")

</div>

Can you please post your entire code?

---

<div class="post-metadata">

**Author:** ![nhghi](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nhghi](https://discourse.processing.org/u/nhghi)\
**Post date:** [March 20, 2020, 5:19pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/7 "2020-03-20T17:19:46Z")

</div>

```auto
// 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;
}     
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 20, 2020, 5:24pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/9 "2020-03-20T17:24:18Z")

</div>

like this

```auto
// 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;
}     
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 20, 2020, 5:26pm UTC](https://discourse.processing.org/t/frogger-game-make-the-frog-not-to-go-outside/18825/10 "2020-03-20T17:26:40Z")

</div>

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
