Description
For the midterm assignment, we were asked to make a video game using processing. I made one in which the person has to try crossing the road without getting hit by the car.
Process
I used basic shapes to create a scenario of a road on the screen. The roads are made out of the rectangles. The cars are made using the rectangle with curved edges and ellipses for wheels. Along with a sun in the corner.
So my game is quite simple. Using sprite sheet, I made a person move across the screen for the game. The cars come from the side in random speeds to try and hit the person. The speed increases with increase in every level.
There are three levels in total and are shown in the top-left corner. When the bar goes all the way up to green, it means that the player has all three lives. On getting hit by the car, a life is reduced and the player as to start the game all the way from the start of the road.
I have added sound to the game( which somehow does not come in the video below). The sounds add a a special affect to the game I believe. The start of the game shows instructions on how to play the game. Just follow the simple instructions and win the game.
The player to cross all three rounds, wins the game. Here’s the video for the game:
CODE:
PFont intro; PImage spritesheet; PImage background_image; PImage[][] sprites; int direction = 1; // 0 up int step = 0; int x; int y; int speed = 3; int level = 1; float car1x = 750; float hood1x = 720; float wheel1a = 825; float wheel1b = 775; float car2x = -150; float hood2x = -50; float wheel2a = -130; float wheel2b = -75; float speed1 = 50; float speed2= 50; int lives = 3; import processing.sound.*; SoundFile sound_cars; SoundFile sound_levelup; SoundFile sound_lost; SoundFile sound_win; SoundFile sound_crash; boolean instruct = true; void setup() { size(600,600); background_image = loadImage("im_game.jpg"); //the background image spritesheet = loadImage("man_walking.png"); //the man sprites = new PImage[4][9]; //putting spritesheet in an array int w = spritesheet.width/9; int h = spritesheet.height/4; for (int y=0; y < 4; y++) { for (int x=0; x< 9; x++) { sprites[y][x] = spritesheet.get(x*w, y*h, w, h); } } x = 300; // setting the original values y = 370; imageMode(CENTER); //introducing sound. sound_cars = new SoundFile(this, "sound_cars.wav"); sound_levelup = new SoundFile(this,"sound_levelup.wav"); sound_lost= new SoundFile(this,"game_lost.mp3"); sound_win = new SoundFile(this,"sound_win.wav"); sound_crash = new SoundFile(this,"sound_crash.mp3"); //playing the sound of cars if (level < 4 && lives > 0 ){ sound_cars.loop(); } } void draw() { //laying a base background(255,70); //displaying the image image(background_image,400,280); //showing the instructions instructions(); if (instruct == false){ lifeline(); ///showing the lifelines scene();//displaying the scene car1_dimension();//calling out the car1 dimensions car2_dimensions();//calling out the car2 dimensions //executing the game if (level == 1 && lives >0) { movecar1(); if (car1x < 0){ speed1 = random(5,10); car1x = 750; hood1x = 720; wheel1a = 825; wheel1b = 775; } movecar2(); if (car2x > 600){ speed2 = random(5,10); car2x = -150; hood2x = -50; wheel2a = -130; wheel2b = -75; } //Determining the direction of the man moveman(); image(sprites[direction][step], x, y); //printing the image crash();//calling out the crash function } if (level == 2 && lives >0 ){ car1_dimension(); movecar1(); if (car1x < 0){ speed1 = random(10,13);//speed upgrades with every level car1x = 750; hood1x = 720; wheel1a = 825; wheel1b = 775; } car2_dimensions(); movecar2(); if (car2x > 600){ speed2 = random(10,13); car2x = -150; hood2x = -50; wheel2a = -130; wheel2b = -75; } //Determining the direction of the man moveman(); image(sprites[direction][step], x, y); //printing the image crash(); } if (level == 3 && lives >0){ car1_dimension(); movecar1(); if (car1x < 0){ speed1 = random(13,17); car1x = 750; hood1x = 720; wheel1a = 825; wheel1b = 775; } car2_dimensions(); movecar2(); if (car2x > 600){ speed2 = random(13,17); car2x = -150; hood2x = -50; wheel2a = -130; wheel2b = -75; } //Determining the direction of the man moveman(); image(sprites[direction][step], x, y); //printing the image crash(); } //level up settings if (y>600){ level+=1; sound_levelup.play(); //level up sound car1x = 750; hood1x = 720; wheel1a = 825; wheel1b = 775; car2x = -150; hood2x = -50; wheel2a = -130; wheel2b = -75; speed1 = 20; speed2= 20; x= 300; y= 370; } if (y<370){ y=370; } if (x<0){ x=0; } if (x>600){ x=600; } if (y>600){ sound_levelup.play(); } if (instruct == false){ leveltext(); // to display level only on starting the game } } } //function to indicate lives left void lifeline(){ if (lives == 3){ //shows all three boxes fill(#F21D1D); rect(10,10,50,10); fill(#FACC12); rect(55,10,95,10); fill(#62E32C); rect(100,10,140,10); } if (lives ==2){ //deleted the green box showing two lives left fill(#F21D1D); rect(10,10,50,10); fill(#FACC12); rect(55,10,95,10); } if (lives == 1){ //deletes the orange box showing one life left fill(#F21D1D); rect(10,10,50,10); } } void scene(){ //background stroke(255,69,0); strokeWeight (3); stroke (1); strokeWeight (1); //road fill(100); rect(0,400,600,200); //road lines fill(255,255,0); rect(0,475,600,8); rect(0,517,600,8); //sun arc(600,0,200,200,radians(90),radians(180),PIE); } void car1_dimension(){ fill(0,191,255); rect(car1x,405,100,50,80,30,0,0); rect(hood1x,430,30,25,40,0,0,0); fill(1); ellipse(wheel1a,450,25,25); ellipse(wheel1b,450,25,25); } void car2_dimensions(){ fill(255,20,20); rect(car2x,535,100,50,30,80,0,0); rect(hood2x,560,30,25,0,40,0,0); fill(1); ellipse(wheel2a,580,25,25); ellipse(wheel2b,580,25,25); } void movecar1(){ //subtracting speed to make the car move left car1x -= speed1; hood1x -= speed1; wheel1a -= speed1; wheel1b -= speed1; } void movecar2(){ //adding speed to make the car move right car2x += speed2; hood2x += speed2; wheel2a += speed2; wheel2b += speed2; } //function to make the man move void moveman(){ if (keyPressed) { if (keyCode == DOWN) { //to move down direction = 2; y +=speed*0.6; } if (keyCode == LEFT) { //to move left direction = 1; x -=speed*0.6; } if (keyCode == RIGHT) { //to move right direction = 3; x +=speed*0.6; } if (keyCode == UP) { //to move up direction = 0; y -=speed*0.6; } if (frameCount%speed==0) { //repeating the spritesheet step = (step+1) % 9; } } } //function to detect if the man crashes with the car void crash(){ //calculating distance between man and the car float distancehood1 = dist(hood1x,430,x,y); float distancehood2 = dist(hood2x,560,x,y); float distancecar1 = dist(car1x,405,x,y); float distancecar2 = dist(car2x,535,x,y); float distancebody1 = dist(car1x + 30,405,x,y); float distancebody2 = dist(car2x + 30,405,x,y); //detecting a crash if (distancehood1 < 15){ y = 370;// restarting man's position to the start of the road lives -=1;// reducing lives sound_crash.play(); //playing sound } if (distancehood2 < 15){ y = 370;// restarting man's position to the start of the road lives-=1; // reducing lives sound_crash.play(); //playing sound } if (distancecar1 < 15){ y = 370; // restarting man's position to the start of the road lives -=1; // reducing lives sound_crash.play(); //playing sound } if (distancecar2 < 15){ y = 370; // restarting man's position to the start of the road lives -=1; // reducing lives sound_crash.play(); //playing sound } if (distancebody1 < 15){ y = 370; // restarting man's position to the start of the road lives -=1; // reducing lives sound_crash.play(); //playing sound } if (distancebody2 < 15){ y = 370; // restarting man's position to the start of the road lives-=1; // reducing lives sound_crash.play(); //playing sound } } //displaying level of the game void leveltext(){ if (level == 4 && lives >0){ background(0); textSize(50); fill(255); text("Congratulations! \n You Win!!",150,200); sound_cars.stop(); } if (level == 1 && lives >0 || level == 2 && lives >0 || level == 3 && lives >0){ textSize(100); fill(#1AD628); textAlign(CENTER); text(level,278,100); } if (lives == 0){ background(0); textSize(60); fill(255,0,0); text("You Lost! \n Game Over:(", 300,200); textSize(20); text("Press SHIFT to play again",300,500); sound_cars.stop(); //restarting the game if (keyPressed){ if (keyCode== SHIFT){ sound_lost.stop(); car1x = 750; hood1x = 720; wheel1a = 825; wheel1b = 775; car2x = -150; hood2x = -50; wheel2a = -130; wheel2b = -75; x= 300; y= 370; lives = 3; level = 1; image(background_image,400,280); scene(); sound_cars.loop(); } } } } //displaying instructions of the game void instructions(){ if (instruct == true){ background(0); fill(#22F0E0); textSize(20); text("Make the man cross the road without getting hit by a car",100,100); text(" ↑ : to move up", 100,125); text("↓: to move down", 100,150); text(" ←: to move left",100,175); text(" → : to move right",100,200); text("Press Shift to make the instructions dissappear", 100,225); textSize(50); intro = createFont("STFangsong",32); textFont(intro); fill(0,0,random(150,255)); text("DON'T CRASH!",150,500); } //to remove instructions and start the game. if (keyPressed){ if (keyCode == SHIFT){ instruct = false; } } }