Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. document.onkeydown = checkKey;
  2. var canvas;
  3. var ctx;
  4.  
  5. var up;
  6. var down;
  7. var left;
  8. var right;
  9.  
  10. var bobX = 200;
  11. var bobY = 200;
  12. var bobWidth = 30;
  13. var bobHeight = 30;
  14.  
  15. window.onload = function() {
  16. canvas = document.getElementById("gameCanvas");
  17. ctx = canvas.getContext("2d");
  18.  
  19.  
  20. var fps = 200; // frames per second
  21. setInterval(function() {
  22. updateAll();
  23. drawAll();
  24. }, 1000/fps)
  25.  
  26. };
  27.  
  28. var drawAll = function() {
  29. // draw background
  30. ctx.fillStyle = "white";
  31. ctx.fillRect(0, 0, canvas.width, canvas.height);
  32.  
  33. // draw bob
  34. ctx.fillStyle = "red";
  35. ctx.fillRect(bobX, bobY, bobWidth, bobHeight);
  36. };
  37.  
  38. var updateAll = function() {
  39.  
  40. if (up == true) {
  41.  
  42. up = false;
  43.  
  44. }
  45. if (down == true) {
  46. bobY += 1;
  47. down = false;
  48. }
  49. if (left == true) {
  50. bobX -= 1;
  51. left = false;
  52. }
  53. if (right == true) {
  54. bobX += 1;
  55. right = false;
  56. }
  57.  
  58.  
  59. };
  60.  
  61.  
  62.  
  63. function checkKey(e) {
  64.  
  65. e = e || window.event;
  66.  
  67. if (e.keyCode == '38') {
  68. up = true;
  69. }
  70. else if (e.keyCode == '40') {
  71. down = true;
  72. }
  73. else if (e.keyCode == '37') {
  74. left = true;
  75. }
  76. else if (e.keyCode == '39') {
  77. right = true;
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement