Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 20, 2016 14:23
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 codecademydev/57f09b3f49acec356d63b67eae4981aa to your computer and use it in GitHub Desktop.
Save codecademydev/57f09b3f49acec356d63b67eae4981aa to your computer and use it in GitHub Desktop.
Codecademy export
alert("Welcome to Oliver's GameBook, press OK to begin your adventure");
var intro = prompt("You are on your way home from school, and when you get to your house, Monema is there waiting. \n\n'Hi Oliver, how was your day?\n\nPlease choose an answer: GREAT / GOOD / BAD").toUpperCase();
if ( intro == 'GREAT') {
alert("Monema: I am really glad you had such a great time Oliver");
}
else if (intro == 'GOOD') {
alert("Monema: I had a good day too sweetheart");
}
else if
(intro == 'BAD') {
alert("Monema: awww no, that makes me sad, my sweetie had a bad day");
}
else {
alert("Computer is not happy, you have not given a right answer!");
}
alert("Raul comes out of the house, and says hello!");
var question = prompt("Raul: Shall we all go and play in your garden Oliver? Do you reply NO or YES").toUpperCase();
if ( question == 'NO') {
alert("Oliver: Only joking, you guys do smell but I will go ask my mummy and then we can play!");
}
else if (question == 'YES') {
alert("Oliver: Sure thing, I just need to ask my mummy if it's okay");
}
else {
alert("Computer is not happy, you have not given a right answer!");
}
alert("You walk inside the house to ask mummy if it is okay");
alert("But before you can find her, Chris is in the lounge on his laptop");
var manuel = prompt("I have just been looking on Uncle Manuels Facebook, man he is sooooo cooooool, don't you think Oliver?\n\nDo you answer YES or NO or KINDA?").toUpperCase();
switch (manuel)
{
case 'YES':
alert("The computer loves your inteligence");
break;
case 'NO':
alert("The computer knows you're wrong and you should listen to Christopher");
break;
case 'KINDA':
alert("The computer thinks you have some sense of style");
break;
default:
alert("Computer is not happy, you have put in a wrong answer");
}
alert("Mummy at this moment walks into the room.\n\nShe says: Oliver, of course your uncle Manuel is the coolest person in the whole wide world");
alert("Don't you remember the time he taught you how to do your 9 times tables?\n\nRemember the trick is to put the finger down on the number you are multiplying by 9.\n\nWhat's on the right of the finger you put down is the first number, and on the left is the second number");
var timesByNine = function () {
while(val != 8){
var val = parseInt(prompt("what number do you times 9 by to make 72? Input your answer as a number and make the computer happy by getting it right!"));
var val9 = val * 9;
if ( val == 8) {
alert('See how great your uncle is to teach you the 9 times table trick, well done Oliver!');
}
else {
alert('Wrong, you made computer cry! You number times 9 makes ' + val9 + ', try again!');
}
}
};
timesByNine();
alert("Yeah you are right guys, Uncle Manuel is the coolest in the world!\n\nMummy is it okay if Monema and Raul come to the garden to play?");
alert("Sure, that's fine Oliver");
alert("You may have asked your mummy, but you forgot about me, COMPUTER!!!!!!!!!!!\n\nBefore you can play with your friends, you will need to complete my game challenge.\n\nIf you do not complete my game, computer will CRUSH you!");
var canvas=document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius=10;
var x=canvas.width/2;
var y=canvas.height-30;
var dx=2;
var dy=-2;
var paddleHeight=10;
var paddleWidth=75;
var paddleX=(canvas.width-paddleWidth)/2;
var rightPressed= false;
var leftPressed=false;
var brickRowCount = 3;
var brickColumnCount = 5;
var brickWidth = 75;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 30;
var score = 0;
document.addEventListener("keydown",keyDownHandler,false);
document.addEventListener("keyup",keyUpHandler,false);
var bricks = [];
for(c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
function keyDownHandler(e){
if(e.keyCode==39){
rightPressed=true;
}
else if(e.keyCode==37){
leftPressed=true;
}
}
function keyUpHandler(e){
if(e.keyCode==39){
rightPressed=false;
}
else if(e.keyCode==37){
leftPressed=false;
}
}
function drawBall(){
ctx.beginPath();
ctx.arc(x,y,ballRadius,0,2*Math.PI);
ctx.fillstyle="#0033FF";
ctx.fillStroke="#0033FF";
ctx.Stroke="10"
ctx.fill();
ctx.closePath();
}
function drawPaddle(){
ctx.beginPath();
ctx.rect(paddleX,canvas.height-paddleHeight,paddleWidth,paddleHeight);
ctx.fillstyle="#0095DD";
ctx.fill();
ctx.closePath();
}
function drawBricks() {
for(c=0; c<brickColumnCount; c++) {
for(r=0; r<brickRowCount; r++) {
if(bricks[c][r].status == 1) {
var brickX = (c*(brickWidth+brickPadding))+brickOffsetLeft;
var brickY = (r*(brickHeight+brickPadding))+brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
}
}
}
function collisionDetection() {
for(c=0; c<brickColumnCount; c++) {
for(r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.status == 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 0;
score++;
if(score == brickRowCount*brickColumnCount) {
alert("CONGRATULATIONS OLIVER, YOU HAVE BEATEN THE COMPUTER!");
}
}
}
}
}
}
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("Score: "+score, 8, 20);
}
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
drawBricks();
drawBall();
drawPaddle();
collisionDetection();
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if(y + dy < ballRadius) {
dy = -dy;
}
else if(y + dy > canvas.height-ballRadius) {
if(x > paddleX && x < paddleX + paddleWidth) {
if(y= y-paddleHeight){
dy = -dy ;
}
}
else {
alert("GAME OVER OLIVER, COMPUTER WINS!");
document.location.reload();
}
}
if(rightPressed && paddleX<canvas.width-paddleWidth){
paddleX+=7;
}
else if(leftPressed && paddleX>0 ){
paddleX-=7;
}
x=x+dx;
y=y+dy;
requestAnimationFrame(draw);
}
document.addEventListener("mousemove", mouseMoveHandler, false);
function mouseMoveHandler(e) {
var relativeX = e.clientX - canvas.offsetLeft;
if(relativeX > 0 && relativeX < canvas.width) {
paddleX = relativeX - paddleWidth/2;
}
}
draw();
alert("You might have won this battle, but Computer is not finished yet Oliver!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment