Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
 Interactive Toy: Flappy fly
 Assignment 2
 Intro to Media Computation
 Nicolas Hesler
 Made by Baker Albach
 
 Play as a fly trying to get as much food as possible without falling into deadly spikes!
 Crawl on and over the platforms to move around and stop yourself from falling!
 
 Use the arrow keys to move around
 TIP: tapping provides more control!
 
 
 Shout out to Jasper for all the much needed assistance
 
 */




float playerX = 200;          // declaring global variables before setup
float playerY = 0;
float playerMoveX;
float playerMoveY;

float gravity = 0.3;
boolean onPlatform = false;

float playerSize = 25;

boolean doSpawnFood = true;
float foodX;
float foodY;
float foodSize = 8;

float p1X, p1Y, p2X, p2Y, p3X, p3Y;            // x and y coordiantes for platforms 1 2 &3
float platformSizeX = 85, platformSizeY = 5;
boolean platform1Spawn = false;
boolean platform2Spawn = false;
boolean platform3Spawn = false;

boolean isGameOver = false;

int score = 0;

void setup() {
  size(400, 400);
  rectMode(CENTER);
}


void draw() {
  if (isGameOver == true) {            //game over loop
    gameOver();
    if (keyPressed) {
      reset();
    }
  } else {
    background(66, 165, 255);
    randomizePlatforms();
    spawnFood();
    scoreDisplay();
    foodTouch();
    updatePlayer();   
    updateGravity();
    spikeTouch();
    platformTouch();
    drawTrees();
    drawPlatforms();
    drawFood();
    drawSpikes();
    drawPlayer();
  }
}

void drawSpikes() {              //draw loop for the spikes along the bottom of the screen
  for (int i = 0; i<400; i+= 25) {        // increments distance between each triangle so they don't overlap
    stroke(0);
    fill(170);
    triangle(i, 350, i+12.5, 300, i+25, 350);
  }
}

void spikeTouch() {                 // didn't need to code collision for each shape, just noted when playerY goes below spike line
  if (playerY >= 300) {
    isGameOver = true;
  }
}

void gameOver() {             
  fill(0);
  textSize(50);
  text("GAME OVER", 65, 200);
  if (keyPressed) {                 // resets game when a key is pressed
    isGameOver = false;
  }
}

void scoreDisplay(){
  fill(0);
  textSize(13);
  text("SCORE: " + score, 335, 370);
  
}

void reset() {                      //resets player to starting position, returns booleans to starting states
  playerX = 200;
  playerY = 0;
  onPlatform = false;
  doSpawnFood = true;
  score = 0;
}

void updateGravity() {                    // implements gravity on player's Y movement
  if (playerY <= 300 && onPlatform == false) {
    playerMoveY += gravity;
  }
}

void updatePlayer() {                // updates player location, as well as constrains player to the boundaries of the screen
  playerX += playerMoveX;
  playerY += playerMoveY;

  playerX = constrain(playerX, 0+playerSize/2, 400-playerSize/2);
  playerY = constrain(playerY, 0+playerSize/2, 350-playerSize/2);
  
}

void drawPlayer() {                 
  fill(0);
  ellipse(playerX, playerY, playerSize-5, playerSize);
  strokeWeight(1);
  stroke(200);
  fill(180);
  ellipse(playerX-7*(sin(radians(frameCount*5))), playerY+3, 8*(sin(radians(frameCount*5))), 16);      //flapping wing animation
  ellipse(playerX+7*(sin(radians(frameCount*5))), playerY+3, 8*(sin(radians(frameCount*5))), 16);
}

void drawTrees() {
  for (int i = 0; i<400; i+= 80) {
    stroke(0);
    fill(92, 71, 32);
    rect(i+40, 320, 20, 60);
  }
  for (int i = 0; i<400; i+= 80) {        // increments distance between each tree so they don't overlap
    stroke(0);
    fill(26, 138, 51);
    triangle(i, 290, i+40, 175, i+80, 290);
  }
}

void drawFood() {
  strokeWeight(1);
  stroke(0);
  fill(255, 0, 0);
  ellipse(foodX, foodY, foodSize, foodSize);
}

void spawnFood() {                  // spawns food in a random location, while limiting the spawn area to above the spikes
  if (doSpawnFood) {

    foodX = (round(random(399)));
    foodY = (round(random (0, 250)));
    doSpawnFood = false;
  }
}

void foodTouch() {                  // detects collision with food & spawns another instance of food
  if (playerX + playerSize/2 >= foodX - foodSize/2 && playerY - playerSize/2 <= foodY + foodSize/2 && playerX - playerSize/2 <= foodX + foodSize/2 && playerY + playerSize/2 >= foodY - foodSize/2) {
    score = score+1;
    doSpawnFood = true;
  }
}

void randomizePlatforms() {                  // randomizes spawn location of each platform while limiting them to certian areas of the screen
  if (platform1Spawn == false) {
    p1X = random(200 + platformSizeX/2, 400 - platformSizeX/2);
    p1Y = random(100 + platformSizeY/2, 300 - platformSizeY/2);
    platform1Spawn = true;
  }
  if (platform2Spawn == false) {
    p2X = random(0 + platformSizeX/2, 200 - platformSizeX/2);
    p2Y = random(100 + platformSizeY/2, 300 - platformSizeY/2);
    platform2Spawn = true;
  }
  if (platform3Spawn == false) {
    p3X = width/2;                          // constrains platform's X to avoid player losing on spawn
    p3Y = random(100 + platformSizeY/2, 200 - platformSizeY/2);
    platform3Spawn = true;
  }
}

void drawPlatforms() {
  rectMode(CENTER);

  fill(175);
  rect(p1X, p1Y, platformSizeX, platformSizeY);
  rect(p2X, p2Y, platformSizeX, platformSizeY);
  rect(p3X, p3Y, platformSizeX, platformSizeY);
}

void platformTouch() {
  // Right platform
  if (playerX + playerSize/2 >= p1X - platformSizeX/2 && playerY - playerSize/2 <= p1Y + platformSizeY/2 && playerX - playerSize/2 <= p1X + platformSizeX/2 && playerY + playerSize/2 >= p1Y - platformSizeY/2) {
    if (playerY <= p1Y) {            //collision for top of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
      onPlatform = true;
    } else if (playerY >= p1Y) {     //collision for bottom of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
    } else if (playerX >= p1X) {     //collision for side of platform
      playerMoveX = 0;
    } else if (playerX <= p1X) {     //collision for side of platform
      playerMoveX = 0;
    }
  } else {
    onPlatform = false;
  }
  //  Left platform
  if (playerX + playerSize/2 >= p2X - platformSizeX/2 && playerY - playerSize/2 <= p2Y + platformSizeY/2 && playerX - playerSize/2 <= p2X + platformSizeX/2 && playerY + playerSize/2 >= p2Y - platformSizeY/2) {
    if (playerY <= p2Y) {            //collision for top of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
      onPlatform = true;
    } else if (playerY >= p2Y) {     //collision for bottom of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
    } else if (playerX >= p2X) {     //collision for side of platform
      playerMoveX = 0;
    } else if (playerX <= p2X) {     //collision for side of platform
      playerMoveX = 0;
    }
  } else {
    onPlatform = false;
  }
  // middle platform
  if (playerX + playerSize/2 >= p3X - platformSizeX/2 && playerY - playerSize/2 <= p3Y + platformSizeY/2 && playerX - playerSize/2 <= p3X + platformSizeX/2 && playerY + playerSize/2 >= p3Y - platformSizeY/2) {
    if (playerY <= p3Y) {            //collision for top of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
      onPlatform = true;
    } else if (playerY >= p3Y) {     //collision for bottom of platform
      playerMoveY = 0;
      playerMoveX *= 0.95;
    } else if (playerX >= p3X) {     //collision for side of platform
      playerMoveX = 0;
    } else if (playerX <= p3X) {     //collision for side of platform
      playerMoveX = 0;
    }
  } else {
    onPlatform = false;
  }
}

void keyPressed() {                 // controls for player
  if (isGameOver == false) {
    if (key == CODED) {                           
      if (keyCode == UP) {
        playerMoveY = -5;
      }
      if (keyCode == DOWN) {
        playerMoveY = 5;
      }
      if (keyCode == LEFT) {
        playerMoveX = - 5;
      }
      if (keyCode == RIGHT) {  
        playerMoveX = 5;
      }
    }
  }
}

void keyReleased() {
  if (isGameOver == false) {
    playerMoveX = playerMoveX*0.5;
  }
}