Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* Obstacle Course by Travis Pynn
Red walls are on the ground
Green walls are in the air
Yellow walls are tall, so they are both
You can avoid red walls by flying over them (holding mouse button)
You can avoid green walls by going under them (releasing mouse button)
You can only avoid yellow walls by going around them
You will slowly gain speed as your score increases
Try to get a high score! */


//initialize variables
float borderW, borderH;
float playerX, playerY, playerW, playerH;
float wallX1, wallY1, wallW1, wallH1, wallS1;
float wallX2, wallY2, wallW2, wallH2, wallS2;
float wallX3, wallY3, wallW3, wallH3, wallS3;
int score;
int playerColor;
int red, green, yellow;

//set size and variables
void setup() {
  //size and framerate
  size(400, 400);
  frameRate(60);
  smooth();

  //set the colors
  red = color(240, 0, 0);
  green = color(0, 240, 0);
  yellow = color(240, 240, 0);

  //dimensions for borders
  borderW = 50;
  borderH = height;

  //dimensions for the player
  playerX = mouseX;
  playerY = 350;
  playerW = 50;
  playerH = 50;

  //dimensions and speed for walls
  wallX1 = random(50, 350);
  wallY1 = wallH1/2;
  wallW1 = random(100, 200);
  wallH1 = 50;
  wallS1 = 3;

  wallX2 = random(50, 350);
  wallY2 = wallH2/2;
  wallW2 = random(100, 200);
  wallH2 = 50;
  wallS2 = 4;

  wallX3 = random(50, 350);
  wallY3 = wallH2/2;
  wallW3 = random(100, 200);
  wallH3 = 50;
  wallS3 = 2;

  //initial score
  score = 0;
}

void draw() {
  //Draw the repeating grass background
  for (int i = 25; i < 450; i += 50) {
    fill(30, 100, 0);
    strokeWeight(3);
    rect(i, 200, 50, 400);
  }
  
  //Keeps the player following the mouse
  playerX = mouseX;

//Everything else
  wall1();
  wall3();
  player();
  wall2();
}

void player() {
  //sets up player look
  rectMode(CENTER);
  fill(playerColor);
  strokeWeight(2);

  //draw player
  rect(playerX, playerY, playerW, playerH);

  //Controls "verticality" of the player
  if (mousePressed == true)
    playerColor = green;
  else
    playerColor = red;

  //collisions with walls
  if (playerY - playerH/2 < wallY1 + wallH1/2 && (playerY + playerH/2 > wallY1 + wallH1/2) && playerX < wallX1 + wallW1/2 && playerX > wallX1 - wallW1/2 && playerColor == red) {
    //println("hit red!");
    score = 0;
    wallS1 = 3;
    wallS2 = 4;
    wallS3 = 2;
  }
  if ((playerY - playerH/2 < wallY2 + wallH2/2) && (playerY + playerH/2 > wallY2 + wallH2/2) && (playerX < wallX2 + wallW2/2) && (playerX > wallX2 - wallW2/2) && (playerColor == green)) {
    //println("hit green!");
    score = 0;
    wallS1 = 3;
    wallS2 = 4;
    wallS3 = 2;
  }
  if (playerY - playerH/2 < wallY3 + wallH3/2 && (playerY + playerH/2 > wallY3 + wallH3/2) && mouseX < wallX3 + wallW3/2 && mouseX > wallX3 - wallW3/2) {
    //println("hit yellow!");
    score = 0;
    wallS1 = 3;
    wallS2 = 4;
    wallS3 = 2;
  }
}

void wall1() { //The code for the red walls
  fill(red);
  rect(wallX1, wallY1, wallW1, wallH1);
  wallY1 += wallS1;

  if (wallY1 > height + 50) {
    wallY1 = -50;
    wallX1 = random(50, 350);
    wallW1 = random(100, 200);
    wallS1 += 0.1;
    
  score = score+1;
  println(score);
  }
}

void wall2() { //The code for the green walls
  fill(green);
  rect(wallX2, wallY2, wallW2, wallH2);
  wallY2 += wallS2;

  if (wallY2 > height + 50) {
    wallY2 = -50;
    wallX2 = random(50, 350);
    wallW2 = random(100, 200);
    wallS2 += 0.1;
    
  score = score+1;
  println(score);
  }
}

void wall3() { //The code for the yellow walls
  fill(yellow);
  rect(wallX3, wallY3, wallW3, wallH3);
  wallY3 += wallS3;

  if (wallY3 > height + 50) {
    wallY3 = -50;
    wallX3 = random(50, 350);
    wallW3 = random(50, 150);
    wallS3 += 0.1;
    
  score = score+1;
  println(score);
  }
}