int pedestrianX;
int pedestrianY;
int vehicleX;
int vehicleY;
PFont f;
int lineX = 0;
int lineY = 80; // Vertical location of each line
int spacing = 50; // How far apart is each line
int len = 50; // Length of each line
float speed = random(0, 10);
void setup() {
size(1200, 400);
f = createFont(“Arial”, 100, true);
}
void draw() {
background(255);
boolean dash = true;
for (int y = 0; y < height; y+=20) {
for (int i = 0; i < width; i+=20) {
if (dash) stroke(0);
else stroke(255);
line(i, 100, i + 20, 100);
dash = !dash;
}
}
//line(0, height / 4, 1200, height / 4);
vehicleX = (frameCount * 5) % width;
vehicleY = 0;
drawPedestrian(pedestrianX, pedestrianY); // This is used to create the pedestrian
drawVehicle(vehicleX, vehicleY); // This is used to create the vehicle.
// The speed can be altered by changing the number multipied by the frameCount.
// The % is used to reset the vehicle
collisionDetection();
fill(0);
if ( collisionDetection()) {
stop();
background(#F70000);
textFont(f);
text(“Game over!”, 300, 200);
}
if (pedestrianY > height) {
pedestrianY = 314;
}
}
void drawVehicle(int vehicleX, int ypos) {
int x[] = { 0 + vehicleX, 150 + vehicleX, 100 + vehicleX, 105 + vehicleX, 70 + vehicleX, 30 + vehicleX, 40 };
int y[] = { 0, 80, 70, 40, 30, 10, 60, 78, 50 }; // These are all common points in the shapes. The vehicleX variable enables the vehicle to move.
stroke(0);
fill(#DE482B);
quad(x[4], y[3], x[2], y[4], x[2], y[1], x[4], y[2]); //This is the hood of the vehicle
fill(#1AB718);
quad(x[0], y[4], x[5], y[3], x[5], y[2], x[0], y[1]); //This is the boot of the vehicle
rectMode(CORNER);
fill(#144EA7);
rect(x[5], y[3], x[6], y[3]); //This is the roof of the vehicle
fill(#E2E359);
quad(x[0], y[1], x[5], y[2], x[4], y[2], x[2], y[1]);
quad(x[0], y[4], x[5], y[3], x[4], y[3], x[2], y[4]); //These are the doors of the vehicle
stroke(#E2E359);
triangle(x[2], y[3], x[1], y[4], x[1], y[6]);
triangle(x[2], y[2], x[1], y[8], x[1], y[7]); //These are the headlights of the vehicle
stroke(0);
}
void drawPedestrian(int pedestrianX, int pedestrianY) {
int x[] = { 11, 31, 600 + pedestrianX};
int y[] = { 10, 20, 30, 30, 314 + pedestrianY, 347 + pedestrianY, 375 + pedestrianY, 392 + pedestrianY };
stroke(0);
fill(#FF1803);
ellipse(x[2], y[4], x[1], y[2]); // This is the head of the pedestrian
fill(#1FC14E);
rectMode(CENTER);
rect(x[2], y[5], x[1], y[3]); // This is the body of the pedestrian
fill(#341FC1);
rect(x[2], y[6], x[0], y[1]); // This is the pillar of the pedestrian
fill(#F5FA17);
ellipse(x[2], y[7], x[0], y[0]); // This is the sphere on which the pedestrian glides on
}
void keyPressed() {
if ((keyPressed == true) && (key == CODED)) {
if (keyCode == UP) {
pedestrianY = pedestrianY - height/4;
} else if (keyCode == DOWN ) {
pedestrianY = pedestrianY + height/4;
} else if (keyCode == LEFT) {
pedestrianX = pedestrianX - 50;
} else if (keyCode == RIGHT) {
pedestrianX = pedestrianX + 50;
}
}
}
boolean collisionDetection() {
float vx = vehicleX + 5;
float vy = vehicleY + 30;
float vw = 150;
float vh = 50;
float px = pedestrianX + 585;
float py = pedestrianY + 300;
float pw = 30;
float ph = 95;
// Turn the lines below on to see borders on the collsion zones.
noFill();
noStroke();
rect(vx, vy, vw, vh);
rect(px, py, pw, ph);
stroke(0);
if (vx + vw >= px && vx <= px + pw && vy + vh >= py && vy <= py + ph) {
return true;
} else {
return false;
}
}