// Variables
var score = 0;
var lives = 3;
// Create Sprites
var platform1 = createSprite(randomNumber(80, 310), 50);
platform1.setAnimation(“platform”);
platform1.velocityY = 1;
platform1.scale = 0.4;
var platform2 = createSprite(randomNumber(80, 310), 250);
platform2.setAnimation(“platform”);
platform2.velocityY = 1;
platform2.scale = 0.4;
var star1 = createSprite(randomNumber(50, 350), randomNumber(-40, -60));
star1.setAnimation(“star”);
star1.velocityY = 2.5;
star1.scale = 0.5;
star1.setCollider(“circle”);
var star2 = createSprite(randomNumber(50, 350), randomNumber(-30, -60));
star2.setAnimation(“star”);
star2.velocityY = 2.5;
star2.scale = 0.5;
star2.setCollider(“circle”);
var alien = createSprite(200, -30);
alien.setAnimation(“alien”);
var rock1 = createSprite(randomNumber(50, 350), -250);
rock1.setAnimation(“rock”);
rock1.velocityY = 1.5;
rock1.rotationSpeed = -6;
rock1.setCollider(“circle”);
var rock2 = createSprite(randomNumber(50, 350), -250);
rock2.setAnimation(“rock”);
rock2.velocityY = 1.5;
rock2.rotationSpeed = 6;
rock2.setCollider(“circle”);
function draw() {
// draw the background
background1();
// update the sprites
if (score > 20) {
background2();
showScore();
} else {
background1();
showScore();
}
loopPlatforms();
loopItems();
playerFall();
controlPlayer();
playerLands();
collectItems();
alienReset();
showLives();
rockReset();
if (lives < 1) {
background3();
}
if (rock1.isTouching(alien)) {
alien.x = 200;
alien.y = -30;
lives = lives-1;
}
if (rock2.isTouching(alien)) {
alien.x = 200;
alien.y = -30;
lives = lives-1;
}
if (rock1.isTouching(rock2)) {
rockReset();
}
if (platform1.isTouching(platform2)) {
platform1.x = randomNumber(80, 310);
platform1.y = -350;
platform2.x = randomNumber(80, 310);
platform2.y = -350;
}
if (star1.isTouching(star2)) {
star1.x = randomNumber(50, 350);
star1.y = randomNumber(-30,-60);
star2.x = randomNumber(50, 350);
star2.y = randomNumber(-30,-60);
}
drawSprites();
}
// Functions
function background1() {
background(“darkBlue”);
noStroke();
fill(“yellow”);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(randomNumber(0, 400), randomNumber(0, 400), 3, 3);
ellipse(340, 50, 60, 60);
fill(“darkBlue”);
ellipse(320, 30, 60, 60);
}
function background2() {
background(“purple”);
stroke(“darkGreen”);
fill(“green”);
rect(randomNumber(0, 400), randomNumber(0, 400), 30, 30);
rect(randomNumber(0, 400), randomNumber(0, 400), 30, 30);
}
function showScore() {
fill(“white”);
textSize(20);
text("Score: ",10, 10, 80, 20);
text(score, 75, 30);
}
function showLives() {
fill(“white”);
textSize(20);
text("Lives: ", 10, 37, 80, 20);
text(lives, 70, 57);
}
function loopPlatforms() {
if (platform1.y>450) {
platform1.y = -10;
platform1.x = randomNumber(80, 310);
}
if (platform2.y>450) {
platform2.y = -10;
platform2.x = randomNumber(80, 310);
}
}
function loopItems() {
if (star1.y > 410) {
star1.y = randomNumber(-30, -60);
star1.x = randomNumber(50, 350);
}
if (star2.y > 410) {
star2.y = randomNumber(-30, -60);
star2.x = randomNumber(50, 350);
}
}
function playerFall() {
alien.velocityY = alien.velocityY+0.5;
}
function controlPlayer() {
if (keyDown(“left”)) {
alien.x = alien.x-3;
alien.setAnimation("alienleft");
}
if (keyDown(“right”)) {
alien.setAnimation("alien");
alien.x = alien.x+3;
}
if (keyDown(“up”)) {
alien.velocityY = alien.velocityY=-5;
}
}
function playerLands() {
alien.collide(platform1);
alien.collide(platform2);
}
function collectItems() {
if (alien.isTouching(star1)) {
score = score+1;
star1.y = randomNumber(-30, -60);
star1.x = randomNumber(50, 350);
}
if (alien.isTouching(star2)) {
score = score+1;
star2.y = randomNumber(-30, -60);
star2.x = randomNumber(50, 350);
}
}
function alienReset() {
if (alien.y > 410) {
alien.x = 200;
alien.y = -40;
lives = lives-1;
}
}
function background3() {
background(“red”);
fill(“blue”);
text(“Game Over”, 150, 210, 400, 400);
textSize(300);
}
function rockReset() {
if (rock1.y > 410) {
rock1.x = randomNumber(50, 350);
rock1.y = -250;
}
if (rock2.y > 410) {
rock2.x = randomNumber(50, 350);
rock2.y = -250;
}
}