// ========================================== // Code to handle the actual gameplay // ========================================== // global variable for the timer which calls the // update function. You need this so you can cancel the timer // later var gTimer = null; // constants to influence how easy the game is const FRAME_RATE = 30; const TIME_TO_WIN = 10; // how many seconds you need to survive const MONSTER_SPEED = 5; // speeds of the objects const PLAYER_SPEED = 2; // global variables for drawing on the canvas var gCanvas = document.getElementById("gameSpace"); var gCtx = gCanvas.getContext("2d"); // when the game was started, for the timer gStartTime = null; // global variables for the objects on the screen // they all need the same properties x,y,etc var gMonster = { "x": 0, "y": 0, "xspeed": MONSTER_SPEED, "yspeed": MONSTER_SPEED, "width":50, "height":50, "colour": "#FF00FF" } // start the game function startTheGame() { // place player and monster in random places randomlyPlace(gMonster); // start the timer which calls the updater every 10th of a second gTimer = window.setInterval(updateGame,1000/FRAME_RATE); // start the keyboard listener addEventListener("keydown",handleKeyDown); // start the survival timer gStartTime = Date.now(); } // when a key is pressed, decide what to do function handleKeyDown(keyEvent) { switch(keyEvent.key) { case "ArrowUp": // decide what happens break; case "ArrowDown": // decide what happens break; // and more... } } // the main game loop. This function gets called for every frame of the game function updateGame() { // move the objects and get them to bounce or constrain // your code here // update the survival timer message // your code here // draw the players and monsters drawGame(); // if the monster and player have collided, the game ends // your code here // if the player has survived enough time, the game ends // your code here } // display a message by freplacing the innerHTML of the message DIV function displayMessage(text) { // your code here } // compare the current time to the time the game started // and return that, converted to seconds function getTimeSurvived() { var timeSurvived = (Date.now() - gStartTime)/1000; return timeSurvived; } // erase the current display and draw all the objects in the game function drawGame() { // erase the background // your code here // draw the objects // your code here } // draw a single object function drawObject(thing) { // set the context colour // use the context fillRect function } // stop the timer from calling the update function function endTheGame() { clearInterval(gTimer); } // move an object by adding the speeds to the coordinates function moveObject(thing) { // your code here }