Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* - - - - - - - - - - - - - - - - - - - - - - - /
 SHARKS WITH JETPACKS
 by A.Bellikka
 Submitted October 23, 2016
 
 control the hot air balloon with WASD
 try to avoid sharks! chomp
 - - - - - - - - - - - - - - - - - - - - - - - - */

/*
-REFERENCES:
(N.Hesler, "Gone Fishing", n.d.) -- shark movement/code layout
(A.Bellikka, "Gameboy", 2016) -- balloon movement
(M.Arcadi, "SNEK", 2015) -- keyPressed conditionals
*/

//global variables
Balloon bloon;
JetShark shark;

//make only four sharks appear on the screen at one time
int sharkLimit = 4;
JetShark[] schoolOfSharks = new JetShark[sharkLimit];

// W A S D bools
boolean goUp = false;
boolean goDown = false;
boolean goLeft = false;
boolean goRight = false;


void setup()
{
  size(400, 400);
  frameRate(120);

  //call game objects
  bloon = new Balloon();
  createSharks();
}
void createSharks()
{
  for (int i = 0; i<schoolOfSharks.length; i++) {
    schoolOfSharks[i] = new JetShark();
  }
}

void draw()
{
  //sky blue
  background(#008aff);

  //call sky visuals
  drawClouds();
  drawGradientLoop();

  //call bloon-related functions
  keyPressed();
  bloon.display();
  bloon.goBalloon();

  //call shark-related functions
  //reference N.Hesler
  for (int i=0; i<schoolOfSharks.length; i++) {
    schoolOfSharks[i].moveSharks();

    for (int u=0; u<schoolOfSharks.length; u++) {
      schoolOfSharks[u].display();
    }
  }
}

void drawClouds()
{
  rectMode(CORNERS);

  fill(255);
  noStroke();

  //make the clouds "drift" side to side slowly
  rect(60+sin(frameCount*0.015)*7, 40, 160+sin(frameCount*0.015)*7, 100, 25);
  rect(140+sin(frameCount*0.015)*7, 60, 200+sin(frameCount*0.015)*7, 100, 25);

  rect(200+sin(frameCount*0.015)*7, 20, 240+sin(frameCount*0.015)*7, 40, 25);

  rect(260+sin(frameCount*0.015)*7, 60, 290+sin(frameCount*0.015)*7, 80, 25);
  rect(280+sin(frameCount*0.015)*7, 50, 330+sin(frameCount*0.015)*7, 80, 25);

  rect(180+sin(frameCount*0.015)*7, 220, 210+sin(frameCount*0.015)*7, 240, 25);
  rect(220+sin(frameCount*0.015)*7, 180, 320+sin(frameCount*0.015)*7, 240, 25);
  rect(300+sin(frameCount*0.015)*7, 210, 330+sin(frameCount*0.015)*7, 240, 25);

  rect(310+sin(frameCount*0.015)*7, 250, 350+sin(frameCount*0.015)*7, 270, 25);

  rect(-5+sin(frameCount*0.015)*7, 300, 60+sin(frameCount*0.015)*7, 340, 25);
  rect(40+sin(frameCount*0.015)*7, 280, 140+sin(frameCount*0.015)*7, 340, 25);
  rect(150+sin(frameCount*0.015)*7, 320, 180+sin(frameCount*0.015)*7, 340, 25);

  rect(220+sin(frameCount*0.015)*7, 380, 280+sin(frameCount*0.015)*7, 420, 25);
  rect(260+sin(frameCount*0.015)*7, 360, 380+sin(frameCount*0.015)*7, 420, 25);
}

void drawGradientLoop()
{
  rectMode(CENTER);
  noStroke();

  //int c refers to rect transparency
  //make the background have a loop of rectangles that create a gradient
  int y = 0;
  for (int c = 190; c > 0; c -=15)
  {
    fill(255, c);
    rect(width/2, y, width, 30);
    y = y+30;
  }
}

void keyPressed()
{
  //if the w key is pressed, bloon will go up
  if (key == 'w') {   
    goUp=true;
    goDown=false;
    goLeft=false;
    goRight=false;
    //if the a key is pressed, bloon will go left
  } else if (key == 'a') {
    goLeft=true;
    goRight = false;
    goUp = false;
    goDown = false;
    //if the s key is pressed, bloon will go down
  } else if (key == 's') {
    goDown = true;
    goUp = false;
    goLeft = false;
    goRight = false;
    //if the d key is pressed, bloon will go right
  } else if (key == 'd') {
    goRight=true;
    goUp = false;
    goDown = false;
    goLeft = false;
  }
}
class Balloon
{
  //variables
  PVector balloonPos = new PVector ();

  Balloon ()
  {
    //set bloon's initial position to the middle of the window
    balloonPos.x = width/2;
    balloonPos.y = height/2;
  }

  void goBalloon()
  { 
    //restrict bloon from going off screen 
    //ex. if the A key is pressed and the balloonPos.x is greater than 0,
    //    then the bloon can move left.

    if (goLeft == true && balloonPos.x > 0) {
      balloonPos.x = balloonPos.x - 1;
    } else if (goRight == true && balloonPos.x < width) {
      balloonPos.x = balloonPos.x + 1;
    } else if (goUp == true && balloonPos.y > 0) {
      balloonPos.y = balloonPos.y - 1;
    } else if (goDown == true && balloonPos.y < height) {
      balloonPos.y = balloonPos.y + 1;
    }
  }

  //constructor: draw a hot air balloon.
  void display()
  {
    ellipseMode(CENTER);
    rectMode(CENTER);

    //draw ropes
    stroke(#CB9E79);
    strokeWeight(2);
    line(balloonPos.x-15, balloonPos.y, balloonPos.x-10, balloonPos.y+50);
    line(balloonPos.x+15, balloonPos.y, balloonPos.x+10, balloonPos.y+50);

    noStroke();

    //draw bloon part
    fill(#FFE68B);
    ellipse(balloonPos.x, balloonPos.y, 60, 60);
    fill(#FFA052);
    ellipse(balloonPos.x, balloonPos.y, 40, 60);

    //draw basket part
    fill(#CB9E79);
    rect(balloonPos.x, balloonPos.y+60, 20, 20);
  }
}
class JetShark {

  PVector sharkPos = new PVector();
  PVector velocity = new PVector();

  JetShark() {
    sharkSpawn();
  }

  void sharkSpawn()
  {
    sharkPos.x = random(-200);
    sharkPos.y = random(0, 400);
    velocity.x = random(0.5,2);
  }


  void moveSharks()
  {
    //make the sharks move from left to right across the window
    sharkPos.add(velocity);

    //if a shark goes a little bit off screen, respawn at the beginning
    if (sharkPos.x > 550) {
      sharkSpawn();
    }
  }

  //draws Shark
  void display()
  {

    ellipseMode(CORNERS);
    rectMode(CORNERS);

    noStroke();

    //shark mouth
    fill(#FFA0A3);
    quad(sharkPos.x+45, sharkPos.y-10, sharkPos.x+45, sharkPos.y+10, sharkPos.x+20, sharkPos.y+10, sharkPos.x+20, sharkPos.y-10);

    //teeth
    fill(#FCF0F0);
    triangle(sharkPos.x+30, sharkPos.y-10, sharkPos.x+40, sharkPos.y-10, sharkPos.x+35, sharkPos.y);
    triangle(sharkPos.x+40, sharkPos.y-10, sharkPos.x+48, sharkPos.y-10, sharkPos.x+45, sharkPos.y);
    triangle(sharkPos.x+35, sharkPos.y+10, sharkPos.x+38, sharkPos.y, sharkPos.x+40, sharkPos.y+10);
    triangle(sharkPos.x+43, sharkPos.y+10, sharkPos.x+45, sharkPos.y, sharkPos.x+47, sharkPos.y+10);

    //shark's body
    fill(#85979D);
    ellipse(sharkPos.x-30, sharkPos.y-20, sharkPos.x+30, sharkPos.y+20);
    quad(sharkPos.x-50, sharkPos.y-20, sharkPos.x-40, sharkPos.y-0, sharkPos.x-50, sharkPos.y+20, sharkPos.x-20, sharkPos.y-0);
    quad(sharkPos.x-5, sharkPos.y-35, sharkPos.x+5, sharkPos.y-30, sharkPos.x+15, sharkPos.y-10, sharkPos.x-5, sharkPos.y-10);
    triangle(sharkPos.x, sharkPos.y-20, sharkPos.x+50, sharkPos.y-10, sharkPos.x+30, sharkPos.y-5);
    triangle(sharkPos.x, sharkPos.y+20, sharkPos.x+30, sharkPos.y+5, sharkPos.x+50, sharkPos.y+10);

    //shark fin and gills
    stroke(#7B8386);
    strokeWeight(2);
    line(sharkPos.x+5, sharkPos.y-7, sharkPos.x+5, sharkPos.y);
    line(sharkPos.x+10, sharkPos.y-7, sharkPos.x+10, sharkPos.y);
    line(sharkPos.x+15, sharkPos.y-7, sharkPos.x+15, sharkPos.y);

    noStroke();
    fill(#94A7AD);
    quad(sharkPos.x+10, sharkPos.y, sharkPos.x, sharkPos.y+5, sharkPos.x-10, sharkPos.y+5, sharkPos.x, sharkPos.y);

    //shark eyes
    stroke(0);
    strokeWeight(4);
    point(sharkPos.x+30, sharkPos.y-20);
    point(sharkPos.x+20, sharkPos.y-10);

    //jetpack
    noStroke();
    fill(#D6D6D6);
    ellipse(sharkPos.x, sharkPos.y+10, sharkPos.x+20, sharkPos.y+30);
    rect(sharkPos.x-15, sharkPos.y+10, sharkPos.x+10, sharkPos.y+30);

    fill(#C1C1C1);
    rect(sharkPos.x-20, sharkPos.y+10, sharkPos.x-15, sharkPos.y+30);

    //jet fire
    fill(#FFAF1A);
    triangle(sharkPos.x-20, sharkPos.y+10, sharkPos.x-20, sharkPos.y+30, sharkPos.x-38, sharkPos.y+20);
    fill(#FFAF1A);
    fill(#FF571A);
    triangle(sharkPos.x-20, sharkPos.y+10, sharkPos.x-20, sharkPos.y+30, sharkPos.x-35, sharkPos.y+20);
  }
}