Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//////////////////////////////////////////////////////////////////////////////
//                          Satallite flyer                                 //
//            \\                                           //               //
//                            Ethan Davis                                   //
//                                                                          //
//This is an interactive toy of a Satallite.                                //
//You can fly it around the screen with space like physics and acceleration.//
//Hitting the edge of the screen will make the satallite bounce             //
//Flying close to the planet will have a gravitational pull towards it.     //
//Clicking turns on your beacon                                             //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

//Variables that control the stars
float StarGlow1 = 2;
float StarGrow1 = 0.5;
float StarGlow2 = 5;
float StarGrow2 = 0.5;
float StarGlow3 = 10;
float StarGrow3 = 0.5;

//variables that control the planet
float PlanetX = 300;
float PlanetY = 300;
float R = random(0, 255);
float G = random(0, 255); 
float B = random(0, 255);

//variables that control the satallite
float xSpeed = 0;
float ySpeed = 0;
float SatX = 200;
float SatY = 200;
float SatLight = 10;
float SatGrow = 1.5;

//variables that the user interacts with
boolean UP = false;
boolean DOWN = false;
boolean LEFT = false;
boolean RIGHT = false;
boolean CLICK = false;


void setup() {

  size(400, 400);
  rectMode(CENTER);
}

void draw() {
  
  background(0);
  PlanetGravity();
  movement();
  drawStars();
  //This grid was a personal grid I looped for location refrence when needed.
  //drawGrid();
  drawPlanets();
  drawThrusters();
  drawSatalite();

}

void drawStars() {

  //Draw Gradient that follows the planet
  //This creates a light effect for the planet
  noStroke();
  int x = 1000;
  float c = 0;
  c = constrain (c, 0, 255);
  while (x >= 0) {
    fill(c);  
    ellipse(PlanetX, PlanetY, x, x); 
    x = x - 50;
    c = c + 3;
  }
  //Draw the star glow
  rect(100, 10, 3, 3);
  fill(255, 100);
  rect(225, 225, StarGlow1, StarGlow1);
  rect(350, 125, StarGlow1, StarGlow1);
  rect(75, 200, StarGlow1, StarGlow1);
  rect(125, 100, StarGlow1, StarGlow1);
  rect(123, 294, StarGlow2, StarGlow2);
  rect(360, 380, StarGlow2, StarGlow2);
  rect(372, 342, StarGlow2, StarGlow2);
  rect(300, 300, StarGlow2, StarGlow2);
  rect(235, 99, StarGlow3, StarGlow3);
  rect(100, 10, StarGlow3, StarGlow3);
  rect(26, 78, StarGlow3, StarGlow3);
  rect(146, 360, StarGlow3, StarGlow3);
  fill(255);

  //Draw the Stars
  rect(225, 225, 3, 3);
  rect(350, 125, 3, 3);
  rect(75, 200, 3, 3);
  rect(125, 100, 3, 3);
  rect(123, 294, 3, 3);
  rect(360, 380, 3, 3);
  rect(372, 342, 3, 3);
  rect(300, 300, 3, 3);
  rect(235, 99, 3, 3);
  rect(100, 10, 3, 3);
  rect(26, 78, 3, 3);
  rect(146, 360, 3, 3);

  //Make the glowing star light expand and revert
  //This creates the glowing effect on the stars
  if (StarGlow1 >= 10|| StarGlow1 <= 0.5) {
    StarGrow1 = StarGrow1 * -1;
  }
  StarGlow1 = StarGlow1 + StarGrow1;

  if (StarGlow2 >= 10|| StarGlow2 <= 0.2) {
    StarGrow2 = StarGrow2 * -1;
  }
  StarGlow2 = StarGlow2 + StarGrow2;

  if (StarGlow3 >= 10|| StarGlow3 <= 0.7) {
    StarGrow3 = StarGrow3 * -1;
  }
  StarGlow3 = StarGlow3 + StarGrow3;
}

void drawGrid() {

  //Draw a line grid for personal refrence
  //This helped me finalize spacing
  float x = 50;
  float y = 50;
  line (50, 0, 50, 400);
  stroke(255);
  strokeWeight(1);
  while (x <= 400) {

    line(x, 0, x, 400);
    x = x+50;
  }
  while (y <=400) {
    line (0, y, 400, y);
    y = y+50;
  }
}

void drawPlanets() {

  //Make the planet spin in a circle
  //It will spin on a large axis around the center, slowly
  PlanetX = 200-(cos((frameCount-28)*0.0015)*200);
  PlanetY = 200-(sin((frameCount-28)*0.0015)*200);

  //Give the planet a random colour every time the sketch is run
  fill(R, G, B);
  ellipse(PlanetX, PlanetY, 100, 100);

  //Fill craters with same random colour, only darker
  fill(R-20, G-20, B-20);
  ellipse(PlanetX-25, PlanetY-25, 20, 20);
  ellipse(PlanetX+10, PlanetY+10, 10, 10);
  ellipse(PlanetX+20, PlanetY-10, 15, 15);
  ellipse(PlanetX-25, PlanetY+17, 17, 17);
  ellipse(PlanetX-5, PlanetY-30, 5, 5);
}

void drawSatalite() {

  //Satallite base
  stroke(150);
  strokeWeight(3);
  line(SatX-10, SatY-10, SatX+10, SatY+10);
  noStroke();
  fill(200);
  rect(SatX, SatY, 16, 16);

  //Yellow glass
  fill(121, 98, 33);
  ellipse(SatX-3, SatY+3, 7, 7);
  fill(240, 204, 106);
  ellipse(SatX-3, SatY+3, 5, 5);

  //WINGS
  fill(21, 36, 129);
  quad(SatX, SatY-20, SatX-40, SatY-50, SatX-50, SatY-40, SatX-20, SatY);
  quad(SatX, SatY+20, SatX+40, SatY+50, SatX+50, SatY+40, SatX+20, SatY);

  ///Details on Wings\\\
  stroke(50);
  strokeWeight(1.5);

  //Left Wing
  line(SatX-45, SatY-45, SatX-10, SatY-10);
  line(SatX-27, SatY-12, SatX-12, SatY-27);
  line(SatX-37, SatY-25, SatX-25, SatY-37);
  line(SatX-45, SatY-35, SatX-35, SatY-45);

  //Right Wing
  line(SatX+45, SatY+45, SatX+10, SatY+10);
  line(SatX+27, SatY+12, SatX+12, SatY+27);
  line(SatX+37, SatY+25, SatX+25, SatY+37);
  line(SatX+45, SatY+35, SatX+35, SatY+45);

  ////////Reflections help give the appearance of solar panels\\\\\\\\
  //Left Wing reflection
  noStroke();
  fill(91, 102, 170);
  ellipse(SatX-38, SatY-45, 3, 3);
  ellipse(SatX-30, SatY-38, 5, 5);
  ellipse(SatX-18, SatY-28, 8, 8);
  ellipse(SatX-7, SatY-20, 8, 8);

  //Right Wing Reflection
  ellipse(SatX+48, SatY+41, 3, 3);
  ellipse(SatX+42, SatY+34, 5, 5);
  ellipse(SatX+32, SatY+24, 8, 8);
  ellipse(SatX+22, SatY+10, 8, 8);

  //Draw Click lights/alter appearence
  //Use a boolean statement to toggle the effect, isntead of holding the mouse down
  if (CLICK == true) {
    fill(255, 0, 0, 100);
    noStroke();
    ellipse(SatX+7, SatY-7, SatLight, SatLight);
    fill(255, 0, 0);
    noStroke();
    ellipse(SatX+7, SatY-7, 5, 5);
    //Make Satallite lights flicker
    //Ellipse expands and shrinks making it look like a signal
    if (SatLight >= 30 || SatLight <= 0.5) {
      SatGrow = SatGrow * -1;
    }
    SatLight = SatLight + SatGrow;
  } else {
    //Toggle the lights off
    fill(150, 0, 0);
    noStroke();
    ellipse(SatX+7, SatY-7, 5, 5);
  }
}

void movement() {

  //Accelerate UP
  //Boolean statements combined with keypressed and keyreleased helped
  //the satallite move in multiple directions at one time
  if (UP == true) {
    ySpeed = ySpeed - 0.02;
  }
  //Accelerate DOWN
  if (DOWN == true) {
    ySpeed = ySpeed + 0.02;
  }
  //Accelerate LEFT
  if (LEFT == true) {
    xSpeed = xSpeed - 0.02;
  }  
  //Accelerate RIGHT
  if (RIGHT == true) {
    xSpeed = xSpeed + 0.02;
  }
  //Slow down motion Y
  if (ySpeed < 0) {
    ySpeed = ySpeed + 0.01;
    //force speed to 0 so it doesn't go backwards
    if (ySpeed > 0) {
      ySpeed =0;
    }
  }
  if (ySpeed > 0) {
    ySpeed = ySpeed - 0.01;
    if (ySpeed < 0) {
      ySpeed = 0;
    }
  }

  //Slow down motion X
  if (xSpeed < 0) {
    xSpeed = xSpeed + 0.01;
    if (xSpeed > 0) {
      xSpeed = 0;
    }
  }
  if (xSpeed > 0) {
    xSpeed = xSpeed - 0.01;
    if (xSpeed < 0) {
      xSpeed = 0;
    }
  }

  //Constrain The satalite to the canvas
  //by bouncing off the edges instead of using constrain
  if (SatY <= 0) { 
    ySpeed = ySpeed * -1;
  }
  if (SatY >= width) { 
    ySpeed = ySpeed * -1;
  }
  if (SatX <= 0) {
    xSpeed = xSpeed * -1;
  }
  if (SatX >=height) {
    xSpeed = xSpeed * -1;
  }

  //Apply the speed to Satalite
  //This allows it to move and accelerate
  SatY = SatY + ySpeed;
  SatX = SatX + xSpeed;

  //Constrain speed so it's not infinite
  //Without this, the satallite could infinately buyild up speed
  ySpeed = constrain(ySpeed, -2, 2);
  xSpeed = constrain(xSpeed, -2, 2);
}

void keyPressed() {

  //Check what keys are pressed
  //Recognize Up
  if (key == 'w' || key == 'W') {
    UP = true;
  }
  //Recognize Down
  if (key == 's' || key == 'S') {
    DOWN = true;
  }
  //Recognize Left
  if (key == 'a' || key == 'A') {
    LEFT = true;
  }
  //Recognize Right
  if (key == 'd' || key == 'D') {
    RIGHT = true;
  }
}

void keyReleased() {

  //Check when keys are released
  //Recognize Up
  if (key == 'w' || key == 'W') {
    UP = false;
  }
  //Recognize Down
  if (key == 's' || key == 'S') {
    DOWN = false;
  }
  //Recognize Left
  if (key == 'a' || key == 'A') {
    LEFT = false;
  }
  //Recognize Right
  if (key == 'd' || key == 'D') {
    RIGHT = false;
  }
}

void PlanetGravity() {

  //Create four quadrants to pull the satallite
  //depending on the quadrant it's in, it will pull the satallite towards the center of the planet

  //Top Left Quad
  if (SatX >= PlanetX - 100 && SatX < PlanetX && SatY >= PlanetY - 100 && SatY < PlanetY) {
    xSpeed = xSpeed + 0.007;
    ySpeed = ySpeed + 0.007;
  }

  //Top Right Quad
  if (SatX <= PlanetX + 100 && SatX > PlanetX && SatY >= PlanetY - 100 && SatY < PlanetY) {
    xSpeed = xSpeed - 0.007;
    ySpeed = ySpeed + 0.007;
  }

  //Bottom Left Quad
  if (SatX >= PlanetX - 100 && SatX < PlanetX && SatY <= PlanetY + 100 && SatY > PlanetY) {
    xSpeed = xSpeed + 0.007;
    ySpeed = ySpeed - 0.007;
  }

  //Bottom Right Quad
  if (SatX <= PlanetX + 100 && SatX > PlanetX && SatY <= PlanetY + 100 && SatY > PlanetY) {
    xSpeed = xSpeed - 0.007;
    ySpeed = ySpeed - 0.007;
  }
}

void drawThrusters() {
  float Random = random(20, 30);
  float Random2 = random(5, 15);

  noStroke();

  //Thrusters when flying up
  if (UP == true) {
    fill(160, 27, 209);
    triangle(SatX, SatY+ 5 +Random, SatX-5, SatY+5, SatX+5, SatY+5);
    fill(210, 110, 247);
    triangle(SatX, SatY+ 5 +Random2, SatX-5, SatY+5, SatX+5, SatY+5);
  }

  //Thrusters when flying down
  if (DOWN == true) {
    fill(160, 27, 209);
    triangle(SatX, SatY- 5 +(-1*Random), SatX-5, SatY-5, SatX+5, SatY-5);
    fill(210, 110, 247);
    triangle(SatX, SatY- 5 +(-1*Random2), SatX-5, SatY-5, SatX+5, SatY-5);
  }

  //Thrusters when flying Right
  if (RIGHT == true) {
    fill(160, 27, 209);
    triangle(SatX- 5 +(-1*Random), SatY, SatX, SatY-5, SatX, SatY+5);
    fill(210, 110, 247);
    triangle(SatX- 5 +(-1*Random2), SatY, SatX, SatY-5, SatX, SatY+5);
  }

  //Thrusters when flying Left
  if (LEFT == true) {
    fill(160, 27, 209);
    triangle(SatX+ 5 +Random, SatY, SatX, SatY-5, SatX, SatY+5);
    fill(210, 110, 247);
    triangle(SatX+ 5 +Random2, SatY, SatX, SatY-5, SatX, SatY+5);
  }
}

void mousePressed() {

  //Reset light so it always starts small when activated
  SatLight = 1;

  //Toggle using boolean so the light stays on untill clicked again
  if (CLICK == false) {
    CLICK = true;
  } else if (CLICK == true) {
    CLICK = false;
  }
}