Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//player related variables
float playerX; 
float playerY; 
float playerH;
float playerW;
float playerOpacity;


boolean invincible = false;
float hitTime;
int playerLives = 3;
float speed; 

float road1 = 1;

//menu related variables
boolean gameStart = false;
int menuTime;

boolean isSpawned = false;


float carX;
float carY;
float carH = 55;
float carW = 28;

float carX2;
float carY2;
float carH2 = 50;
float carW2 = 25;
//car spawn locations
float carSpawnX1 = 287;
float carY1= 402;
float carSpawnX2 = 237;
float carSpawnX3 = 187;
float carSpawnX4 = 137;
float carSpawnX5= 87;

int score;
int highScore = 0;
float preSpawn = 0;
float carSpawn;  

void setup () {
  size (400, 400); 
  frameRate = 30;
  score = 0; 
  playerX = 187;
  playerY = 320;
  playerH = 50;
  playerW= 26;
  speed = 10;
}

void draw () {
  noStroke();

  //Upate
  updatePlayer ();
  updateScore();
  speed = score * 0.0001;
  //Draw
  drawTerrain();

  drawCar1();
  drawCar2();
  drawPlayer();
  drawScore();

  if (gameStart == false ) {
    gameMenu();
  }
}

void gameMenu () {
  fill(255);
  textSize (60);
  text ( "Desert Racer", 20, 150);
  fill ( 255, 255, 255, 125 * sin(0.3 * frameCount) + 125);
  textSize(20);

  menuTime = millis();
  text ("Click to start", 150, 200);
  if (mousePressed ==true ) {
    gameStart = true;
  }
}

void updateScore() {
  if ( gameStart) {
    score = (millis() - menuTime)/2;
  }
}

void drawScore() {
  fill(255, 0, 0);
  textSize ( 13);
  text ( "score " + score, 10, 10);

  text ("Highscore "  +highScore, 250, 10);
}

void drawTerrain() {
  background (180, 125, 70);
  fill (100, 100, 100);
  rect ( 75, 0, 250, 400);

  stroke ( 200, 200, 50 ) ;
  strokeWeight(3);


  //road line 1 
  road1 = road1 + 2 + (1 *speed);
  if (road1 >= 400) {
    road1 = 1;
  }
  for ( int i = 1; i < 21; i++) {
    float f =  -400 + (i * 20);
    line ( 125, road1 + f, 125, road1 + f + 10);
  }
  for ( int i = 1; i < 20; i++) {
    float f = (i * 20);
    line ( 125, road1 + f, 125, road1 + f + 10);
  }

  //road line 2
  if (road1 >= 400) {
    road1 = 1;
  }
  for ( int i = 1; i < 21; i++) {
    float f =  -400 + (i * 20);
    line ( 175, road1 + f, 175, road1 + f + 10);
  }
  for ( int i = 1; i < 20; i++) {
    float f = (i * 20);
    line ( 175, road1 + f, 175, road1 + f + 10);
  }
  //road line 3
  if (road1 >= 400) {
    road1 = 1;
  }
  for ( int i = 1; i < 21; i++) {
    float f =  -400 + (i * 20);
    line ( 225, road1 + f, 225, road1 + f + 10);
  }
  for ( int i = 1; i < 20; i++) {
    float f = (i * 20);
    line ( 225, road1 + f, 225, road1 + f + 10);
  }
  //road line 3
  if (road1 >= 400) {
    road1 = 1;
  }
  for ( int i = 1; i < 21; i++) {
    float f =  -400 + (i * 20);
    line ( 275, road1 + f, 275, road1 + f + 10);
  }
  for ( int i = 1; i < 20; i++) {
    float f = (i * 20);
    line ( 275, road1 + f, 275, road1 + f + 10);
  }

  stroke(255);  
  strokeWeight(2);
  line (80, 0, 80, 400);
  line ( 320, 0, 320, 400);
  noStroke();
}
void updatePlayer () {
  if ( mouseX > playerX + 30) {
    playerX = playerX +( 0.005 * (mouseX - playerX));
  }

  if ( mouseX < playerX -5) {
    playerX = playerX - (0.005 * (playerX - mouseX));
  }

  if ( mousePressed && mouseButton ==  LEFT) {
    playerY = playerY - 1;
  }

  if (mousePressed && mouseButton == RIGHT) {
    playerY = playerY + 1;
  }

  if (invincible == true && millis() - hitTime > 3000) {
    invincible = false;
  }
}

void hit() {
  println ("hit");


  //set play to be invincible when hit
  if (invincible == false ) {
    if ( playerLives == 1 ) {
      reset();
    } else {
      invincible = true;
      speed = 1;
      playerLives = playerLives - 1;
      hitTime = millis();
    }
  }
}

void reset () {
  highScore = score;
  score = 0;
  gameStart = false;
  playerLives = 3;
}


