Keyboard Arrow Keys.
xxxxxxxxxx
// SpaceX Simulator - Jon Bourne
// DMA1010 - Computers & Coding
// Credits & Attributions in accompanying Process Journal.
// (C) Jon Bourne 2018
//All Defining Variables
var bgmus;
var explodesfx;
var fireworksfx;
var startscreen;
var simstage;
var introvid;
var rocket;
var mainfontlight;
var gravitypower;
var thrustpower;
var thrustpowerside;
var rocketangle;
var landerxpos;
var landerypos;
var landersizeparam;
var closetoland;
var rocketimg;
var landerimg;
var rocketfiredimg;
var topspeedrocket;
var movedtoofast;
var explosioneffect;
var outofarea;
var speedtoland;
var landheightrefine;
var levelwinimg;
var anglebadimg;
var speedbadimg;
function preload() {
// Game Essentials
bgmus = loadSound('BGMusicLQ.mp3'); // Background Music
explodesfx = loadSound('explode.mp3'); //Explosion SFX
fireworksfx = loadSound('firework.mp3'); //Fireworks SFX
startscreen = loadImage('title.png'); // Screen Image
cloudbg = loadImage('cloudbg2.png'); // BG with Logo (cloudbg2 = new version)
introvid = createVideo('StartLevel2_newbg.mp4'); // Welcome Video Playback (StartLevel2_newbg = new version)
rocketimg = loadImage("rocket1.png"); // Rocket Sprite
rocketfiredimg = loadImage("rocket1-fired.png"); // Rocket Sprite
landerimg = loadImage("lander1.png"); // Lander Sprite
movedtoofast = loadImage("movetoofast.png"); //Moved too Fast error to load.
mainfontlight = loadFont('Montserrat-Light.ttf'); //Font #1
mainfontextralight = loadFont('Montserrat-ExtraLight.ttf'); //Font #2
mainfontbolditalic = loadFont('Montserrat-BoldItalic.ttf'); //Font #3
explosioneffect = loadGif("explosiontransparent.gif"); //Explosion GIF for Crash Landing
fireworksuccess = loadGif("newfw.gif"); //Fireworks GIF for Successful Landing
outofarea = loadImage("outofarea.png"); // Out-Of-Area Overlay
levelwinimg = loadImage('levelwin.png'); // Level Won Overlay
anglebadimg = loadImage('anglebad.png'); // Bad Angle Overlay
speedbadimg = loadImage('landtoofast.png'); // Speed too Great Overlay
// *** Game Parameters - Adjust these as required for testing etc ***
simstage = "intro"; //Stage that game launches at. Default: "intro"
gravitypower = 0.05; // Downward force on rocket. Default: "0.05"
thrustpower = 2.0; // Thrust Power of rocket. Default: "2.0="
thrustpowerside = 0.2; //Thrust Power of rocket with side controls. Default: "0.2"
rocketangle = 4; // Angle that L/R controls rotate by. Default: "4"
landerxpos = 800; // X-Coord. Position of Lander on Screen
landerypos = 740; // Y-Coord. Position of Lander on Screen
landersizeparam = 50; // Used in calculations of how close the rocket can be to win. *** check if still required..
closetoland = 50; // How close the rocket must be to the lander for a successful landing. Centre - Centre Distance. Decrease to make harder.
topspeedrocket = 10; // Used to later control rocket speeds.
speedtoland = 5; //Max speed to match with distance param. for a successful landing.
landheightrefine = 655; // Used during debug to ensure rocket lands at top of screen.
}
function setup() {
frameRate(30); // Reduces lag and is less processor intensive when restricted to 30fps.
angleMode(DEGREES); // Change setting to degress for all angles.
createCanvas(1024, 768); // 1024x768 resolution as specified in brief.
background(100); // Plain BG colour
rocket = new rocket(); //Sets up rocket element
lander = new lander(); //Sets up lander element
}
function draw() {
if (bgmus.isPlaying() == false) { // Starts the soundtrack if stopped
bgmus.play();
}
if (simstage == "intro") { //Determine which level to draw based on the current stage
draw_welcome();
} else if (simstage == "level1") {
draw_level1();
} else if (simstage == "level2") {
draw_level2();
}
}
function draw_welcome() {
imageMode(CENTER); // Centre all images
image(startscreen, 512, 384) //Load start screen image
textFont(mainfontlight); // Choose font
fill('white'); // Font Colour
textSize(12); // Font Size
text('© Jon Bourne 2018', 30, 500); // Footer Info
text('DMA1010 Coursework Assignment', 30, 520); // Footer Info
text('V0.8', 30, 540); // Footer Info
}
function draw_level1() {
imageMode(CENTER); // Centre all images
image(introvid, 512, 384); // Draw the video frame to canvas
introvid.play(); //Play Video
}
function draw_level2() {
introvid.hide(); // Hide video
image(cloudbg, 512, 384) // Load BG Frame
rocket.move(); // Rocket becomes responsive to movement
rocket.apply_gravity(); // Gravity applies to Rocket
// Rocket Stats Window
fill(106, 215, 242, 230); // Colour RGB + Alpha
rect(800, 10, 120, 70); // Draw Rectangle
textSize(18); // Text Size
textFont(mainfontbolditalic); // Text Font
fill('white'); // Text Colour
text('Rocket Info...', 800, 100); // Title for window (below box as in brief)
textFont(mainfontextralight); // Text Font for info box
textSize(14); // Text Size
fill('black'); // Text Colour
text('X-Vel = ' + int(rocket.speedx)*100, 810, 50); // X-Velocity Readout
text('Y-Vel = ' + int(rocket.speedy)*100, 810, 70); // Y-Velocity Readout
text('Dist = ' + nf(dist(rocket.x, rocket.y, lander.x, lander.y), 3, 1), 810, 30); // Rocket-Lander Dist. to 4 sig. fig.
// draw lander
lander.draw(); // Execute draw commands
// draw ship
rocket.draw(); // Execute draw commands
if (abs(rocket.speedy) < speedtoland && abs(rocket.speedx) < 1.0 && abs(rocket.angle) < 10 && rocket.x < 980 && 620 < rocket.x && landheightrefine < rocket.y) {
//***Successful Landing***
image(fireworksuccess, 512, 384, 1024, 768); // BG Fireworks
image(levelwinimg, 512, 384); // Congratulations Messag
rocket.speedx = 0 //Prevent Rocket Moving
rocket.speedy = 0 //Prevent Rocket Moving
rocket.gravity = 0 //Prevent Rocket Moving
if (fireworksfx.isPlaying() == false) { // Starts the explosion if stopped
fireworksfx.play(); // *
}
} else if (abs(rocket.speedy) < speedtoland && abs(rocket.angle) > 10 && rocket.x < 980 && 620 < rocket.x && landheightrefine < rocket.y) {
//***Failed Landing - Angle***
image(cloudbg, 512, 384) //Load BG Frame to blank canvas
image(landerimg, landerxpos, landerypos); //Load Lander
image(explosioneffect, landerxpos, landerypos); //Load Explosion GIF
image(anglebadimg, 512, 384);
rocket.speedx = 0
rocket.speedy = 0
rocket.gravity = 0
explosioneffect.play();
if (explodesfx.isPlaying() == false) { // Starts the explosion if stopped
explodesfx.play(); // *
}
} else if (abs(rocket.speedy) > speedtoland && abs(rocket.angle) > 10 && rocket.x < 980 && 620 < rocket.x && landheightrefine < rocket.y) {
//***Failed Landing - Angle (too quick also) ***
image(cloudbg, 512, 384) //Load BG Frame to blank canvas
image(landerimg, landerxpos, landerypos); //Load Lander
image(explosioneffect, landerxpos, landerypos); //Load Explosion GIF
image(anglebadimg, 512, 384);
rocket.speedx = 0
rocket.speedy = 0
rocket.gravity = 0
explosioneffect.play();
if (explodesfx.isPlaying() == false) { // Starts the explosion if stopped
explodesfx.play(); // *
} else if (abs(rocket.speedy) > speedtoland && abs(rocket.angle) < 10 && rocket.x < 980 && 620 < rocket.x && landheightrefine < rocket.y) {
//***Failed Landing - Y-Dir Speed***
image(cloudbg, 512, 384) //Load BG Frame to blank canvas
image(landerimg, landerxpos, landerypos); //Load Lander
image(explosioneffect, landerxpos, landerypos); //Load Explosion GIF
image(speedbadimg, 512, 384); // Overspeed Warning
explosioneffect.play();
if (explodesfx.isPlaying() == false) { // Starts the explosion if stopped
explodesfx.play(); // *
} else if (abs(rocket.speedy) < speedtoland && abs(rocket.speedx) > 1.0 && abs(rocket.angle) < 10 && rocket.x < 980 && 620 < rocket.x && landheightrefine < rocket.y) {
//***Failed Landing - Y-Dir Speed***
image(cloudbg, 512, 384) //Load BG Frame to blank canvas
image(landerimg, landerxpos, landerypos); //Load Lander
image(explosioneffect, landerxpos, landerypos); //Load Explosion GIF
image(speedbadimg, 512, 384);
explosioneffect.play();
if (explodesfx.isPlaying() == false) { // Starts the explosion if stopped
explodesfx.play(); // *
}
}
}
}
if (rocket.speedx < -topspeedrocket || rocket.speedx > topspeedrocket) { //adj final var
image(movedtoofast, 512, 384); // Display 'Moved too Fast' crash message (general for anywhere in play area)
noLoop(); //End Game
}
}
// rocket object
function rocket() {
this.x = random(30, 300); //Start Xpos - random top left
this.y = random(100, 210); //Start Ypos - random top left
this.size = 50; //Size of rocket. Possibly obsolete? check
this.speedx = 0; //Starting speed. Later altered.
this.speedy = 0; //Starting speed. Later altered.
this.gravity = gravitypower; //Pulls values from central variables.
this.angle = 0; //Initial starting angle, 0deg.
// rocket methods - what it does, here drawing itself.
this.draw = function() {
push()
translate(this.x + 2.5, this.y + 1.5);
rotate(this.angle);
translate(-this.x - 2.5, -this.y - 1.5);
if (keyIsDown(LEFT_ARROW) || keyIsDown(RIGHT_ARROW) || keyIsDown(UP_ARROW)) { //If L/R/U keys are used, rocket will 'fire' instead of being static.
image(rocketfiredimg, this.x, this.y, 60, 356); //Fire Image
} else image(rocketimg, this.x, this.y, 60, 356); //No-Fire Image
pop(); //NEWNEWNEW
}
// move ship
this.move = function() {
this.x += this.speedx; //not just changing x and y by a number, but by the speed instead.
this.y += this.speedy;
// Out of area once rocket substantially leaves play area.
if (this.x < -50 || this.x > width + 50) { //X-Dir Variant
image(outofarea, 512, 384); // Display 'Out of Area' crash message.
noLoop(); //End Game
} else if (this.y < -100 || this.y > height + 200) { //Y-Dir Variant
image(outofarea, 512, 384); // Display 'Out of Area' crash message.
noLoop(); //End Game
}
}
// apply gravity
this.apply_gravity = function() {
this.speedy += this.gravity; // Gravity effect on y-dir speed.
}
}
// lander object
function lander() {
// lander data - this should be altered to match (!)
this.x = landerxpos;
this.y = landerypos;
this.size = landersizeparam;
// the actual object it lands on
this.draw = function() {
imageMode(CENTER);
image(landerimg, this.x, this.y);
}
}
function keyPressed() {
//ROCKET CONTROL ONLY
if (keyCode == LEFT_ARROW) {
rocket.speedx -= thrustpowerside; //Speed alteration
rocket.angle -= rocketangle; //Alter angle by preset variable.
} else if (keyCode == RIGHT_ARROW) {
rocket.speedx += thrustpowerside; //Speed alteration
rocket.angle += rocketangle; //Alter angle by preset variable.
} else if (keyCode == UP_ARROW) {
rocket.speedy -= thrustpower; //Speed alteration
} else if (keyCode == DOWN_ARROW) {
rocket.speedy += thrustpower; //Speed alteration
//GAME UI CONTROL ONLY
} else if (simstage == "intro") {
simstage = "level1" //move to level 1 on key press
draw_level1(); //start the level 1 scene
} else if (simstage == "level1") {
simstage = "level2"
draw_level2(); //start the level 2 scene
}
return false; // prevent default
} //key controls