Creating 2D Obstacle-Dodging Game

Hi,
I am taking an Object-Oriented Programming class, and our final project can be anything we want. I decided to create a game which has a car traveling around a track, with a random set of objects appearing on the track, and the user has to switch lanes in order to dodge the random objects. My idea was to have the objects re-randomize after every lap, and so the User gets a new challenge each lap.
This is my code so far. I have the track, with the car looping around it, and the arrow keys are used to change lanes. My problem is that I really don’t know where to begin in having objects, even just regular rectangles, randomly appear, but also have them change positions every lap. Any ideas or help on how you think I could go about this would be awesome.
Bean

raceCar rc;
track tk;

void setup() {
  size(400, 400);
  rc = new raceCar();
  tk = new track();
}

void draw() {
  background(0, 200, 200);

  tk.drawTrack();
  tk.lapLine();
  rc.onTrack();
  rc.carDisplay();
  rc.laneSpeeds();
  rc.lapCounter();
  rc.titleScreen();
    rc.startCar();
    rectMode(CENTER);
    rect(50,90,10,10);

}

void keyPressed() {
  rc.switchinLanes(); 
  rc.exitTitleScreen();

}

class raceCar {
  color c;
  float xpos, ypos, off=7;
  float r=-90; //starting degrees of car's rotation around center of window
  int togetherx= -15;//keeps the car together; used to switch lanes
  int togethery=-15; //pointless for now
  float speed=1;
  int lapCount;

  boolean startDriving=false;
  boolean titleScreen=true;
  boolean paused=false;

  raceCar() {
    c = color(200, 155, 25);
    xpos = width/2;
    ypos = 15;
  }

  void onTrack() {
    pushMatrix();
    translate(width/2, height/2); //changes car's reference point from top left of window (0,0) to middle of window.
    rotate(radians(r));
  }


  void carDisplay() {
    noStroke();
    rectMode(CENTER);
    //body of car
    fill(c);
    rect(xpos+togetherx, ypos+togethery, 18, 12);
    fill(0);

    //wheels
    fill(0);
    rect(xpos-off+togetherx, ypos-off+togethery, off, off/2);
    rect(xpos+off+togetherx, ypos-off+togethery, off, off/2);
    rect(xpos-off+togetherx, ypos+off+togethery, off, off/2);
    rect(xpos+off+togetherx, ypos+off+togethery, off, off/2);
    popMatrix();

    if (startDriving==true) {
      r+=speed;
    }
  }

  void switchinLanes() { //allows user to switch lanes using arrow keys; also creates barriers so user cannot leave track.
    if (keyCode==DOWN&&togetherx>-85) { //move closer to center of track
      togetherx-=35;
      // println(togetherx);
    }
    if (keyCode==UP&&togetherx<-15) { //move closer to outside of track
      togetherx+=35;
      //println(togetherx);
    }
  }

  void laneSpeeds() { // makes outside lane faster than inside lanes
    float lane1Speed=1.642; //speeds adjusted so that each lap takes about 3.82 seconds to complete, regardless of lane.
    float lane2Speed=1.646;
    float lane3Speed=1.649;


    if (togetherx==-85) { //if car is in inner most lane
      speed=lane1Speed;
    }
    if (togetherx==-50) { //if car is in middle lane
      speed=lane2Speed;
    }

    if (togetherx==-15) {
      speed=lane3Speed;
    }
  }

  void lapCounter() {
    if (startDriving==true) {
      arcadeFont();
      fill(0);
      textAlign(CENTER); 
      textSize(25);
      text("Score" +"\n" + lapCount + "  Laps", width/2, height/2);
    }
    if (r>=270) {
      lapCount+=1;
      //int timer = millis();
      //println("drove " + lapCount + " laps");
      //print(timer);
      r=-90;
    }
  }

  void startCar() {
    if (startDriving==false&&titleScreen==false) {
      arcadeFont();
      fill(0);
      textAlign(CENTER);
      textSize(25);
      text("Press Space" + " \n" + "to Begin!", width/2, height/2);
    }
    if (key==' ') {
      startDriving=true;
    }
  }
  void obstacleArrays() {
    rect(0, 0, 100, 100);
  }

  void titleScreen() {
    if (titleScreen==true) {
      rectMode(CORNER);
      fill(136, 32, 240);
      rect(0, 0, width, height);

      rc.arcadeFont();
      fill(0);
      textAlign(CENTER);
      textSize(40);
      text("Welcome to" + "\n" + "DRIFT KING", width/2, height/4);
      text("Press Enter", width/2, height/1.2);
    }
  }

  void exitTitleScreen() {
    if (keyCode==ENTER) {
      titleScreen=false;
    }
  }
  


  void arcadeFont() {
    PFont arcadeFont;
    arcadeFont = createFont("ARCADECLASSIC.TTF", 20);
    textFont(arcadeFont);
  }
}


class track {

  track() {
  }

  void drawTrack() {

    stroke(255);
    strokeWeight(3);

    //track with decreasing sizes of ellipses
    fill(220);
    ellipse(width/2, height/2, width, height);
    ellipse(width/2, height/2, width/1.2, height/1.2);
    ellipse(width/2, height/2, width/1.5, height/1.5);

    //middle circle with  score + health
    fill(255, 0, 0); 
    ellipse(width/2, height/2, width/2, height/2);
  }
  void lapLine() {
    stroke(255, 0, 0);
    line(width/2, 0, width/2, (height-height/1.5)-35);
  }


}
1 Like

please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.


your question:

from the code you posted above, we see you know class already.

so the obstacles would need a class Obstacle with position size color or more memory…

  • and draw and
  • randomize position

functions.

and make a array or arrayList
obstacles of class Obstacle
draw it by a FOR loop of its own draw function from inside draw()
and randomize it on newgame?

3 Likes