Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <body onkeydown="keydown();" onkeyup="keyup();">
- <canvas id= "mybox" canvas height="500px" width="600px"></canvas>
- <script type="text/javascript">
- var Killx; var Killy;
- var Wallx; var Wally;
- var Playerx=0; var Playery=0;
- var controlleft; var controlright; var controlup; var controldown;
- var ctx = document.getElementById('mybox').getContext('2d');
- setInterval ("draw();",33);
- function draw()
- {
- CreateBackground();
- CreatePlayer(0,0);
- MovePlayer();
- CreateKill(250,300);
- CreateKill(200,200);
- CreateWall(0,250);
- kill();
- }
- function CreateBackground()
- {
- ctx.fillStyle="black";
- ctx.fillRect(0,0,800,800);
- }
- //creates the background for the game
- function CreateKill(Killx, Killy)
- {
- ctx.fillStyle="red";
- ctx.fillRect(Killx,Killy,25,25);
- }
- //this function creates kill blocks which if touched, will cause game over
- function CreateWall(Wallx, Wally)
- {
- ctx.fillStyle="blue";
- ctx.fillRect(Wallx,Wally,25,25);
- }
- //this function creates wall blocks intended to limit passage way without killing player
- function CreatePlayer()
- {
- ctx.fillStyle="green";
- ctx.fillRect(Playerx,Playery,50,50);
- }
- //this function creates the player block
- function MovePlayer()
- {
- if (controlleft == 1){Playerx -= 5;}
- if (controlright == 1) {Playerx += 5;}
- if (controlup == 1) {Playery -= 5;}
- if (controldown == 1) {Playery += 5;}
- }
- //this function moves the player block
- function keydown()
- {
- if (event.keyCode == 37){controlleft = 1;}
- if (event.keyCode == 39 ){controlright = 1;}
- if (event.keyCode == 38) {controlup = 1;}
- if (event.keyCode == 40){controldown = 1;}
- }
- //when the keys are hit, the variables that control movement are set to 1
- function keyup()
- {
- if (event.keyCode == 37){controlleft = 0;}
- if (event.keyCode == 39){controlright = 0;}
- if (event.keyCode == 38){controlup = 0;}
- if (event.keyCode == 40){controldown = 0;}
- }
- //when the keys are released, the variables that control movement are set to 0
- function kill()
- {
- var a = (Playery) - (Killy);
- var b = (Playerx) - (Killx);
- var c = Math.sqrt((a)*(a)+(b)*(b));
- if (c < 100000000000000000) {Playery = 0; Playerx = 0;}
- }
- //if the player touches a kill block, the player is reset to the start
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment