CodeHS
About
Tutors
Jobs
Testimonials
Demos
FAQ
Schools
Library
Plans + Sign Up
Login
var MAX = 12; var TERRAIN_WIDTH = 10; var MIN_TERRAIN_HEIGHT = getHeight()/25; var MAX_TERRAIN_HEIGHT = getHeight()/10; var NUM_OBSTACLES = 4; var DUST_Y_BUFFER = 10; var OBSTACLE_WIDTH = 30; var pressed; var flying; var plane; var obstacles; var score; var scoreLabel; var clickToStart; var dust; function start(){ addMenu(); obstacles = []; dust = []; score = 0; flying = 0; pressed = false; mouseDownMethod(mousePressed); mouseUpMethod(mouseUp); waitForClick(); setTimer(game, 40); setBackgroundColor(Color.black); plane = new WebImage(ImageLibrary.Objects.helicopter); plane.setSize(50, 25); plane.setPosition(getWidth()/3, getHeight()/2); add(plane); createTerrain(); scoreLabel = new Text(score); scoreLabel.setPosition(10, 30); scoreLabel.setColor(Color.white); add(scoreLabel); } function addMenu() { clickToStart = new Text("Click to Start"); clickToStart.setPosition(getWidth()/2 - clickToStart.getWidth()/2, getHeight()/3); clickToStart.setColor(Color.white); add(clickToStart); } function createTerrain() { for (var i = 0; i < getWidth()/TERRAIN_WIDTH + 2; i++) { var rect = new Rectangle(TERRAIN_WIDTH, Randomizer.nextInt(MIN_TERRAIN_HEIGHT, MAX_TERRAIN_HEIGHT)); rect.setPosition(TERRAIN_WIDTH*i, 0); add(rect); rect.setColor(Color.green); obstacles.push(rect); var height = Randomizer.nextInt(MIN_TERRAIN_HEIGHT, MAX_TERRAIN_HEIGHT); var rect = new Rectangle(TERRAIN_WIDTH, height); rect.setPosition(TERRAIN_WIDTH*i, getHeight() - height); rect.setColor(Color.green); add(rect); obstacles.push(rect); } for (var i=0; i < NUM_OBSTACLES; i++) { var height = getHeight()/5 + Randomizer.nextInt(getHeight()/50, getHeight()/7); var rect = new Rectangle(OBSTACLE_WIDTH, height); var x = getWidth() + (3 * i * getWidth()) / 4; var y = Randomizer.nextInt(height, getHeight() - height); rect.setPosition(x, y); rect.setColor(Color.green); add(rect); obstacles.push(rect); } } function mousePressed(e) { pressed = true; } function mouseUp(e) { pressed = false; } function updateScore() { scoreLabel.setLabel(score); } function lose() { stopTimer(game); var lose = new Text("You Lose! Play Again?", "12pt Courier"); lose.setColor(Color.red); lose.setPosition(getWidth()/2 - lose.getWidth()/2, getHeight()/3); add(lose); waitForClick(); setTimer(playAgain, 100); } function playAgain() { stopTimer(playAgain); removeAll(); start(); } function outOfBounds(){ var hit_bottom = plane.getY() > getHeight(); var hit_top = plane.getY() < 0; return hit_bottom || hit_top; } function game() { score += 5; updateScore(); remove(clickToStart); collider = getCollision(); if (collider != null && collider != plane && collider) { lose(); return; } if(outOfBounds()){ return lose(); } if (pressed) { flying -= 1; if (flying < -1*MAX) { flying = -1*MAX; } } else { flying += 1; if (flying > MAX) { flying = MAX; } } plane.move(0, flying); addDust(); moveObstacles(); } function getCollision() { //top left var elem = getElementAt(plane.getX()-1, plane.getY()-1); if (elem != null) { return elem; } //bottom left var elem = getElementAt(plane.getX()-1, plane.getY() + plane.getHeight() + 1); if (elem != null) { return elem; } //top right var elem = getElementAt(plane.getX() + plane.getWidth() + 1, plane.getY()-1); if (elem != null) { return elem; } //bottom right var elem = getElementAt(plane.getX() + plane.getWidth() + 1, plane.getY() + plane.getHeight() + 1); if (elem != null) { return elem; } return null; } function addDust() { var DUST_RADIUS = 3; var d = new Circle(DUST_RADIUS); d.setPosition(plane.getX() + DUST_RADIUS, plane.getY() + DUST_Y_BUFFER); d.setColor("#cccccc"); dust.push(d); add(d); } function moveObstacles() { for (var i = 0; i < obstacles.length; i++) { var obs = obstacles[i]; obs.move(-5, 0); if (obs.getX() < -obs.getWidth()) { if (obs.getY() > 0 && obs.getY() < getHeight()-50) { var nextY = Randomizer.nextInt(obs.getHeight(), getHeight() - obs.getHeight()); obs.setPosition( Math.max(getWidth()*(NUM_OBSTACLES-1) - score/10, getWidth()), nextY); } else { obs.setPosition(getWidth(), obs.getY()); } } } for (var i = 0; i < dust.length; i++) { var d = dust[i]; d.move(-5, 0); d.setRadius(d.getRadius()-0.1); if (d.getX() < -d.getWidth()) { remove(d); dust.splice(dust.indexOf(d), 1); } } }
Helicopter
Your browser doesn't support canvas.
Hold the mouse button down to go up. Avoid the obstacles.
Share This Program
Tweet
I Want to Learn!
Sign up for the free trial of CodeHS