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 ctx = document.getElementById('mybox').getContext('2d');
- var controlleft=0; var controlright=0; var controlup=0; var controldown=0; var controlspace=0;
- var bullet = {};
- bullet.x =100;
- bullet.y =100;
- bullet.On = true;
- /*bullet.fire = function fire()
- {
- if (controlspace == 1)
- {
- bullet.On = true;
- }
- };
- */
- /*bullet.draw = function draw()
- {
- ctx.fillStyle = "Red";
- ctx.fillRect = (bullet.x,bullet.y,10,10);
- };
- */
- var tank = {};
- tank.x = 10;
- tank.y = 10;
- tank.Cannonx = 50;
- tank.Cannony = 15;
- tank.Right = true;
- tank.Left = false;
- tank.Down = false;
- tank.Up = false;
- tank.draw = function draw()
- {
- ctx.fillStyle="blue";
- ctx.fillRect(tank.x,tank.y,50,50);
- ctx.fillStyle="blue";
- ctx.fillRect((tank.x + tank.Cannonx), (tank.y + tank.Cannony), 20,20);
- };
- tank.move = function move()
- {
- if (controlup == 1)
- {
- tank.y -= 10;
- tank.Up = true;
- tank.Right = false;
- tank.Down = false;
- tank.Left = false;
- }
- if (controldown == 1)
- {
- tank.y += 10;
- tank.Up = false;
- tank.Right = false;
- tank.Down = true;
- tank.Left = false;
- }
- if (controlleft == 1)
- {
- if (controldown == 0)
- {
- if (controlup == 0)
- {
- tank.x -= 10;
- tank.Up = false;
- tank.Right = false;
- tank.Down = false;
- tank.Left = true;
- }
- }
- }
- if (controlright ==1)
- {
- if (controldown == 0)
- {
- if (controlup == 0)
- {
- tank.x += 10;
- tank.Up = false
- tank.Right = true;
- tank.Down = false;
- tank.Left = false;
- }
- }
- }
- };
- tank.turn = function turn()
- {
- if (tank.Right == true)
- {
- tank.Cannonx = 50;
- tank.Cannony = 15;
- }
- if (tank.Left == true)
- {
- tank.Cannonx = -20;
- tank.Cannony = 15;
- }
- if (tank.Up == true)
- {
- tank.Cannonx = 15;
- tank.Cannony = -20;
- }
- if (tank.Down == true)
- {
- tank.Cannonx = 15;
- tank.Cannony = 50;
- }
- };
- setInterval ("draw();",33);
- function draw()
- {
- CreateBackground();
- tank.draw();
- tank.move();
- tank.turn();
- ctx.fillStyle = "blue";
- ctx.fillRect = (100,100,10,10);
- //bullet.draw();
- //bullet.fire();
- }
- 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;}
- if (event.keyCode == 32){controlspace = 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;}
- if (event.keyCode == 32){controlspace = 0;}
- }
- //function responsible for keyboard interface, when key is released, buttons stop working
- function CreateBackground()
- {
- ctx.fillStyle="black";
- ctx.fillRect(0,0,800,800);
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment