For my final project I continued developed the random landscape project, adding a collision with one of the planets. I loaded more images to get the effect and modified the direction of background stars so they look more real when the spaceship is traveling ahead. The collision and explosion would bring the animation to an end.
//create objects
var planets = [];
var starsLeft = [];
var redStar = [];
var starsRight = [];
var starsBR = [];
var starsBL = [];
var size2 = 20;
var size3 = 350;
var light;
var light2;
var smoke;
var boom;
function preload(){
light = loadImage("http://i.imgur.com/rU3fzb5.png");
light2 = loadImage("http://i.imgur.com/UsbCAS5.png");
smoke = loadImage("http://i.imgur.com/3Uer6bI.png");
boom = loadImage("http://i.imgur.com/9V62Xu1.png");
}
function setup() {
createCanvas(600,400);
frameRate(8);
// initial planet and stars
var initialX = random(width/2);
planets[0] = makePlanets(initialX);
redStar[0] = makeRedPlanets(initialX);
redStar[1] = makeRedPlanets(initialX);
}
function draw() {
background(0);
PlanetsinMotion();
addPlanets();
StarsLinMotion();
addStarsL();
redStarinMotion();
addRed();
StarsRinMotion();
addStarsR();
StarsBRinMotion();
addStarsBR();
StarsBLinMotion();
addStarsBL();
fill("blue");
noStroke();
strokeWeight(2);
ellipse(width/2, height/2, size2, size2);
size2 +=1;
if (size2 > 200){
if (frameCount%2 > 0) {
image(light, 0,0, 500 ,500);
}
}
if (size2 > 300) {
image(light2, 100, 0, 500, 500);
}
spaceship();
if (size2 > 400) {
fill("black");
rect(0, 0, 600, 400);
fill(255, 108, 17);
ellipse(width/2, height/2, size3, size3);
size3 -= 20;
image(boom, 0, -100, 600, 600);
}
}
// all about the planets
function PlanetsinMotion() {
for (var i = 0; i < planets.length; i++) {
planets[i].move();
planets[i].display();
}
}
function addPlanets() {
var maybeAdd = 0.004;
if (random(0,1) < maybeAdd) {
planets.push(makePlanets(width));
}
}
function moveplanets() {
this.x += this.speedX;
this.y += this.speedY;
}
function displayplanets() {
fill(187, 210, 247);
noStroke();
ellipse(this.x, this.y, this.randomsize, this.randomsize);//blue planets
this.randomsize += -0.01;
}
function makePlanets(planetX) {
var plt = { x: planetX,
y: height/2,
speedX: -1.0,
speedY: -0.5,
randomsize: random (10, 50),
move: moveplanets,
display: displayplanets,
}
return plt;
}
//all about the red stars
function redStarinMotion() {
for (var i = 0; i < redStar.length; i++) {
redStar[i].move();
redStar[i].display();
}
}
function moveRed() {
this.x += this.speedX;
this.y += this.speedY;
}
function displayRed() {
fill(122, 43, 19);
noStroke();
ellipse(this.x, this.y, this.randomsize, this.randomsize);
this.randomsize += 0.1;
}
function addRed() {
var maybeAdd = 0.003;
if (random(0,1) < maybeAdd) {
redStar.push(makeRedPlanets(width));
}
}
function makeRedPlanets(redX) {
var red = {x: random(100,200),
y: random(height-50, height-100),
speedX: -0.5,
speedY: -0.7,
randomsize: random(5, 8),
move: moveRed,
display: displayRed
}
return red;
}
//all about the starsL
function StarsLinMotion() {
for (var i = 0; i < starsLeft.length; i++){
starsLeft[i].move();
starsLeft[i].display();
}
}
function displayStarsL() {
stroke("white");
point(this.x, this.y);
}
function addStarsL() {
var maybeAdd = 0.6;
if (random(0,1) < maybeAdd) {
starsLeft.push(makeStarsL());
}
}
function moveStarsL() {
this.x += this.speedX;
this.y += this.speedY;
}
function makeStarsL() {
var str = {x: random(0, width/2-50),
y: random(0, height/2+50),
speedX: -1.0,
speedY: -0.7,
move: moveStarsL,
display: displayStarsL
}
return str;
}
//all about the starsRight
function StarsRinMotion() {
for (var i = 0; i < starsRight.length; i++){
starsRight[i].move();
starsRight[i].display();
}
}
function displayStarsR() {
stroke("white");
point(this.x, this.y);
}
function addStarsR() {
var maybeAdd = 0.3;
if (random(0,1) < maybeAdd) {
starsRight.push(makeStarsR());
}
}
function moveStarsR() {
this.x += this.speedX;
this.y += this.speedY;
}
function makeStarsR(StarX) {
var strR = {x: random(width/2-2, width-50),
y: random(0, height/2+10),
speedX: 1.0,
speedY: -0.7,
move: moveStarsR,
display: displayStarsR
}
return strR;
}
//all about the starsBR
function StarsBRinMotion() {
for (var i = 0; i < starsBR.length; i++){
starsBR[i].move();
starsBR[i].display();
}
}
function displayStarsBR() {
stroke("white");
point(this.x, this.y);
}
function addStarsBR() {
var maybeAdd = 0.3;
if (random(0,1) < maybeAdd) {
starsBR.push(makeStarsBR());
}
}
function moveStarsBR() {
this.x += this.speedX;
this.y += this.speedY;
}
function makeStarsBR(StarX) {
var strBR = {x: random(width/2-2, width-50),
y: random(height/2-10, height),
speedX: 1.0,
speedY: 0.7,
move: moveStarsBR,
display: displayStarsBR
}
return strBR;
}
//all about the starsBL
function StarsBLinMotion() {
for (var i = 0; i < starsBL.length; i++){
starsBL[i].move();
starsBL[i].display();
}
}
function displayStarsBL() {
stroke("white");
point(this.x, this.y);
}
function addStarsBL() {
var maybeAdd = 0.3;
if (random(0,1) < maybeAdd) {
starsBL.push(makeStarsBL());
}
}
function moveStarsBL() {
this.x += this.speedX;
this.y += this.speedY;
}
function makeStarsBL(StarX) {
var strBL = {x: random(0, width/2+50),
y: random(height/2-10, height),
speedX: -1.0,
speedY: 0.7,
move: moveStarsBL,
display: displayStarsBL
}
return strBL;
}
//creating the interior of spaceship
function spaceship() {
noStroke();
fill(53, 58, 66);
rect(0, 315, width, height-315);//floor
fill(95, 105, 122);//light color
quad(287, 0, 357, 0, 419, 26, 397, 53);//3rd wall 1
quad(311, 255, 173, 249, 184, 269, 311, 271);//2nd wall 1
quad(27, 213, 0, 223, 0, 269, 30, 269);//1st wall 3
quad(30, 269, 25, 381, 0, 361, 0, 269);//1st wall 4
fill(81, 90, 104);// medium light
quad(419, 26, 397, 53, 409, 126, 497, 110);//3rd wall 2
quad(311, 255, 311, 271, 476, 276, 497, 250);//3rd wall 4
quad(311, 330, 311, 271, 184, 269, 186, 339);//2nd wall 2
triangle(495, 0, 357, 0, 419, 26);//1st wall 1-3
fill(73, 81, 94);//slightly dark
quad(409, 126, 497, 110, 497, 250, 311, 255);//3rd wall 3
quad(476, 351, 476, 276, 311, 271, 311, 330);//3rd wall 6
quad(600, 87, 497, 110, 419, 26, 495, 0);//4th wall 1
triangle(495, 0, 600, 87, 600, 0);//4th wall 1-2
fill(62, 68, 78);//dark
quad(476, 276, 497, 250, 497, 334, 476, 349);//3rd wall 5
fill(144, 156, 175);//lightest
quad(187, 271, 162, 229, 27, 213, 30, 269);//1st wall 1
fill(122, 133, 153);//still very light
quad(30, 269, 187, 271, 186, 357, 25, 381);//1st wall 2
fill(65, 73, 86);//darkest
quad(497, 110, 600, 87, 600, 130, 497, 150);//4th wall 2-1
quad(497, 150, 536, 142, 536, 345, 497, 334);//4th wall 2-2
quad(536, 345, 536, 262, 600, 268, 600, 360); //4th wall 2-3
if (size2 > 350) {
image(smoke, 0, 0, 500, 300);
}
}