# I need help is to make an easy game but i have some problem now

**URL:** <https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803>\
**Category:** Coding Questions\
**Created:** [November 21, 2018, 7:44pm UTC](https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803 "2018-11-21T19:44:26Z")\
**Posts on this page:** 3\
**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:** [November 21, 2018, 7:44pm UTC](https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803/1 "2018-11-21T19:44:26Z")

</div>

my basic idea is to draw a small planet (ellipse code) and then make it move. But I want to minimize where it can go.  
like I will make a boolean ( kinda hitbox) wich smaller than my planet, if the hitbox touches the rear of the canvas it only moves the other direction, not the direction go out of the canvas.

```auto
float x,y;

void setup()
{
  size(1000, 1000);
  x=width/2;
  y=height/5*(4.5);
 
}
void draw()
{
  draw_planet(x,y,width/50);
}
void draw_planet(float x, float y, float diam) {

  fill(0, 100, 0);
  ellipse(x,y,diam,diam);
  
}
void Planet_move(float x_flight, float y_flight){
y=y+y_flight;
x=x+x_flight;
}
void keyPressed()
{
  if (key=='w' ||key =='i')
  {
    Planet_move(0,-10);
  }
   else if (key == 's'|| key =='k')
  {
    Planet_move(0, 10);
    
  } else if (key == 'd' | key == 'l')
  {
    Planet_move(10, 0);
    
  } else if (key == 'a' | key=='j')
  {
    Planet_move(-10, 0);
  }
}
void Planet_move_in_the_canvas_or_not()
{

  
}
 

```

I want to use boolean but I cant type`boolean planet(float x, float y, float z)=true`

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 22, 2018, 12:24am UTC](https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803/2 "2018-11-22T00:24:49Z")

</div>

i understand that you think you need a boolean function,  
more inside code and some clean up i hope you like.

```auto
// https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803
/*
question about boolean function
when you want do like:

if ( hungry() ) eat();

// need function
void eat(){ }
// and boolean function
boolean hungry(){
 if ( eatlastTime > 5min ) return true;
 return false; 
}

*/

int my_width = 500, my_height = my_width; // window
float x = my_width/2, y = 45, w = my_width/50, step = 10; // object position
float fence_x = 20, fence_y = 20, fence_w = my_width-2*fence_x; // 

void settings() {
  size(my_width, my_height);  
}

void setup() {
  println("use key [w][s][d][a]");
}

void draw() {
  background(200,200,0); // _____________ add
  noFill();
  stroke(0,0,200);
  rect(fence_x,fence_y,fence_w,fence_w); // _____________ add
  planet_draw(x, y, w);
}

void planet_draw(float x, float y, float diam) {
  fill(0, 100, 0);
  noStroke();
  ellipse(x, y, diam, diam);
}

void planet_move(float x_flight, float y_flight) {
  y=y+y_flight;
  x=x+x_flight;
  if (planet_check_fence(x,y,w)) { println("what do now?"); }
}

boolean planet_check_fence(float x, float y, float diam) {
  // if x y at fence return true; ??
  return false;
}

void keyPressed() {
  if ( key == 'w' || key == 'i') planet_move(0, -step);
  else if ( key == 's' || key == 'k') planet_move(0, step);
  else if ( key == 'd' || key == 'l') planet_move( step, 0);
  else if ( key == 'a' || key == 'j') planet_move(-step, 0);
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 25, 2018, 5:23pm UTC](https://discourse.processing.org/t/i-need-help-is-to-make-an-easy-game-but-i-have-some-problem-now/5803/3 "2018-11-25T17:23:00Z")

</div>

> [@nhghi](#):
>
> if the hitbox touches the rear of the canvas it only moves the other direction, not the direction go out of the canvas.

Look for “bounce” examples in the PDE examples folder and in [Processing.org](http://Processing.org) examples. Also look for “collision detection” – these are the ways to describe related problems, and you will find lots of example code, tutorials, and resources. Coding Train and Happy Coding also have tutorials on collision detection with bouncing.
