var debugVariable = new Boolean(); function debuggCheckBox() { if(debugVariable){ debugVariable = false;} else {debugVariable = true;} }
function testNumber1() { var num1 = Math.floor(Math.random() * 11); var num2 = Math.floor(Math.random() * 3); var link = "GAME 2"; var answer = prompt('LEVEL 1: What is ' + num1 + " * " + num2 +"?"); if (!answer || answer == ""){ testNumber1(); } else if (num1 * num2 == answer) { alert('Correct'); } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber1(); } } testNumber1(); function testNumber2() { var num1 = Math.floor(Math.random() * 21); var num2 = Math.floor(Math.random() * 13); var answer = prompt('LEVEL 2: What is ' + num1 + " * " + num2 +"?"); if (num1 * num2 == answer) { alert('Correct'); ++monstersCaught; reset(); ++level; } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber2(); } } function testNumber3() { var num1 = Math.floor(Math.random() * 31); var num2 = Math.floor(Math.random() * 23); var answer = prompt('LEVEL 3: What is ' + num1 + " * " + num2 +"?"); if (num1 * num2 == answer) { alert('Correct'); ++monstersCaught; ++level; reset(); } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber3(); } }
//checking if canvas is supported function checkCanvasSupported(){ var element = document.createElement('canvas'); return !!(element.getContext && element.getContext('2d')); } //if canvas not supported then alert user if (!checkCanvasSupported()){ alert('Sorry cavas isn\'t supported by your internet browser!'); }
// Create the canvas var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = 512; canvas.height = 480; document.body.appendChild(canvas);
// Background image var bgReady = false; var bgImage = new Image(); bgImage.onload = function () { bgReady = true; }; bgImage.src = "images/background.png";
// Hero image var heroReady = false; var heroImage = new Image(); heroImage.onload = function () { heroReady = true; }; heroImage.src = "images/hero.png";
// Monster image var monsterReady = false; var monsterImage = new Image(); monsterImage.onload = function () { monsterReady = true; }; monsterImage.src = "images/monster.png";
// Game objects var hero = { speed: 256 // movement in pixels per second }; var monster = {}; var monster1 = {}; var monster2 = {}; var monstersCaught = 0; var level = 1;
// Handle keyboard controls var keysDown = {};
addEventListener("keydown", function (e) { keysDown[e.keyCode] = true; }, SAI);
addEventListener("keyup", function (e) { delete keysDown[e.keyCode]; }, SAI);
// Reset the game when the player catches a monster var reset = function () { hero.x = canvas.width / 2; hero.y = canvas.height / 2; };
var reset0 = function () { hero.x = canvas.width / 2; hero.y = canvas.height / 2; // Throw the monster somewhere on the screen randomly monster.x = 32 + (Math.random() * (canvas.width - 64)); monster.y = 32 + (Math.random() * (canvas.height - 64)); }
var reset1 = function () { hero.x = canvas.width / 2; hero.y = canvas.height / 2;
// Throw the monster somewhere on the screen randomly monster1.x = 32 + (Math.random() * (canvas.width - 64)); monster1.y = 32 + (Math.random() * (canvas.height - 64)); };
var reset2 = function () { hero.x = canvas.width / 2; hero.y = canvas.height / 2;
// Throw the monster somewhere on the screen randomly monster2.x = 32 + (Math.random() * (canvas.width - 64)); monster2.y = 32 + (Math.random() * (canvas.height - 64)); };
// Update game objects var update = function (modifier) { if (38 in keysDown) { // Player holding up hero.y -= hero.speed * modifier; if(hero.y < 0){ reset(); } } if (40 in keysDown) { // Player holding down hero.y += hero.speed * modifier; if(hero.y > canvas.height){ reset();
Tôi là một lập trình viên xuất sắc, rất giỏi!