Tron Game movement

I been making Tron but when I go backwards I die. Can someone help me fix this?

boolean gameOver=false;
boolean gameOver1=false;
Car_2 c=new Car_2();
Car_1 f=new Car_1();
int direction=RIGHT;
int Direction2=LEFT;

boolean backL=false;
boolean backR=false;
boolean backU=false;
boolean backD=false;
boolean backL2=false;
boolean backR2=false;
boolean backU2=false;
boolean backD2=false;
void setup()
{
  size(400, 400);
  background(0);
  frameRate(80);
}
void draw()
{
  if (gameOver==true || gameOver1==true)
  {
    text("GAME OVER", 200, 100);
    text("Click to Restart", 200, 300);
  }
  else if(gameOver==false || gameOver1==false)
  {
    f.human(); 
    c.human2();
   if (direction==RIGHT )
   {
    f.x++;
    
   }
    }
  if (direction==LEFT)
    {
      f.x--;
    }
    if (direction==UP)
    {
      f.y--;
    }
    if (direction==DOWN)
    {
      f.y++;
    }
    //second car
     if (Direction2==RIGHT)
  {
    c.x2++;
  }
    if (Direction2==LEFT)
    {
      c.x2--;
    }
    if (Direction2==UP)
    {
      c.y2--;
    }
    if (Direction2==DOWN)
    {
      c.y2++;
    }
  
  
  if(backL==true)
  {
    direction=RIGHT;
  }
}

void mousePressed()
{
  if(gameOver==true || gameOver1==true)
  {
  if (mousePressed == true)

  {
    background(0);
    f.x=50;
    f.y=200;
    direction=RIGHT;
    c.x2=350;
    c.y2=200;
    Direction2=LEFT;
    gameOver=false;
    gameOver1=false;
  }
  }
}
void keyPressed()
{
  if (key=='a')
  {
    direction=LEFT;
  }
  else if (key=='d')
  {
    direction=RIGHT;
  }
  else if (key=='w')
  {
    direction=UP;
  }
  else if (key=='s')
  {
    direction=DOWN;
  }
  //second car
  if (key=='j')
  {
    Direction2=LEFT;
  }
  else if (key=='l')
  {
    Direction2=RIGHT;
  }
  else if (key=='i')
  {
    Direction2=UP;
  }
  else if (key=='k')
  {
    Direction2=DOWN;
  }
}
class Car_1
{
int x=50;
int y=200;



void human()
{
  if (get(x, y) != color(0,0,0))
  {
    gameOver1=true;
    textSize(15);
    text("player 2 wins!", 210, 120);
    

  }
  stroke(255,0,0);
  point(x, y);

  
  
}

}

class Car_2
{
int x2=350;
int y2=200;

void human2()
{
  if (get(x2, y2) != color(0, 0, 0))
  {
    gameOver=true;
    textSize(15);
    text("player 1 wins!", 210, 120);
    

  }
  stroke(255);
  point(x2, y2);

 
  
}

}

1 Like

Try debugging your code by printing variables or use debugger. read this https://happycoding.io/tutorials/processing/debugging. Don’t worry debugging is a part of programming and you will always learn something new after solving a bug.

BTW game is awesome. :star_struck:

It isn’t really a debugging problem. I just don’t know how to prevent it from going the opposite way and committing suicide. Can you tell me what variables to use?

In keyPressed add a condition before those lines:

if(direction!=right)

This would just make it impossible to turn around 180 degrees.

Chrisir

2 Likes