void drawPlayer () {
  noStroke();

  if ( invincible == true && gameStart) {
    playerOpacity = 127 * sin ( 20 * frameCount )+ 127;
  } else {
    playerOpacity = 255;
  }
  //tires
  fill ( 60, 60, 60, playerOpacity);
  rect ( playerX - 2, playerY + 8, 3, 12);
  rect ( playerX - 2, playerY + 36, 3, 12);
  rect ( playerX + 25, playerY + 8, 3, 12);
  rect (playerX + 25, playerY + 36, 3, 12);
  fill (130, 0, 0, playerOpacity );
  //body
  rect (playerX, playerY, playerW, playerH);
  fill (100, 0, 0, playerOpacity);
  rect (playerX, playerY + 45, 26, 5);
  fill (130, 0, 0, playerOpacity);
  rect ( playerX, playerY + 42, 26, 3);
  rect ( playerX + 4, playerY + 18, 18, 12);
  fill (120, 0, 0, playerOpacity);
  quad ( playerX, playerY +14, playerX, playerY +42, playerX + 4, playerY + 30, playerX + 4, playerY + 18);
  quad ( playerX + 26, playerY + 14, playerX + 26, playerY + 42, playerX + 22, playerY + 30, playerX + 22, playerY + 18);
  //windows
  fill (145, 190, 240, playerOpacity);
  quad ( playerX+1, playerY + 14, playerX + 4, playerY + 18, playerX + 22, playerY + 18, playerX + 25, playerY + 14);
  quad ( playerX+1, playerY + 42, playerX + 25, playerY + 42, playerX + 22, playerY + 32, playerX + 4, playerY + 32);

  //lights
  //triangle ( playerX +8, playerY + 10, playerX -15, playerY - 60, playerX + 31, playerY - 60);
}

//determine car spawn location
void spawnCar () {

  //place car in random lane
  float r = random(1, 5);
  int f = round(r);
  if ( f == 1) {
    carSpawn = carSpawnX1;
  } else if (f==2) {
    carSpawn= carSpawnX2;
  } else if (f == 3) {
    carSpawn = carSpawnX3;
  } else if ( f == 4) {
    carSpawn = carSpawnX4;
  } else if ( f == 5) {
    carSpawn = carSpawnX5;
  }
  // stop cars from spawning in the same lane
  if ( preSpawn == carSpawn) {
    spawnCar();
  }
  preSpawn = carSpawn;
}

void drawCar1() {


  if (rectRect (carX, carY1, carW, carW, playerX, playerY, playerW, playerH) == true && invincible == false && gameStart) {
    hit();
  }

  if (isSpawned == false ) {
    carX = 87;
    isSpawned = true;
  }

  carY1 = carY1 + (1 *speed);

  //fill(120);
  // rect ( carX + 2 , carY1-10, 24, 10);

  fill(240);
  //float carPosX = carX1;
  //stroke(0);
  rect ( carX, carY1, 28, 45);
  fill(200);
  rect (carX, carY1 + 30, 28, 25);

  fill ( 200, 0, 0 );
  rect ( carX, carY1 + 45, 28, 3);

  fill(60);
  rect ( carX + 5, carY1 + 35, 18, 20);

  fill(80) ; 
  rect ( carX -1, carY1 + 54, 30, 3);
  fill (100);
  rect (carX - 1, carY1 + 53, 30, 1);

  if (carY1 >= 400) {
    spawnCar (); 
    carX = carSpawn;
    carY1 = -20;
  }
}

void drawCar2() {

  carY2 = carY2 + 1;


  if (carY2 >= 400) {
    spawnCar();
    carX2 = carSpawn;

    carY2 = -60;
  }

//tires 

  fill (60);
  rect ( carX2 - 2, carY2  +10 , 3, 10);
  rect ( carX2 + 25, carY2 +10 , 2 , 10);
  rect ( carX2 - 2, carY2 + 37, 2 , 10);
  rect ( carX2 + 25, carY2 + 37, 2 ,10);
//body
  fill ( 0, 100, 0);
  rect ( carX2, carY2, 25, 50);
  
  fill (0, 115, 0);
  rect (carX2, carY2 + 10, 25, 15 );

  fill ( 0, 60, 0 );
  rect( carX2 + 3, carY2 + 34, 19, 8);

  fill ( 0, 80, 0 ) ;
  rect ( carX2, carY2 + 43, 25, 7);
  rect ( carX2, carY2 + 25, 25, 7);
  rect ( carX2 + 3, carY2 + 32, 19, 2);
}


//collision system from http://www.jeffreythompson.org/collision-detection/rect-rect.php
boolean rectRect(float r1x, float r1y, float r1w, float r1h, float r2x, float r2y, float r2w, float r2h) {

  // are the sides of one rectangle touching the other?

  if (r1x + r1w >= r2x &&    // r1 right edge past r2 left
    r1x <= r2x + r2w &&    // r1 left edge past r2 right
    r1y + r1h >= r2y &&    // r1 top edge past r2 bottom
    r1y <= r2y + r2h) {    // r1 bottom edge past r2 top
    return true;
  }
  return false;
}