Week 3: Game Assignment using OOP



This project consisted of creating a game using Object Oriented Programming.



For this assignment, I first wanted to create an artwork with OOP. However, I later changed my mind to create a game as I thought it would be better for me to understand the concept of OOP. I liked the idea of using cars in the assignment so I decided to recreate a game I had seen before. The game consisted of a car on a road with the objective of avoiding being hit by an obstacle.



I had a lot of challenges with this assignment. First, it was very challenging for me to come up with an idea to make the obstacles appear repeatedly. I tried several ways by investigating how an array in Java worked. In the end, I decided to use one obstacle and make it repeatedly appear on the screen. The next challenge was that I had trouble coming up with a way to check if the car was colliding with an obstacle. This was hard to do because I was unaware of the distance function. After the class I had on Monday, I was able to use the dist() function to accomplish what I wanted to do. I also had a big challenge trying to figure out how to use the keys to accomplish the movement of the car. Finally, the last challenge I had was finding a way to make the scene move. The yellow marks in the road make an effect to make it look as if the car is moving. However, calculating the timing and distance of this took me a long time. 



For this project, I first designed the car with basic shapes so that I could recreate it on Processing using the shape functions. Then I tried to plan what kind of classes I needed and the attributes and methods linked to the class. Then I tried to finish all the methods and make sure they were working properly. At the end, I decided to add a background to make the game seem more interesting. However, I realized that the scene was not really engaging and it looked like the car was not moving. Therefore I decided to implement a way to make the scene more active by creating the road with yellow marks. 



Overall I think I failed to manage my time on this project and I underestimated the difficulty in working with Object-Oriented Programming. As the code gets longer, it gets harder to fix errors that appear. I had a hard time fixing various errors because I didn’t know what they meant by such errors. On the other hand, I think I worked well with the attributes part and avoiding hardcode. Although I hardcoded some numbers throughout this assignment, I think I have improved a lot on this part of the class. Although I am not very proud of the outcome in terms of design, I think I am satisfied with the functionality of the methods. The methods worked exactly as I expected initially.


Car car;
Obstacle obs;
Environment env;

void setup(){
  car = new Car(color(random(255),random(255), random(255)));
  obs = new Obstacle();
  env = new Environment();

void draw(){
  fill(0,255,0); //Green Parts
  rect(0, 0, width, height);
  fill(220); //Gray parts
  rect(100, 0, 50, height);
  rect(300, 0, 50, height);
  rect(500, 0, 50, height);
  float distance = dist(car.posX, car.posY, obs.posX, obs.posY);
    if ( distance <= car.carHeight){

void keyPressed(){
  if(keyCode == RIGHT){
    if(car.roadpos < 1){
  if(keyCode == LEFT){
    if(car.roadpos >= 0){
class Car {
  float posX, posY, roadpos;
  float velocity; 
  float direction;
  color carcolor;
  float carWidth, carHeight;
  float wheelWidth, wheelHeight;
  float positioncarroad;
  float live; //Counter for the lives remaining

  Car(color c) {
    posX = width/2;
    posY = height/2 + height/3;
    roadpos = 0;
    carWidth = 20;
    carHeight = 35;
    wheelWidth = 5;
    wheelHeight = 10;
    carcolor = c;
    live = 3;
  void drawCar() {
    rect(posX, posY-carHeight*0.5, carWidth*1.4, carHeight*0.05); //Wheel Supporter
    rect(posX, posY+carHeight*0.6, carWidth*1.4, carHeight*0.05); //Wheel Supporter
    rect(posX-carWidth*1.5, posY-carHeight*0.5, wheelWidth, wheelHeight);
    rect(posX+carWidth*1.5, posY-carHeight*0.5, wheelWidth, wheelHeight);
    rect(posX-carWidth*1.5, posY+carHeight*0.6, wheelWidth, wheelHeight);
    rect(posX+carWidth*1.5, posY+carHeight*0.6, wheelWidth, wheelHeight);
    //Car Body
    rect(posX, posY, carWidth, carHeight); //Main big rectangle of car
    rect(posX, posY, carWidth*0.4, carHeight*0.4); //Window inside big rectangle
    rect(posX, posY+carHeight, carWidth, carHeight*0.2); // Back of the car
    rect(posX, posY+carHeight*1.2, carWidth*1.5, carHeight*0.2); //back of the Car
    triangle(posX-carWidth, posY-carHeight, posX+carWidth, posY-carHeight, posX, posY-carHeight*1.7); //front triangle of the car
    rect(posX, posY-carHeight*1.35, carWidth*1.4, carHeight*0.15); //Front Rectangle of the Car
  void changetoleft(){ //Changing to left with keyboard
    posX -= width*0.33;
  void changetoright(){ //Changing to right with keyboard
    posX += width*0.33;
  void advanceposition(){ //Record the position of the car
    positioncarroad += 1;
  void gameloss(){
    if(live == 0){
      rect(0, 0, width, height);
      text("Game Over", width/2, height/2);
class Environment {
  float posY1 = 0;
  float posY2 = 200;
  float posY3 = 400;
  void movinglines(){ //Drawing the lines
    fill(255, 239, 0);
    rect(100, posY1, 10, 50);
    rect(300, posY1, 10, 50);
    rect(500, posY1, 10, 50);
    rect(100, posY2, 10, 50);
    rect(300, posY2, 10, 50);
    rect(500, posY2, 10, 50);
    rect(100, posY3, 10, 50);
    rect(300, posY3, 10, 50);
    rect(500, posY3, 10, 50);
  void movement(){ //Making the lines move
    posY1 += 5;
    posY2 += 5;
    posY3 += 5;
  void drawenv(){
    if(posY1 > 600){
      posY1 = 0;
    if(posY2 > 600){
      posY2 = 0;
    if(posY3 > 600){
      posY3 = 0;

class Obstacle{
  float posX, posY;
  float velocity;
  float direction;
  float obsWidth, obsHeight;
  color obscolor;
  Obstacle() {
    posX = (width/2)-(width*0.3)-20+int(random(0, 3))*(width/3);
    posY = 0;
    velocity = 5;
    obsWidth = 20;
    obsHeight = 20;
    obscolor = color(255, 0, 0); 
  void velocityobs(){ //Update the object Y position
    posY += velocity;
  void obsdraw(){
    rect(posX, posY, obsWidth, obsHeight);
    fill(255, 0, 0);
    circle(posX, posY, 30);
    if(posY > height){
  void obsnew(){
    posY = 0;
    posX = (width/2)-(width*0.3)-20+int(random(0, 3))*(width/3);  


One thought on “Week 3: Game Assignment using OOP”

  1. Good job on implementing the different classes Rock. And nice design with the car. Glad you figured out the collision detection. I think it would’ve been great to have a score illustrated on screen, to show how many lives are left. There is no indication to the user that something happens when they hit an obstacle. Seeing the lives go down, plus perhaps the car changing the color red or something, would be enough of a clue to the user for them to understand the stakes of the game.

Leave a Reply