Highway Madness

For this week’s object programming project, I decided to create a game. I was inspired by a game application on my phone that features a person attempting to cross roads with moving cars and obstacles.

I first played with the phenomenon of moving cars with different speeds and directions and created this canvas.

int num = 500; // number of cars
Car[] myCars; 

void setup() {
 size(500, 500);
 myCars = new Car[num]; 
 for (int i =0;i<num;i++) {
 myCars[i] = new Car();

void draw() {
 for (int i =0;i<num;i++) {

class Car {
 color col;
 int x;
 int y;
 int speed;

 Car () {
 col = color(random(256), random(256), random(256), random(256));
 speed = int(random(1, 10));
 if (random(1)>0.5) {
 speed = -speed;
 x = int(random(width));
 y = int(random(height));

 void draw() {
 rect(x, y, 20, 10);
 ellipse(x+5, y+10, 5, 5);
 ellipse(x+15, y+10, 5, 5);

 void go() {
 x+= speed;
 if (x>width && speed>0) {
 x = -20;
 if (x<0 && speed < 0) {
 x = width+20;

For the game, I created road lanes and a  character that is able to move up, left and right. The principle is for the character to cross the lanes without crashing into a car. However, because I was not able to account for collision in my code, I allowed the character to cross the roads while avoiding cars and try to reach the other side within a given time limit (10seconds).

The number of cars on each lane is chosen randomly as well as the color and speed of each car. Time is displayed on the upper left corner of the screen and the player’s goal is to reach the “Home” area. I also inserted a start and end screen to simulate a real game.

The challenge for this game was creating a collision since I was not familiar with the concept of PVector yet. My aim is to develop this further now that I have the essential tools to do so and add more features such as obstacles and levels and perhaps a moving screen with more roads generated as it moves.