Skip to content

Instantly share code, notes, and snippets.

@AidanNelson
Created October 10, 2017 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AidanNelson/5a5d90fd9be049eba7b6f9a679bd73dd to your computer and use it in GitHub Desktop.
Save AidanNelson/5a5d90fd9be049eba7b6f9a679bd73dd to your computer and use it in GitHub Desktop.
/*
2017.10.09 -- Aidan Nelson
A quick and dirty clone of the helicopter game as I remember playing it in
middle school math class on ebaumsworld -- circa 2002. For Intro to Computational
Media at ITP.
Questions that came up:
1. should helicopter controls go into helicopter class?
2. should the function returning an intersection go into the wall or helicopter
class?
3. how to monitor an ongoing keypress?
4. can an object delete itself?
5. calling 'height' and 'width' from within class OK ?
6. how to space out walls properly with increasing speed?
*/
////////////////////////////////////////////
//variable for helicopter object
let heli;
//variable for gamestate
let gameOver = false;
let isFirstGo = true;
//game speed and max speed
let speed = 0;
//empty array to hold the walls
let walls = [];
//wall parameters
let wallHeight = 200;
let wallWidth = 50;
//variable for background color
let bg_col;
//difficulty should be a value between 0 and 10, higher numbers being more difficult
let difficulty = 14;
let framesElapsed = 0;
////////////////////////////////////////////
function setup() {
createCanvas(600, 400);
//create the helicopter object
heli = new Heli();
//define background colors for normal gamestate
bg_col = color(119, 151, 198);
//start off with a set of short walls:
for (let x = 0; x < 50; x++) {
//top walls
let shortWallWidth = width / 50;
walls.push(new Wall(speed, x * shortWallWidth, 0, shortWallWidth, random(20, 30)));
//bottom walls
let wh = random(20, 30); // wall height
walls.push(new Wall(speed, x * shortWallWidth, height - wh, shortWallWidth, wh));
framesElapsed = 10;
}
}
function draw() {
//display the game
displayGame();
//update the game
updateGame();
}
function displayGame() {
if (isFirstGo) { //splash screen
//text box
fill(119, 151, 198);
rectMode(CENTER);
rect(width / 2, height / 2, width, height);
//text
textSize(50);
textAlign(CENTER, CENTER);
fill(255);
text("HELICOPTER", width / 2, height / 2 - 10);
textSize(20);
text("space to play", width / 2, height / 2 + 30);
} else if (gameOver) { //special parameters for game over state
//stop game and set color
speed = 0;
bg_col = color(255, 0, 0);
//text box
fill(0);
rectMode(CENTER);
rect(width / 2, height / 2, width, 100);
//text
textSize(50);
textAlign(CENTER, CENTER);
fill(255);
text("GAME OVER", width / 2, height / 2 - 10);
textSize(20);
text("space to resume", width / 2, height / 2 + 30);
} else { //play state
//display the background as bg_col;
background(bg_col);
//display the helicopter every frame
heli.run();
//iterate through walls list backwards
for (let i = walls.length - 1; i >= 0; i--) {
//run the walls
walls[i].run();
//splice from the array those walls which are out of the screen
if (walls[i].isOut()) {
walls.splice(i, 1);
}
}
}
}
function updateGame() {
//check if the game is over:
checkIfGameOver();
if (isFirstGo) {
if (keyIsDown(32)) {
resetGame();
isFirstGo = false;
}
} else if (gameOver) { //check for gameover state
heli.stop();
//space to resume game
if (keyIsDown(32)) {
resetGame();
}
} else { //game is not over!
// add a new wall on right of window every time a wall...
if (framesElapsed * speed >= wallWidth) {
addWalls();
framesElapsed = 0;
}
//check for keypresses!
if (keyIsDown(32)) {
heli.yspeed -= 1;
}
}
//move frame marker:
framesElapsed += 1;
}
function checkIfGameOver() {
//iterate throug walls list backwards, checking if ball is in each wall
for (let i = walls.length - 1; i >= 0; i--) {
if (heli.isIn(walls[i])) {
gameOver = true;
}
}
}
//add walls
function addWalls() {
//top walls
walls.push(new Wall(speed, windowWidth, 0, wallWidth, random(20, 10 * difficulty)));
//bottom walls
let wh = random(20, 10 * difficulty); // wall height
walls.push(new Wall(speed, windowWidth, height - wh, wallWidth, wh));
}
function resetGame() {
gameOver = false;
walls.splice(0, walls.length);
speed = 5;
bg_col = color(119, 151, 198);
//reset helicopter to starting state
heli.reset();
//start off with a set of short walls:
for (let x = 0; x < 50; x++) {
//top walls
let shortWallWidth = width / 50;
walls.push(new Wall(speed, x * shortWallWidth, 0, shortWallWidth, random(20, 30)));
//bottom walls
let wh = random(20, 30); // wall height
walls.push(new Wall(speed, x * shortWallWidth, height - wh, shortWallWidth, wh));
framesElapsed = 10;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment