Guest User

Untitled

a guest
Jun 19th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById("myCanvas");
  2. var context = canvas.getContext("2d");
  3. var w = 0;
  4. var a = 0;
  5. var s = 0;
  6. var d = 0;
  7.  
  8. var x = 0;
  9. var y = 0;
  10.  
  11. function checkInput() {
  12.     document.onkeydown = function(e) {
  13.         var e = window.event || e
  14.  
  15.         if (e.keyCode == 87) w = 1;
  16.         else if (e.keyCode == 65) a = 1;
  17.         else if (e.keyCode == 83) s = 1;
  18.         else if (e.keyCode == 68) d = 1;
  19.     }
  20.     document.onkeyup = function(e) {
  21.         var e = window.event || e
  22.         if (e.keyCode == 87) w = 0;
  23.         else if (e.keyCode == 65) a = 0;
  24.         else if (e.keyCode == 83) s = 0;
  25.         else if (e.keyCode == 68) d = 0;
  26.     }
  27. }
  28.  
  29. function handleInput() {
  30.     if (w == 1) {
  31.  
  32.         y-=2;
  33.     }
  34.     if (a == 1) {
  35.         x-=2;
  36.     }
  37.     if (d == 1) {
  38.         x+=2;
  39.     }
  40.     if (s == 1) {
  41.         y+=2;
  42.     }
  43. }
  44.  
  45. function drawRectangle(x, y, size, color) {
  46.  
  47.     context.beginPath();
  48.     context.rect(x, y, size, size);
  49.     context.fillStyle = color;
  50.     context.fill();
  51.  
  52. }
  53.  
  54. function drawBackground(color) {
  55.     context.beginPath();
  56.     context.rect(0, 0, 240, 240);
  57.     context.fillStyle = color;
  58.     context.fill();
  59.  
  60. }
  61.  
  62. function Render() {
  63.     drawBackground("Black");
  64.     drawRectangle(x, y, 32, "Red");
  65. }
  66.  
  67. function Update() {
  68.     checkInput();
  69.     handleInput();
  70.     Render();
  71. }
  72. self.setInterval("Update()",50);
Add Comment
Please, Sign In to add comment