Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Elite: 2D Toy

//Fly about in the starting-off Elite system of LHS 3447 like the one I started in.

// Control the ship using WASD

//float for the spaceship
float playerShipX = 0;
float playerShipY = 0;

//float for the planets and stars
float planetX1 = 20;
float planetX2 = 135;
float planetX3 = 210;
float planetX4 = 325;
float planetX5 = 270;
float planetX6 = 190;
float star1X = 30;
float star2X = 60;
float star3X = 90;
float star4X = 120;
float star5X = 150;
float star6X = 180;
float star7X = 210;
float star8X = 240;
float star9X = 270;
float starAX = 300;
float starBX = 330;
float starCX = 360;
float starDX = 390;

//float for the stars
float speed = 3;

//Key presses to move the ship
boolean goleft = false;
boolean goRight = false;
boolean goUp = false;
boolean goDown = false;
boolean stop = false;

//for the flame burners from the back of the ship
boolean isClicked = false;
int selected = (int)random(0, 4);
int choicesR[] = {0, 69, 100, 0};
int choicesG[] = {0, 40, 100, 0};
int choicesB[] = {0, 16, 100, 0};
color flame = color(choicesR[selected], choicesG[selected], choicesB[selected]);


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

void setup(){
  size(400, 400);
  frameRate = 30;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

void draw() {
 
  drawBackground();
  
  drawStars();
  moveStars();
    
  drawPlanets();
  movePlanets();
  
  moveShip();
  drawShip();
}      

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

//Draw the whole spaceship
void drawShip(){

  //Player ship
  ellipseMode(CENTER);
  
  stroke(224, 191, 0);
  fill(52);
  triangle(114 + playerShipX, 170 + playerShipY, 115 + playerShipX, 180 + playerShipY, 130 + playerShipX, 180 + playerShipY);
  fill(52);
  triangle(130 + playerShipX, 180 + playerShipY, 130 + playerShipX, 200 + playerShipY, 140 + playerShipX, 200 + playerShipY);
  stroke(224, 191, 0);
  fill(214, 30);
  ellipse(95 + playerShipX, 180 + playerShipY, 40, 50);
  fill(52);
  rect(45 + playerShipX, 155 + playerShipY, 50, 45);
  fill(52);
  rect(95 + playerShipX, 180 + playerShipY, 35, 20);
  fill(52);
  rect(40 + playerShipX, 175 + playerShipY, 5, 25);
  stroke(150);
  fill(64);
  triangle(50 + playerShipX, 140 + playerShipY, 50 + playerShipX, 180 + playerShipY, 110 + playerShipX, 200 + playerShipY);
  fill(64);
  triangle(50 + playerShipX, 180 + playerShipY, 50 + playerShipX, 230 + playerShipY, 110 + playerShipX, 200 + playerShipY);
  
  //flame burners from the back of the ship
  noStroke();
  fill(211, 109, 0, 200);
  triangle(40 + playerShipX, 175 + playerShipY, 20 + playerShipX, 187.5 + playerShipY, 40 + playerShipX, 200 + playerShipY);
  fill(211, 177, 0, 200);
  triangle(40 + playerShipX, 180 + playerShipY, 30 + playerShipX, 187.5 + playerShipY, 40 + playerShipX, 195 + playerShipY);
  
  if  (isClicked == true) {
    mousePressed();
  } else {
    isClicked = false;
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

void moveShip(){

// Use the WASD to control the ship; up=W, left=A, down=S, and right=D
  if (playerShipX >= -40) {
    if ( goleft == true) {
      playerShipX= playerShipX -speed;
    }
  }
  if (playerShipX <= 255) {
    if ( goRight == true) {
      playerShipX= playerShipX +speed;
    }
  }
  if (playerShipY <= 200 ) {
    if ( goUp == true) {
      playerShipY= playerShipY +speed;
    }
  }
  if (playerShipY >= -160 ) {
    if ( goDown == true) {
      playerShipY= playerShipY -speed;
    }
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
// This is to draw the space background
void drawBackground() {
  background(0);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void drawStars() {
  noStroke();
  fill(255);
  rect(star1X, 60, 2, 2);
  rect(star1X, 360, 2, 2);
  rect(star2X, 140, 2, 2);
  rect(star2X, 300, 2, 2);
  rect(star3X, 80, 2, 2);
  rect(star3X, 180, 2, 2);
  rect(star4X, 20, 2, 2);
  rect(star4X, 240, 2, 2);
  rect(star5X, 120, 2, 2);
  rect(star5X, 340, 2, 2);
  rect(star6X, 160, 2, 2);
  rect(star6X, 200, 2, 2);
  rect(star7X, 60, 2, 2);
  rect(star7X, 260, 2, 2);
  rect(star8X, 140, 2, 2);
  rect(star8X, 340, 2, 2);
  rect(star9X, 80, 2, 2);
  rect(star9X, 180, 2, 2);
  rect(starAX, 20, 2, 2);
  rect(starAX, 240, 2, 2);
  rect(starBX, 120, 2, 2);
  rect(starBX, 380, 2, 2);
  rect(starCX, 160, 2, 2);
  rect(starCX, 220, 2, 2);
  rect(starDX, 60, 2, 2);
  rect(starDX, 300, 2, 2);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void moveStars() {
  star1X = star1X - 1;
  star2X = star2X - 1;
  star3X = star3X - 1;
  star4X = star4X - 1;
  star5X = star5X - 1;
  star6X = star6X - 1;
  star7X = star7X - 1;
  star8X = star8X - 1;
  star9X = star9X - 1;
  starAX = starAX - 1;
  starBX = starBX - 1;
  starCX = starCX - 1;
  starDX = starDX - 1;
    
//The stars will reset once they reach the left edge

  if (star1X == 0) {
    star1X = width;
  }
  if (star2X == 0) {
    star2X = width;
  }
  if (star3X == 0) {
    star3X = width;
  }
  if (star4X == 0) {
    star4X = width;
  }
  if (star5X == 0) {
    star5X = width;
  }
  if (star6X == 0) {
    star6X = width;
  }
  if (star7X == 0) {
    star7X = width;
  }
  if (star8X == 0) {
    star8X = width;
  }
  if (star9X == 0) {
    star9X = width;
  }
  if (starAX == 0) {
    starAX = width;
  }
  if (starBX == 0) {
    starBX = width;
  }
  if (starCX == 0) {
    starCX = width;
  }
  if (starDX == 0) {
    starDX = width;
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

void drawPlanets() {
  // the star
  fill(255, 124, 0);
  ellipse(200, -40, 260, 260);
  fill(255, 200, 0, 150);
  ellipse(200, -40, 300, 300);
  // the planets
  noStroke();
  fill(100, 81, 40);
  ellipse(planetX1, 10, 15, 15);
  fill(80, 61, 19);
  ellipse(planetX2, 20, 19, 19);
  fill(100, 40, 4);
  ellipse(planetX3, 50, 29, 29);
  fill(8, 14, 52);
  ellipse(planetX4, 140, 35, 35);
  fill(52, 29, 6);
  ellipse(planetX5, 190, 40, 40);
  fill(25, 0, 41);
  ellipse(planetX6, 280, 100, 100);

}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void movePlanets() {
  planetX1 = planetX1 - 2;
  planetX2 = planetX2 - 3.5;
  planetX3 = planetX3 - 5;
  planetX4 = planetX4 - 6;
  planetX5 = planetX5 - 7;
  planetX6 = planetX6 - 9;
  
//Like with the stars, the planets reset once they get to left, they repeat

  if (planetX1 < -5) {
     planetX1 = width;
  }
  if (planetX2 < -15) {
     planetX2 = width;
  }
  if (planetX3 < -20) {
     planetX3 = width;
  }
  if (planetX4 < -25) {
     planetX4 = width;
  }
  if (planetX5 < -30) {
     planetX5 = width;
  }
  if (planetX6 < -38) {
     planetX6 = width;
  }  
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
// IMPORTANT NOTE!!!! REPEAT, IMPORTANT NOTE!!!! 'W' is to fly up as 'S' is to fly down. For some reason, it goes in reverse and causes problems.

void keyPressed() {
 
  if (key == 'a') {
    goleft = true;
  }
  if (key == 's') {
    goUp = true;
  }
  if (key == 'w') {
    goDown = true;
  }
  if (key == 'd') {
    goRight = true;
  }
} 

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
 
void keyReleased () {
  if (key == 'a') {
    goleft = false;
  }
  if (key == 's') {
    goUp = false;
  }
  if (key == 'w') {
    goDown = false;
  }
  if (key == 'd') {
    goRight = false;
  }
  
    if (playerShipX >= 450) {
    speed = 0;
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Press the mouse to activate the ship's burners
void mousePressed () {
  
  int selected = (int)random(0, 4);
  int choicesR[] = {0, 69, 100, 0};
  int choicesG[] = {0, 40, 100, 0};
  int choicesB[] = {0, 16, 100, 0};
  color flame = color(choicesR[selected], choicesG[selected], choicesB[selected], 190);
  isClicked = true;
  fill(flame, 220);
  
  triangle(40 + playerShipX, 175 + playerShipY, 20 + playerShipX, 187.5 + playerShipY, 40 + playerShipX, 200 + playerShipY);
  triangle(40 + playerShipX, 180 + playerShipY, 30 + playerShipX, 187.5 + playerShipY, 40 + playerShipX, 195 + playerShipY);
}

void mouseReleased() {
  isClicked = false;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Pres the ESC key to exit the program
void endProgram() {
  if (keyCode == 27) {
    stop = true;
  }
}