# Trying to use boolean and if statement to do colour collision detection (Tron)

**URL:** <https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399>\
**Category:** Coding Questions\
**Created:** [November 13, 2020, 1:22pm UTC](https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399 "2020-11-13T13:22:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 13, 2020, 1:22pm UTC](https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399/1 "2020-11-13T13:22:25Z")

</div>

So i have created the tron light bike and now I want it that when it crashes it I want it maybe to reset and then go again. for this I’m using of a crash detection function using get to check the pixel colour, a crash has occurred if the pixel is not the background colour;

the tron light cycle code:

```auto
//arrow keys to draw
float x, y;
int speedY = 0;
int speedX = 1;

void setup() {
  size(500, 500); 
  background(255, 255, 255); //background white
  x=150;
  y=150;
  stroke(0, 0, 0); //pen black
}

void draw() {
  stroke(0, 0, 0);
  point(x, y); //draw a point at current (x,y) 
  updatepoint();
}

boolean crash(float x, float y)
{
  color col=get((int)x,(int)y);
  if (col==color(0,0,0))
  point(x,y);
  return true;
}

void updatepoint() {
  x = x + speedX;
  y = y + speedY;
}
void keyPressed() {
  if (key == CODED)
  {
    if (keyCode == UP && y>=0) { //restrict to screen edge
      speedX=0; 
      speedY=-1;
    } else if (keyCode == DOWN && y<=500) {
      speedX=0; 
      speedY=1;
    } else if (keyCode == LEFT && x>=0) {
      speedX=-1; 
      speedY=0;
    } else if (keyCode == RIGHT && x<=500) {
      speedX=1; 
      speedY=0;
    }
  }
}

```

if and boolean statement I’m working with:

```auto
boolean crash(float x, float y)
{
  color col=get((int)x,(int)y);
  if (col==color(0,0,0))
  point(x,y);
  return true;
}

```

what I don’t understand is boolean uses true and also, so here I’ve said if it has collided then reset to the point of x and y but that seems not too work…

---

<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:** [November 13, 2020, 1:54pm UTC](https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399/2 "2020-11-13T13:54:32Z")

</div>

err…

- you don’t call crash
- crash returns always true
- you display he point in draw, so no use to display it in crash again

---

<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:** [November 13, 2020, 2:41pm UTC](https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399/3 "2020-11-13T14:41:11Z")

</div>

so this could be the function:

```auto
boolean crash(float x, float y)
{
  color col=get((int)x,(int)y);
  if (col==color(0,0,0))
  return false; // no crash, bit confusing?? Changed to false 
  else 
  return true; // crash 
}

```

Now, make a boolean for the program youLost=false;

in draw():

```auto
if (crash(x,y)) 
    youLost=true;

```

now in draw eval youLost

```auto
void draw () {
  background(255); //background white

if (youLost) {
    text("You lost",270,270);
}else {
  // Game
  if (crash(x,y)) 
       youLost=true;
  stroke(0, 0, 0);
  point(x, y); //draw a point at current (x,y) 
  updatepoint();
}
}

```

Chrisir

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 13, 2020, 2:51pm UTC](https://discourse.processing.org/t/trying-to-use-boolean-and-if-statement-to-do-colour-collision-detection-tron/25399/4 "2020-11-13T14:51:40Z")

</div>

Sorry i already figured another way:

```auto
boolean dead;

void setup() {
  size(500, 500); 
  background(255, 255, 255); //background white
  
  x=150; //start 
  y=150;
  
  deltaX = 1;
  deltaY = 0;
  stroke(0, 0, 0); //pen black
  rect(5,5,490,490); //border
  dead=false; //havent hit anything 
}

void draw() 
{
  dead=detect_collision(x,y); //check for collison
  if (!dead) //if not dead
  {
  point(x, y); //draw a point at current (x,y) 
  updatepoint();
  }
  else //am dead
  {
    fill(255,0,0); //red
    text("GAME OVER", 230,230);
  }
}

boolean detect_collision(int x, int y)
{
  color next_pixel=get((int)x,(int)y); //get colour of specified position 
  if (next_pixel !=color(255,255,255)) //not background
  return true; //collision detected
  else 
  return false; //no collsion
}

```
