Car.pde
class Car
{
int x;
int y;
color colour;
int dir;
boolean crashed;
Car()
{
this(0, 0, color(255,0,0), 1, false);
}
Car(int xx, int yy)
{
this(xx, yy, color(255,0,0), 1, false);
}
Car(int xx, int yy, int c)
{
this(xx, yy, c, 1, false);
}
Car(int xx, int yy, color c, int d, boolean crash)
{
if (xx < 0)
x = 0;
else
x = xx;
if (yy < 0)
y = 0;
else
y = yy;
if (d == 1 || d == -1 || d == 2 || d == -2)
dir = d;
else
dir = 0;
colour = c;
crashed = crash;
}
public String toString()
{
return "(" +x + ", " + y + ")" + " dir=" + dir +" crashed :" + crashed;
}
void display()
{
fill(colour);
stroke(colour);
rect(x, y, 100, 50);
stroke(0);
fill(0);
ellipse(x + 20, y + 50, 25, 25);
ellipse(x + 80, y + 50, 25, 25);
if (crashed)
{
strokeWeight(10);
line(x,y,x + 100, y + 50);
line(x,y+50,x + 100, y);
strokeWeight(1);
}
}
void move()
{
if (!crashed)
{
if (abs(dir) == 1)
{
x += dir;
if (x > width - 100)
dir = -1;
if (x < 0)
dir = 1;
}
else
{
if (abs(dir) == 2)
{
y += dir / 2;
if (y > height - 60)
dir = -2;
if (y < 0)
dir = 2;
}
}
}
display();
}
void collidingWith(Car c)
{
if (Math.abs(x - c.x) < 100 && Math.abs(y - c.y) < 50)
{
crashed = true;
c.crashed = true;
display();
c.display();
}
}
boolean equals(Car c)
{
return c!= null && c.colour == colour && c.crashed == crashed;
}
}