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');
- var a; var b; var c; var d;
- setInterval ("draw();",33);
- function draw()
- {
- CreateBackground();
- CreatePlayer();
- MovePlayerVertical();
- MovePlayerHorizontal();
- CreateKill(250,300);
- Kill(250,300);
- CreateKill(200,200);
- Kill(200,200);
- CreateWall(250,250);
- WallHorizontal(250,250); WallVertical(250,250);
- CreateWall(400,200);
- WallHorizontal(400,200); WallVertical(400,200);
- }
- 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,25,25);
- }
- //this function creates the player block
- function MovePlayerVertical()
- {
- if (controlup == 1) {Playery -= 5;}
- if (controldown == 1) {Playery += 5;}
- }
- //this function moves the player block
- function MovePlayerHorizontal()
- {
- if (controlleft == 1){Playerx -= 5;}
- if (controlright == 1) {Playerx += 5;}
- }
- 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(Killx, Killy)
- {
- if ( Math.abs(Playery - Killy) < 25 && Math.abs(Playerx - Killx) < 25) { Playery = 0; Playerx = 0;}
- }
- function WallVertical(Wallx, Wally)
- {
- if (controlup == 1)
- {
- if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playery - Wally) < 25) {Playery = (Wally + 25);}
- }
- if (controldown ==1)
- {
- if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playery - Wally) > -25) {Playery = (Wally - 25);}
- }
- }
- function WallHorizontal(Wallx, Wally)
- {
- if (controlleft == 1)
- {
- if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playerx - Wallx) < 25) {Playerx = (Wallx + 25);}
- }
- if (controlright ==1)
- {
- if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playerx - Wallx) > -25) {Playerx = (Wallx - 25);}
- }
- }
- //if the player touches a kill block, the player is reset to the start
- //problems with the kill function still need to be solved
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment