//screen controls var screen = 1; var back = createSprite(200, 200); back.setAnimation("welcome"); fill("white"); textSize(30); //game sprites var ufo = createSprite(0, 500); ufo.setAnimation("ufo"); ufo.scale = 0.2; ufo.velocityX = 0; ufo.velocityY = 0; var powerUp = createSprite(0, 500); powerUp.setAnimation("powerupYellow_bolt_1"); var astro = createSprite(0, 500); astro.setAnimation("sticker_34_1"); astro.scale = 0.3; createEdgeSprites(); //game variables var timer = 500; //time to rescue var fuel = 5; //fuel remaining function draw() { //button to move between screens if (keyWentDown("q")) { if (screen == 1) { //if on screen 1, move to #2-rules screen=2; back.setAnimation("rules"); } else if ((screen == 2)) { //if on screen 2, move to #3-game screen=3; back.setAnimation("background"); startGame(); } else { //if on screen 4 or 5, restart //move to #1-welcome screen=1; back.setAnimation("welcome"); endGame(); } } drawSprites(); //if on game screen, play game if(screen==3){ playGame(); } } function startGame(){ //move all sprites up from holding area to screen ufo.velocityX=-4; ufo.velocityY=4; ufo.x=320; ufo.y=120; astro.x=100; astro.y=200; powerUp.x=350; powerUp.y=300; fuel=5; timer=500; } function endGame(){ //move all sprites down to screen 3 holding area ufo.velocityX=0; ufo.velocityY=0; ufo.x=0; ufo.y=500; astro.x=0; astro.y=500; powerUp.x=0; powerUp.y=500; } function playGame(){ //move astronaut if (keyDown("up")) { astro.y -= 5; } else if ((keyDown("down"))) { astro.y += 5; } else if ((keyDown("left"))) { astro.x -= 5; } else if ((keyDown("right"))) { astro.x += 5; } //ufo movememnt & collison (lose fuel) ufo.bounceOff(edges); if (ufo.isTouching(astro)) { fuel--; ufo.x=350; ufo.y=350; } //pick up fuel if (astro.isTouching(powerUp)) { fuel++; powerUp.x=randomNumber(25, 375); powerUp.y=randomNumber(25, 375); } //handle time passing timer--; //if the time is done, you win if(timer<=0){ //move to screen #4-win //move off the game sprites back.setAnimation("win"); screen=4; timer=500; endGame(); } //if the fuel is done, you lose if(fuel<=0){ //move to screen #5-lose //move off the game sprites endGame(); back.setAnimation("gameOver"); screen=5; } //update fuel and time on screen text("Fuel: "+fuel+" Time to Rescue: "+timer,15,30); }