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

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:

//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:

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…

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
1 Like

so this could be the function:

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():

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

now in draw eval youLost

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

1 Like

Sorry i already figured another way:

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
}


1 Like