How to make cars constantly drive in opposite lane/direction?
in
Programming Questions
•
2 years ago
I am trying to make a small game consisting of two lanes with one car being controlled by the keypad and continuous cars driving in the opposite direction. I am trying to make it so that if the car crashes into the cars going in the opposite direction the game is over. If someone could help me out or give me some kind of idea on how to do this it would be greatly appreciated!!!! Thank you!
Here is my code:
int x;
int y;
int thesize;
void setup() {
size(600, 600);
rectMode(CENTER);
x = 220;
y = 220;
thesize = 120;
}
void draw() {
simulate();
draw_road();
draw_car();
}
void simulate() {
if (keyPressed) {
if (key==CODED) {
if (keyCode==UP) {
y-=3;
}
if (keyCode==DOWN) {
y+=3;
}
if (keyCode==RIGHT) {
x+=3;
}
if (keyCode==LEFT) {
x-=3;
}
}
}
}
void draw_road() {
background(255);
// Make one side of road
fill(29, 180, 48);
rect(300, 70, 600, 150);
rect(300, 520, 600, 150);
fill(0);
rect(300, 290, 600, 310);
//Make lane marks
fill(255);
stroke(255);
rect(100, 290, 80, 10);
fill(255);
stroke(255);
rect(300, 290, 80, 10);
fill(255);
stroke(255);
rect(500, 290, 80, 10);
}
void draw_car() {
pushMatrix();
translate( x, y );
//draw main car body
stroke(255);
fill(159,86,13);
rect(0, 0, thesize, thesize/2);
// position of wheels relative to car
int offset = thesize/4;
//Draw four wheels relative to center
pushMatrix();
translate( -offset, -offset );
fill(0);
rect(0, 0, offset, offset/2);
popMatrix();
pushMatrix();
translate( -offset, offset );
fill(0);
rect(0, 0, offset, offset/2);
popMatrix();
pushMatrix();
translate( offset, -offset );
fill(0);
rect(0, 0, offset, offset/2);
popMatrix();
pushMatrix();
translate( offset, offset );
fill(0);
rect(0, 0, offset, offset/2);
popMatrix();
popMatrix();
}
1