Bounce Game in code.
org
var lives;
var score;
var xSpeed;
var ySpeed;
var ballX;
var ballY;
var ballDiameter = 26;
var paddleSpeed;
var paddleX;
var paddleY = 425;
var paddleWidth = 90;
var paddleHeight = 24;
var coinX;
var coinY = 65;
var coinDiameter = 50;
var moveLeft;
var moveRight;
var isReset;
var canBounce;
onEvent("startbutton", "click", function(event) {
setScreen("game");
lives = 2;
score = 0;
reset();
timedLoop(17, update);
});
onEvent("game", "keydown", function(event) {
if (event.key == "Left") {
moveLeft = true;
} else if (event.key == "Right") {
moveRight = true;
});
onEvent("game", "keyup", function(event) {
if (event.key == "Left") {
moveLeft = false;
} else if (event.key == "Right") {
moveRight = false;
});
onEvent("game", "keypress", function(event) {
if (event.key == " " && isReset) {
startGame();
});
onEvent("playagain", "click", function(event) {
setScreen("start");
});
function update() {
movePaddle();
checkBounce();
collectCoin();
ballX += xSpeed;
ballY += ySpeed;
paddleX += paddleSpeed;
// update positions of everything:
setText("score", score);
setText("lives", lives);
setPosition("ball", ballX, ballY);
setPosition("paddle", paddleX, paddleY);
setPosition("coin", coinX, coinY);
function checkBounce() {
if (ballX + ballDiameter > 320 || ballX < 0) {
xSpeed = -xSpeed;
} else if (ballY < 0 && !canBounce) {
ySpeed = -ySpeed;
canBounce = true;
if (checkCollision(ballX, ballY, ballDiameter, ballDiameter, paddleX, paddleY, paddleHeight,
paddleWidth) && canBounce) {
canBounce = false;
ySpeed = -ySpeed;
if (ballY + ballDiameter > 450) {
lives = lives - 1;
reset();
function movePaddle() {
if (moveLeft && !moveRight && paddleX > 0) {
paddleSpeed = -5;
else if (moveRight && !moveLeft && paddleX + paddleWidth < 320) {
paddleSpeed = 5;
else {
paddleSpeed = 0;
function collectCoin() {
if (checkCollision(ballX, ballY, ballDiameter, ballDiameter, coinX, coinY, coinDiameter, coinDiameter)) {
score++;
playSound("hitsound.mp3", false);
coinX = 500; // move the coin off screen for a while once it gets hit
// increase speed of the ball every time a coin is collected:
if (ySpeed < 0 && ySpeed > -15) {
ySpeed--;
else if (ySpeed > 0 && ySpeed < 15) {
ySpeed++;
if (xSpeed < 0 && xSpeed > -15) {
xSpeed--;
else if (xSpeed > 0 && xSpeed < 15) {
xSpeed++;
setTimeout(function() {
coinX = randomNumber(0, 320-coinDiameter);
}, 1500);
function reset() {
xSpeed = 0;
ySpeed = 0;
paddleSpeed = 0;
moveLeft = false;
moveRight = false;
isReset = true;
showElement("startText");
ballX = 147;
ballY = 212;
paddleX = 115;
coinX = 135;
canBounce = true;
if (lives == -1) {
endGame();
function startGame() {
hideElement("startText");
while (xSpeed === 0) { // make sure starting xSpeed can't be zero
xSpeed = randomNumber(-5,5);
ySpeed = randomNumber(3, 5);
isReset = false;
function endGame() {
stopTimedLoop();
setText("finalscore", score);
setScreen("gameover");
function checkCollision(x1, y1, h1, w1, x2, y2, h2, w2) {
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
}
Sprite Lab
var Score = 0;
World.frameRate = 200;
drawWorld();
movingYoda();
// Create your variables here
var lilyoda = createSprite(60, 350);
lilyoda.setAnimation("51N+0bihAKL.jpg_1");
lilyoda.scale = 0.2;
var sun = createSprite(40, 40);
sun.setAnimation("sun_2");
var cloud = createSprite(245, 98);
cloud.setAnimation("cloud_1");
cloud.scale = 0.4;
var cloudagain = createSprite(325, 40);
cloudagain.setAnimation("cloud_1_copy_1");
cloudagain.scale = 0.4;
var bonebroth = createSprite(260, 330);
bonebroth.setAnimation("bone broth.jpg_1");
bonebroth.scale = 0.2;
// Create your sprites here
function draw() {
background(rgb (135,206,235));
textSize(9);
fill(rgb(0, 0, 0, 0));
text("Press W, A, S, and D to move!", 180, 20);
text("Collect the Bone Broth to make Baby Yoda stronger!", 115, 175);
text("Score", 125, 140);
text(Score, 150, 140);
var barrier = createSprite(145, 120);
barrier.setAnimation("reee.jpg_1");
barrier.visible = 0;
var barrieragain = createSprite(200, 400);
barrieragain.setAnimation("reee.jpg_1_copy_1");
barrieragain.height = 0.1;
barrieragain.visible = 0;
if (lilyoda.isTouching(barrier)) {
lilyoda.velocityY = lilyoda.velocityY + 2;
lilyoda.y = lilyoda.y + 2;
if (lilyoda.isTouching(barrieragain)) {
lilyoda.bounce(barrieragain);
drawWorld();
movingYoda();
scores();
enemies();
drawSprites();
}
function drawWorld() {
// draw background
// update sprites
// Create your functions here
function movingYoda() {
if (keyDown("a")) {
lilyoda.x = lilyoda.x - 2;
if (keyDown("d")) {
lilyoda.x = lilyoda.x + 2;
if (keyDown("w")) {
lilyoda.y = lilyoda.y - 2;
if (keyDown("space")) {
lilyoda.y = lilyoda.y - 2;
if (keyDown("s")) {
lilyoda.y = lilyoda.y + 2;
}
function scores() {
if (lilyoda.isTouching(bonebroth)) {
bonebroth.x = randomNumber(0, 400);
bonebroth.y = randomNumber(250, 350);
if (lilyoda.isTouching(bonebroth)) {
Score = Score + 1;
function enemies() {
if (Score >= 15) {
lilyoda.setAnimation("1_copy_1");
if (Score >= 25) {
text("Baby Yoda Is Growing Up!", 180, 195);
if (Score >= 50) {
sun.visible = 0;
cloud.visible = 0;
cloudagain.visible = 0;
bonebroth.visible = 0;
background("white");
textSize(20);
text("Baby Yoda has grown up!", 160, 220);
text("You Won!", 180, 240);
text("Meet Yoda!", 180, 260);
lilyoda.setAnimation("ausitmn");
drawSprites();
Final Clicker Game
setScreen("screen1");
setKeyValue("highscoreTime", 10, function () {
});
var newRecord = ("getUserId");
createCanvas("canvas", 320, 450);
setPosition("canvas", 0, 25, 320, 425);
drawImageURL("http://www.charlierestivo.com/images/NiGHTS/JoD/mydream_n_003_nc.png");
textLabel("goal", "Smash the mole (click).");
image("bug", "http://www.wpclipart.com/cartoon/animals/mouse_rat/rodent/mole_cartoon.svg");
setPosition("bug", 140, 240, 80, 80);
var clicks = 0;
textLabel("score", clicks + " moles smashed.");
console.log(getTime());
var startTime=getTime();
var elapsedTime=((getTime()-startTime)/1000);
textLabel("winner", elapsedTime + " seconds to smash 10 moles.");
hideElement("winner");
var fastestTime=startTime;
textLabel("highscore", "New highscore!");
hideElement("highscore");
onEvent("bug", "click", function(event) {
clicks = clicks + 1;
setPosition("bug", randomNumber(0, 280), randomNumber(60, 415), 80, 80);
setText ("score", clicks + " moles smashed.");
if (clicks==10) {
hideElement("bug");
var elapsedTime=((getTime()-startTime)/1000);
setText("winner", elapsedTime + " seconds to smash 10 moles");
showElement("winner");
console.log((getTime()-startTime)/1000);
showElement("restart");
hideElement("canvas");
getKeyValue("highscoreTime", function (fastestTime) {
});
if (elapsedTime<fastestTime) {
fastestTime=elapsedTime;
showElement("highscore");
console.log('new winner');
setKeyValue("highscoreTime", fastestTime, function () {
});
});
button("restart", "Play again?");
hideElement("restart");
onEvent("restart", "click", function(event) {
hideElement("restart");
hideElement("highscore");
hideElement("winner");
showElement("bug");
showElement("canvas");
clicks=0;
startTime=getTime();
});
getKeyValue("highscoreTime", function (value) {
});
//screen 2
setScreen("screen2");
createCanvas("canvas2", 320, 450);
drawImageURL("");
onEvent("text_input2", "change", function(event) {
console.log("text_input2 entered text: " + getText("text_input2"));
setScreen("screen1");
newRecord.name=getText("text_input2");
});
Snake Game in Applab
setText("instructions", "Press WASD or Arrow Keys to move\n\nPress Spacebar or click Play Again to
play again\n\n\nPress Spacebar to start the game");
setStyle("game", "font-family: Lucida Console;");
var snake = {
x: 0,
y: 0,
w: 10,
h: 10,
velX: 0,
velY: 0,
IDandLength: 1,
moves: [],
didCrash: false
};
var fruit = {
x: 0,
y: 0,
w: 10,
h: 10
};
var ai = {
b: false,
o: false,
t: false,
access: false,
};
var points = {
x: [],
y: []
};
for (var a = 10; a < 310; a += 10)
points.x.push(a);
for (var a = 20; a < 440; a += 10)
points.y.push(a);
var doGameOver = 0;
var startUp = true;
var highscore = 0;
function snakeObj() {
if (ai.access)
aiBot();
if (snake.x > 300 || snake.x < 10 || snake.y > 430 || snake.y < 20)
deathAction();
if (!snake.didCrash) {
if (snake.moves.length == snake.IDandLength + 1)
snake.moves.pop();
snake.moves.splice(0, 0, snake.x + " " + snake.y);
if (snakeCol(fruit.x, fruit.y, fruit.w, fruit.h))
addNewBlock();
setPosition("snake", snake.x, snake.y, snake.w, snake.h);
for (var a = 1; a < snake.IDandLength; a++) {
var coords = snake.moves[a].split(" ");
var x = parseInt(coords[0]);
var y = parseInt(coords[1]);
setPosition("" + a, x, y, 10, 10);
if (snakeCol(x, y, snake.w, snake.h))
deathAction();
snake.x += snake.velX;
snake.y += snake.velY;
function deathAction() {
stopTimedLoop();
snake.didCrash = true;
if (doGameOver == 0) {
doGameOver++;
gameOver();
// inspired from https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection
function snakeCol(x1, y1, w1, h1) {
return x1 < snake.x + snake.w && x1 + w1 > snake.x && y1 < snake.y + snake.h && h1 + y1 > snake.y;
function addNewBlock() {
var coords = snake.moves[snake.IDandLength].split(" ");
var x = parseInt(coords[0]);
var y = parseInt(coords[1]);
image("" + snake.IDandLength, "greenBox.png");
setPosition("" + snake.IDandLength, x, y, 10, 10);
snake.IDandLength++;
fruit.x = points.x[randomNumber(0, points.x.length - 1)];
fruit.y = points.y[randomNumber(0, points.y.length - 1)];
while (xyIsSame(fruit.x, fruit.y)) {
fruit.x = points.x[randomNumber(0, points.x.length - 1)];
fruit.y = points.y[randomNumber(0, points.y.length - 1)];
setPosition("fruit", fruit.x, fruit.y, fruit.w, fruit.h);
setText("scorelabel", "Score: " + snake.IDandLength);
}
function xyIsSame(x, y) {
return snake.moves.indexOf(x + " " + y) >= 0;
function gameOver() {
if (snake.IDandLength > highscore)
highscore = snake.IDandLength;
setText("highscorelabel", "Best: " + highscore);
textLabel("gameOverLabel", "G A M E O V E R");
textLabel("endScoreLabel", "Score: " + snake.IDandLength);
button("playagainButt", "Play Again");
setProperty("gameOverLabel", "text-align", "center");
setProperty("endScoreLabel", "text-align", "center");
setProperty("playagainButt", "text-align", "center");
setProperty("gameOverLabel", "text-color", "white");
setProperty("endScoreLabel", "text-color", "white");
setProperty("playagainButt", "background-color", "#2bbecc");
setPosition("gameOverLabel", 96, 165, 128, 20);
setPosition("endScoreLabel", 110, 185, 100, 20);
setPosition("playagainButt", 110, 215, 100, 30);
setProperty("playagainButt", "font-size", 12);
setStyle("playagainButt", "font-family: Lucida Console; font-weight: bold;");
onEvent("playagainButt", "click", function () {
deleteElement("gameOverLabel");
deleteElement("endScoreLabel");
deleteElement("playagainButt");
hideElement("snake");
hideElement("fruit");
for (var a = 1; a < snake.IDandLength; a++)
hideElement("" + a);
reset();
});
var resetOnce = true;
if (ai.access && resetOnce) {
deleteElement("gameOverLabel");
deleteElement("endScoreLabel");
deleteElement("playagainButt");
hideElement("snake");
hideElement("fruit");
reset();
resetOnce = false;
onEvent("game", "keydown", function (event) {
var key = event.key;
if (key == " " && resetOnce) {
deleteElement("gameOverLabel");
deleteElement("endScoreLabel");
deleteElement("playagainButt");
hideElement("snake");
hideElement("fruit");
for (var a = 1; a < snake.IDandLength; a++)
hideElement("" + a);
reset();
resetOnce = false;
if (key == "r" && resetOnce) {
startUp = true;
ai.access = false;
ai.b = false;
ai.o = false;
ai.t = false;
highscore = 0;
showElement("instructions");
setText("scorelabel", "");
setText("highscorelabel", "");
deleteElement("gameOverLabel");
deleteElement("endScoreLabel");
deleteElement("playagainButt");
hideElement("snake");
hideElement("fruit");
for (var a = 1; a < snake.IDandLength; a++)
hideElement("" + a);
resetOnce = false;
});
function reset() {
setScreen("game");
for (var a = 1; a < snake.IDandLength; a++)
deleteElement("" + a);
snake.IDandLength = 1;
doGameOver = 0;
snake.didCrash = false;
snake.x = points.x[randomNumber(0, points.x.length - 1)];
snake.y = points.y[randomNumber(0, points.y.length - 1)];
snake.moves = [snake.x + " " + snake.y];
fruit.x = points.x[randomNumber(0, points.x.length - 1)];
fruit.y = points.y[randomNumber(0, points.y.length - 1)];
while (xyIsSame(fruit.x, fruit.y)) {
fruit.x = points.x[randomNumber(0, points.x.length - 1)];
fruit.y = points.y[randomNumber(0, points.y.length - 1)];
giveDirection();
setPosition("snake", snake.x, snake.y, snake.w, snake.h);
setPosition("fruit", fruit.x, fruit.y, fruit.w, fruit.h);
setText("scorelabel", "Score: 1");
showElement("snake");
showElement("fruit");
timedLoop(50, snakeObj);
function giveDirection() {
if (snake.x <= 160 && snake.y <= 230) // quadrant 2
if (randomNumber(0, 1) == 0)
down();
else
right();
else if (snake.x < 160 && snake.y > 230) // quadrant 3
if (randomNumber(0, 1) == 0)
up();
else
right();
else if (snake.x >= 160 && snake.y <= 230) // quadrant 1
if (randomNumber(0, 1) == 0)
down();
else
left();
else // quadrant 4
if (randomNumber(0, 1) == 0)
up();
else
left();
function aiBot() {
if (fruit.x == 10 || fruit.x == 300 || fruit.y == 20 || fruit.y == 430) {
if (fruit.x == 10 || fruit.x == 300) {
if (snake.y != fruit.y) {
if (isDown() || isUp())
if (snake.x == 10)
right();
else if (snake.x == 300)
left();
else
if (randomNumber(0, 1) == 0)
left();
else
right();
if (isRight() || isLeft())
if (snake.y > fruit.y)
up();
else if (snake.y < fruit.y)
down();
} else {
if (snake.x == fruit.x)
if (snake.y > 200)
up();
else
down();
else if (fruit.x == 10)
left();
else if (fruit.x == 300)
right();
if (fruit.y == 20 || fruit.y == 430) {
if (snake.x != fruit.x) {
if (isRight() || isLeft())
if (snake.y == 20)
down();
else if (snake.y == 430)
up();
else
if (randomNumber(0, 1) == 0)
down();
else
up();
if (isDown() || isUp())
if (snake.x > fruit.x)
left();
else if (snake.x < fruit.x)
right();
} else {
if (snake.y == fruit.y)
if (snake.x > 160)
left();
else
right();
else if (fruit.y == 20)
up();
else if (fruit.y == 430)
down();
} else {
if (snake.x > fruit.x && !isLeft()) {
if (!isDown() && snake.y > fruit.y - 10)
up();
else if (isDown() && snake.y > fruit.y - 10)
left();
if (!isUp() && snake.y < fruit.y + 10)
down();
else if (isUp() && snake.y < fruit.y + 10)
left();
if (snake.y == fruit.y)
left();
if (snake.x < fruit.x && !isRight()) {
if (!isDown() && snake.y > fruit.y - 10)
up();
else if (isDown() && snake.y > fruit.y - 10)
right();
if (!isUp() && snake.y < fruit.y + 10)
down();
else if (isUp() && snake.y < fruit.y + 10)
right();
if (snake.y == fruit.y)
right();
if (snake.x == fruit.x) {
if (!isDown() && snake.y > fruit.y + 10)
up();
else if (isDown() && snake.y > fruit.y + 10)
if (snake.x == 10)
right();
else if (snake.x == 300)
left();
if (!isUp() && snake.y < fruit.y - 10)
down();
else if (isUp() && snake.y < fruit.y - 10)
if (snake.x == 10)
right();
else if (snake.x == 300)
left();
onEvent("game", "keydown", function (event) {
var key = event.key;
if (startUp) {
if (key == " ") {
startUp = false;
hideElement("instructions");
setText("highscorelabel", "Best: 1");
reset();
} else {
if (key != "b" && key != "o" && key != "t") {
ai.b = false;
ai.o = false;
ai.t = false;
ai.access = false;
if (key == "b")
ai.b = true;
if (ai.b && key == "o")
ai.o = true;
if (key == "t")
if (ai.o)
ai.t = true;
else
ai.b = false;
if (ai.b && ai.o && ai.t)
ai.access = true;
} else {
var coords = snake.moves[1].split(" ");
var oldx = parseInt(coords[0]);
var oldy = parseInt(coords[1]);
if (!isDown() && (key == "w" || key == "Up"))
up();
if (!isUp() && (key == "s" || key == "Down"))
down();
if (!isRight() && (key == "a" || key == "Left"))
left();
if (!isLeft() && (key == "d" || key == "Right"))
right();
if (key == "r" && ai.access) {
stopTimedLoop();
startUp = true;
ai.access = false;
ai.b = false;
ai.o = false;
ai.t = false;
highscore = 0;
showElement("instructions");
setText("scorelabel", "");
setText("highscorelabel", "");
hideElement("snake");
hideElement("fruit");
for (var a = 1; a < snake.IDandLength; a++)
hideElement("" + a);
});
// these are functions for to set velX and velY variables to indicate movement of the snake
function up() {
snake.velY = -10;
snake.velX = 0;
function down() {
snake.velY = 10;
snake.velX = 0;
function left() {
snake.velX = -10;
snake.velY = 0;
function right() {
snake.velX = 10;
snake.velY = 0;
// these are functions to check if the snake is currently going in a certain direction
function isUp() {
return snake.velY == -10 && snake.velX == 0;
function isDown() {
return snake.velY == 10 && snake.velX == 0;
function isLeft() {
return snake.velX == -10 && snake.velY == 0;
function isRight() {
return snake.velX == 10 && snake.velY == 0;
Applab coding
var currentIndex=0;
var icecream="http://www.akerufeed.com/wp-content/uploads/2016/07/allice.jpg";
var padThai="http://www.circulaire-en-ligne.ca/wp-content/uploads/recette-pad-thai-facile-
300x200.jpg";
var panda="https://upload.wikimedia.org/wikipedia/commons/6/62/Panda_(PSF).png";
var myFavoriteThings=[icecream,padThai,panda];
updateScreen();
onEvent("nextButton", "click", function() {
nextImage();
updateScreen();
});
onEvent("lastButton", "click", function() {
previousImage();
updateScreen();
});
onEvent("addButton", "click", function() {
appendItem(myFavoriteThings,getText("inputBox"));
updateScreen();
setText("inputBox","");
});
onEvent("screen1", "keydown", function(event) {
if (event.key=="Up") {
nextImage();
} else if ((event.key=="Down")) {
previousImage();
updateScreen();
});
//functions
function nextImage (){
currentIndex++;
if (currentIndex> myFavoriteThings.length-1){
currentIndex= currentIndex-1;
console.log("Up");
playSound("https://studio.code.org/v3/assets/R3xt1BXO0VuQve0HdwClFg/cartoonButton.mp3");
function previousImage(){
currentIndex--;
if (currentIndex< 0) {
currentIndex = currentIndex+1;
console.log("Down");
playSound("https://studio.code.org/v3/assets/13v2AdelOqxDhJ32C1fnAw/DownSound.mp3");
function updateScreen() {
setImageURL("myImage", myFavoriteThings[currentIndex]);
if (currentIndex>=myFavoriteThings.length) {
currentIndex = 0;
setImageURL("myImage","http://greatwallpapers.online/halloween-iphone-wallpaper-
pinterest/wallpaper-tumblr-halloween-iphone-wallpaper-pinterest-wallpaper-tumblr-7.jpeg");
setText("numbers",currentIndex +" of " + myFavoriteThings.length);
}else {
setText("numbers",currentIndex+1 +" of " + myFavoriteThings.length);}
hideElement("instruction");
onEvent("removeButton", "click", function() {
removeItem(myFavoriteThings, currentIndex);
if (currentIndex>=myFavoriteThings.length) {
currentIndex = 0;
setImageURL("myImage","http://greatwallpapers.online/halloween-iphone-wallpaper-
pinterest/wallpaper-tumblr-halloween-iphone-wallpaper-pinterest-wallpaper-tumblr-7.jpeg");
setText("numbers",currentIndex +" of " + myFavoriteThings.length);
} else {
updateScreen();
console.log("removeButton clicked!");}
});
Link : https://studio.code.org/projects/applab/17S9dUeJd7GnhTW7VSvjhA/view
Banana Game
var score = 0;
var lives = 3;
onEvent("start_button", "click", function() {
setScreen("game_screen");
});
onEvent("banana", "click", function() {
score = score + 1;
setText("total_score", score);
setPosition("banana", randomNumber(50,280), randomNumber(50, 350));
if (score == 20) {
setScreen("win_screen");
});
onEvent("background", "click", function() {
lives = lives - 1;
setText("number_lives", lives);
if (lives == 0) {
setScreen("lose_screen");
}
});
onEvent("playAgain_button", "click", function() {
lives = 3;
setText("number_lives", lives);
score = 0;
setText("total_score", score);
setScreen("welcome_screen");
});
onEvent("tryAgain_button", "click", function() {
score = 0;
setText("total_score", score);
lives = 3;
setText("number_lives", lives);
setScreen("welcome_screen");
});
Link of above code :
https://studio.code.org/projects/applab/TOSgYoamr2c7_8_V7T1Y8VaP7nSQ2V8Qbp5TuXCBk1k/view