Final Project Ideas — Ainsley

April 21 Project Ideas

Project 1

Project 2

Project 3

April 25 Status and Plan

Committed

Stretch

April 28 Status

Completed

Opening screen:

Opening Screen

Can’t wait to play!

Fonts Needed

Add this font file and this font file to the project.

Implementation

final float DELTA_T = .5;
ArrayList<String> heldKeys = new ArrayList<String>();
Car player;
Car player2;
Track track;
PFont font;
PFont font2;
boolean gameStart;
// The font must be located in the sketch's
// "data" directory to load successfully

void setup() {
  frameRate(60);
  size(1300, 650);
  background(205, 189, 206);
  font = createFont("ArcadeClassic.otf", 128);
  font2 = createFont("ka1.ttf", 128);
  track = new Track();
  player = new Car(track.startingPlace().add(30, 0), color(105, 64, 115), 1);
  player2 = new Car(track.startingPlace(), color(89, 111, 125), 2);
}

void draw() {
  background(205, 189, 206);
  noFill();
  strokeWeight(7);
  stroke(0);
  rectMode(RIGHT);
  rect(0, 0, width, height);
  rect(10, 10, width -10, height -10);
    rectMode(CENTER);
  if (player2.winTheGame(player) == false && player.winTheGame(player2) == false) {
    textforscreen();
  }
  if (gameStart == true && player2.winTheGame(player) == false && player.winTheGame(player2) == false ) {
    track.display();
    drive();
    player.update();
    player.checkOtherCar(player2);
    player.display();
    player2.update();
    player2.checkOtherCar(player);
    player2.display();
    player.winTheGame(player2);
    player2.winTheGame(player);
  }
  if (player2.winTheGame(player) == true) {
    player.display();
    player2.display();
    textFont(font2);
    textSize(86);
    textAlign(CENTER);
    stroke(255, 0, 0);
    fill(0);
    for (int i = 10; i > 0; i --) {
      textSize(i *8);
      text("Player 2 wins!!!", width/2 + (10-i), 100 + (70 * (10-i)));
    }
    textFont(font);
    textSize(40);
    textAlign(CENTER);
    text("Restart", 120, height-40);
    rectMode(CORNER);
    noFill();
    stroke(255, 0, 0);
    rect(30, height-75, 170, 45);
  }
  if (player.winTheGame(player2) == true) {
    player.display();
    player2.display();
    textFont(font2);
    textAlign(CENTER);
    stroke(255, 0, 255);
    fill(0);
    for (int i = 10; i > 0; i --) {
      textSize(i *8);
      text("Player 1 wins!!!", width/2 + (10-i), 100 + (70 * (10-i)));
    }
    textFont(font);
    textSize(40);
    textAlign(CENTER);
    text("Restart", 120, height-40);
    rectMode(CORNER);
    noFill();
    stroke(255, 0, 0);
    rect(30, height-75, 170, 45);
  }
}

void mousePressed() {
  if (mouseX > width/2 - 160 && mouseX < width/2 + 160 && mouseY < 525 && mouseY > 475) {
    gameStart = true;
  } else if (mouseX >30 && mouseX < 200 && mouseY >height-75 && mouseY < height-30) {
    restart();
  }
}

void restart() {
  gameStart = false;
  player.laps = 0;
  player.lapcheck1 = 0;
  player.lapcheck2 = 0;
  player.lapcheck3 = 0;
  player2.laps = 0;
  player2.lapcheck1 = 0;
  player2.lapcheck2 = 0;
  player2.lapcheck3 = 0;
  player.velocity.set(0, 0);
  player2.velocity.set(0, 0);
  player.direction = 0;
  player2.direction = 0;
  player.location = track.startingPlace().add(30, 0);
  player2.location = track.startingPlace();
}

class Car {

  PVector location;
  PVector velocity;
  PVector acceleration;
  float radius;
  float direction;
  float topSpeed;
  float handling;
  float sensitivity;
  color myColor;
  int id;
  float h;
  float w;
  int x0;
  int y0;
  int laps = 0;
  int lapcheck1 = 0;
  int lapcheck2 = 0;
  int lapcheck3 = 0;
  boolean winTheGame;

  Car(PVector start, color myColor_, int id_) {
    location = start.copy();
    velocity = new PVector(0, 0);
    direction = 0;
    topSpeed = 5;
    myColor = myColor_;
    id = id_;
    sensitivity = 8.0;
    h = 30;
    w = 15;
  }
  void update() {
    PVector oldPos = location.copy();
    location.x = location.x + (velocity.x * sin(radians(direction))) * DELTA_T;
    location.y = location.y + (velocity.y * cos(radians(direction))) * DELTA_T;
    checkEdges();
    for (int i = 0; i < track.obstacles.size(); i++) {
      Obstacle thisOne = track.obstacles.get(i);
      crash(thisOne);
    }
    if (oldPos.y > height/2 && location.y < height/2
      && location.x > width-125 - 60 && location.x < width-65 + 30) {
      lapcheck1++;
      println("lacpCheck1 " + lapcheck1);
    }
    if (oldPos.x < width/2 && location.x > width/2
      && location.y > height-80 && location.y < height-20) {
      lapcheck2++;
      println("lacpCheck2" + lapcheck2);
    }
    if (oldPos.x > width/2 && location.x < width/2
      && location.y > 20 && location.y < 80) {
      lapcheck3++;
      println("lacpCheck3" + lapcheck3);
    }
    if (oldPos.y < height/2 && location.y > height/2
      && location.x > track.start.x -60 && location.x < track.start.x +90
      && lapcheck1 == laps +1
      && lapcheck2 == laps +1
      && lapcheck3 == laps +1) {
      laps++;
    } else if (oldPos.y < height/2 && location.y > height/2
      && location.x > track.start.x -60 && location.x < track.start.x +90) {
      lapcheck1 = laps;
      lapcheck2 = laps;
      lapcheck3 = laps;
    }
  }

  color returnMyColor() {
    return myColor;
  }

  void checkEdges() {
    if (location.x < 0) {
      location.x = 1;
      velocity.set(0, 0);
    } else if (location.x > width) {
      location.x = width-1;
      velocity.set(0, 0);
    } else if (location.y < 0) {
      location.y = 1;
      velocity.set(0, 0);
    } else if (location.y > height) {
      location.y = height-1;
      velocity.set(0, 0);
    }
  }

  void accelerate() {
    if (velocity.x > topSpeed) {
      velocity.set(topSpeed/2, topSpeed/2);
    } else if  (velocity.y > topSpeed) {
      velocity.set(topSpeed/2, topSpeed/2);
    }
    velocity.add(.1, .1);
  }

  void turnRight() {
    direction = direction - (sensitivity / 3);
  }

  void turnLeft() {
    direction = direction + (sensitivity / 3);
  }

  void brake() {
    velocity.sub(sensitivity/10, sensitivity/10);
    if (velocity.x < -1 | velocity.y < -1) {
      velocity.set(-1, -1);
    }
  }

  void display() {
    pushMatrix();
    translate(location.x, location.y);
    rotate(radians(360)-radians(direction));
    rectMode(CENTER);
    noStroke();
    fill(myColor);
    rect(0, 0, w, h);
    triangle(0 -w/2, 0 + h/2, 0 +w/2, 0 + h/2, 0, h-2);
    ellipseMode(CENTER);
    fill(0);
    ellipse(0-w/2, 0-h/2, 3, 5);
    ellipse(0+w/2, 0+h/2, 3, 5);
    ellipse(0-w/2, 0+h/2, 3, 5);
    ellipse(0+w/2, 0-h/2, 3, 5);
    textSize(15);
    textAlign(CENTER);
    String idstring = "" + id;
    text(idstring, 0, 0);
    popMatrix();
  }

  void crash(Obstacle o1) {
    if (location.x + w/2 >= o1.location.x - o1.lengthobs/2 + 5&&
      location.x - w/2 <= o1.location.x + o1.lengthobs/2 - 5 &&
      location.y + h/2 >= o1.location.y - o1.heightobs/2 + 5 &&
      location.y - h/2 <= o1.location.y + o1.heightobs/2 -5) {
      location.x = location.x - (velocity.x * sin(radians(direction))) * DELTA_T;
      location.y = location.y - (velocity.y * cos(radians(direction))) * DELTA_T;
    }
  }

  void drawMiniCar(int x0_, int y0_) {
    int x0 = x0_;
    int y0 = y0_;
    rectMode(CENTER);
    noStroke();
    fill(myColor);
    rect(x0, y0, w, h);
    triangle(x0 -w/2, y0 + h/2, x0 +w/2, y0 + h/2, x0, y0 + h-2);
    ellipseMode(CENTER);
    fill(0);
    ellipse(x0-w/2, y0-h/2, 3, 5);
    ellipse(x0+w/2, y0+h/2, 3, 5);
    ellipse(x0-w/2, y0+h/2, 3, 5);
    ellipse(x0+w/2, y0-h/2, 3, 5);
    textSize(15);
    textAlign(CENTER);
    String idstring = "" + id;
    text(idstring, x0, y0);
  }

  boolean winTheGame(Car otherCar_) {
    if (laps == 3 && otherCar_.laps < 3) {
      return true;
    } else {
      return false;
    }
  }

  void checkOtherCar(Car otherCar_) {
    if (location.x + w/2 >= otherCar_.location.x - otherCar_.w/2 &&
      location.x - w/2 <= otherCar_.location.x + otherCar_.w/2 &&
      location.y + h/2>= otherCar_.location.y - otherCar_.h/2 &&
      location.y - h/2 <= otherCar_.location.y + otherCar_.h/2 ) {
      location.x = location.x - (velocity.x * sin(radians(direction))) * DELTA_T;
      location.y = location.y - (velocity.y * cos(radians(direction))) * DELTA_T;
    }
  }
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      if (!heldKeys.contains("UP")) {
        heldKeys.add("UP");
      }
    }
    if (keyCode == LEFT) {
      if (!heldKeys.contains("LEFT")) {
        heldKeys.add("LEFT");
      }
    }
    if (keyCode == RIGHT) {
      if (!heldKeys.contains("RIGHT")) {
        heldKeys.add("RIGHT");
      }
    }
    if (keyCode == DOWN) {
      if (!heldKeys.contains("DOWN")) {
        heldKeys.add("DOWN");
      }
    }
  }
  if (key == 'w') {
    if (! heldKeys.contains("w")) {
      heldKeys.add("w");
    }
  }
  if (key == 'a') {
    if (! heldKeys.contains("a")) {
      heldKeys.add("a");
    }
  }
  if (key == 's') {
    if (! heldKeys.contains("s")) {
      heldKeys.add("s");
    }
  }
  if (key == 'd') {
    if (! heldKeys.contains("d")) {
      heldKeys.add("d");
    }
  }
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) {
      heldKeys.remove("UP");
    }
    if (keyCode == LEFT) {
      heldKeys.remove("LEFT");
    }
    if (keyCode == RIGHT) {
      heldKeys.remove("RIGHT");
    }
    if (keyCode == DOWN) {
      heldKeys.remove("DOWN");
    }
  }
  if (key == 'w') {
    heldKeys.remove("w");
  }
  if (key == 'a') {
    heldKeys.remove("a");
  }

  if (key == 's') {
    heldKeys.remove("s");
  }

  if (key == 'd') {
    heldKeys.remove("d");
  }
}

void drive() {
  for (String s : heldKeys) {
    if (s == "UP") {
      player.accelerate();
    } else if (s == "LEFT") {
      player.turnLeft();
    } else if (s == "RIGHT") {
      player.turnRight();
    } else if (s == "DOWN") {
      player.brake();
    } else if (s == "w") {
      player2.accelerate();
    } else if (s == "a") {
      player2.turnLeft();
    } else if (s == "s") {
      player2.brake();
    } else if (s == "d") {
      player2.turnRight();
    }
  }
}

class Obstacle {
   PVector location;
  float lengthobs;
  float heightobs;
  color obsColor;

  Obstacle(PVector location_, float length_, float height_, color obsColor_) {
    location = location_;
    lengthobs = length_;
    obsColor = obsColor_;
    heightobs = height_;
  }

  void display() {
    stroke(0);
    strokeWeight(2);
    fill(obsColor);
    rect(location.x, location.y, lengthobs, heightobs);
  }
}

void textforscreen() {
  textFont(font2);
  textSize(86);
  textAlign(CENTER);
  stroke(255, 0, 0);
  fill(0);
  text("Racer Game", width/2, height/2 - 75);
  textFont(font);
  if (gameStart == false) {
    textSize(30);
    text("Click  Here  to  Start ", width/2, 500);
    rectMode(CORNER);
    noFill();
    stroke(255, 0, 0);
    rect(width/2 - 160, 472, 320, 40);
    textSize(40);
    text("First  person  to  three  laps  wins", width/2, 300);
    text("You  must  stay  on  the  track  for  your  laps  to  count", width/2, 325);
    text("Player 1", width/2 - 140, height/2 + 50);
    text("Player 2", width/2 + 100, height/2 + 50);
    player.drawMiniCar(width/2 - 40, height/2 + 50);
    player2.drawMiniCar(width/2 + 200, height/2 + 50);
    textSize(25);
    text("Controls", width/2 - 140, height/2 + 75);
    text("Controls", width/2 + 100, height/2 + 75);
    stroke(255);
    textSize(15);
    text("UP  Arrow", width/2 - 140, height/2 + 100);
    text("DOWN  Arrow", width/2 - 140, height/2 + 110);
    text("LEFT  Arrow", width/2 - 140, height/2 + 120);
    text("RIGHT  Arrow", width/2 - 140, height/2 + 130);
    text("W", width/2 + 100, height/2 + 100);
    text("A", width/2 + 100, height/2 + 110);
    text("S", width/2 + 100, height/2 + 120);
    text("D", width/2 + 100, height/2 + 130);
    //int timer = int(((900 - frameCount)-(900 / 60))/60);
    //textSize(30);
    //text("CountDown ", width/2, 500);
    //textSize(50);
    //text("" + timer, width/2, 550);
  } else if (gameStart == true && player2.winTheGame(player) == false && player.winTheGame(player2) == false ) {
    textSize(50);
    text("" + player.laps+ " of 3", width/2 - 140, height/2 + 120);
    text(""+ player2.laps + " of 3", width/2 + 100, height/2 + 120);
    textSize(40);
    text("Player 1", width/2 - 140, height/2 + 50);
    text("Laps", width/2 - 140, height/2 + 85);
    text("Player 2", width/2 + 100, height/2 + 50);
    text("Laps", width/2 + 100, height/2 + 85);
    text("Restart", 120, height-40);
    rectMode(CORNER);
    noFill();
    rect(30, height-75, 170, 45);
  } 
}

class Track {
  PVector start;
  int trackWidth;
  ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();

  Track() {
    start = new PVector(width - (width-200/2), height/2 + 5);
    trackWidth = 60;
    Obstacle obs1 = new Obstacle(new PVector (start.x - 35, start.y), 25, 25, color(200));
    obstacles.add(obs1);
    Obstacle obs2 = new Obstacle(new PVector (start.x - 60, start.y), 25, 25, color(100));
    obstacles.add(obs2);
    Obstacle obs3 = new Obstacle(new PVector (start.x - 85, start.y), 25, 25, color(200));
    obstacles.add(obs3);
    int offSetHere = trackWidth - 10;
    Obstacle obs4 = new Obstacle(new PVector (start.x + offSetHere, start.y), 25, 25, color(200));
    obstacles.add(obs4);
    Obstacle obs5 = new Obstacle(new PVector (start.x + offSetHere + 25, start.y), 25, 25, color(100));
    obstacles.add(obs5);
    Obstacle obs6 = new Obstacle(new PVector (start.x + offSetHere + 50, start.y), 25, 25, color(200));
    obstacles.add(obs6);
    Obstacle obs7 = new Obstacle(new PVector (width/2 - 200, 550), 20, 50, color(150));
    obstacles.add(obs7);
    Obstacle obs8 = new Obstacle(new PVector (width/2 + 200, 550), 20, 50, color(150));
    obstacles.add(obs8);
    Obstacle obs9 = new Obstacle(new PVector (width/2, 625), 20, 50, color(80));
    obstacles.add(obs9);
    Obstacle obs10 = new Obstacle(new PVector (width/2 - 400, 550), 20, 50, color(80));
    obstacles.add(obs10);
    Obstacle obs11 = new Obstacle(new PVector (width/2 + 400, 550), 20, 50, color(80));
    obstacles.add(obs11);
    Obstacle obs12 = new Obstacle(new PVector (width/2 - 450, 440), 20, 50, color(150));
    obstacles.add(obs12);
    Obstacle obs13 = new Obstacle(new PVector (width/2 + 450, 440), 20, 50, color(150));
    obstacles.add(obs13);
    Obstacle obs72 = new Obstacle(new PVector (width/2 - 200, height/2 - 225), 20, 50, color(150));
    obstacles.add(obs72);
    Obstacle obs82 = new Obstacle(new PVector (width/2 + 200, height/2 - 225), 20, 50, color(150));
    obstacles.add(obs82);
    Obstacle obs92 = new Obstacle(new PVector (width/2, 25), 20, 50, color(80));
    obstacles.add(obs92);
    Obstacle obs102 = new Obstacle(new PVector (width/2 - 400, height/2 - 225), 20, 50, color(80));
    obstacles.add(obs102);
    Obstacle obs112 = new Obstacle(new PVector (width/2 + 400, height/2 - 225), 20, 50, color(80));
    obstacles.add(obs112);
    Obstacle obs122 = new Obstacle(new PVector (width/2 - 450, height/2 - 115), 20, 50, color(150));
    obstacles.add(obs122);
    Obstacle obs132 = new Obstacle(new PVector (width/2 + 450, height/2 - 115), 20, 50, color(150));
    obstacles.add(obs132);
    Obstacle obs14 = new Obstacle(new PVector (80, height/2 + 50), 20, 50, color(80));
    obstacles.add(obs14);
    Obstacle obs15 = new Obstacle(new PVector (width - 90, height/2 + 90), 20, 50, color(80));
    obstacles.add(obs15);
    Obstacle obs142 = new Obstacle(new PVector (90, height/2 - 95), 20, 50, color(80));
    obstacles.add(obs142);
    Obstacle obs152 = new Obstacle(new PVector (width - 90, height/2 - 90), 20, 50, color(80));
    obstacles.add(obs152);
    Obstacle obs16 = new Obstacle(new PVector (width -130, height/2), 20, 50, color(150));
    obstacles.add(obs16);
  }

  void display() {
    noFill();
    stroke(119, 131, 233);
    strokeWeight(trackWidth);
    arc(width/2 + 7, height/2, width -200, height - 100, - PI / 2, 3 * PI / 2); // 180 degrees
    arc(width/2 + 7, height/2, width -200, height - 100, PI / 2, 3 * PI / 2); // 180 degrees
    strokeWeight(10);
    stroke(189, 108, 144);
    rectMode(CENTER);
    line(start.x - trackWidth/2 + 7, start.y, start.x + trackWidth /2 + 7, start.y);
    for (Obstacle obstacle : obstacles) {
      obstacle.display();
    }
  }

  PVector startingPlace() {
    PVector carStart = start.copy();
    carStart.sub(8, 35);
    return carStart;
  }
}