gameScript.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var myGamePiece;
  2. var secs = 0;
  3. const imagesDir = 'images/';
  4. const charAnim = [imagesDir+'idle.png', imagesDir+'walk1.png', imagesDir+'walk2.png'];
  5. function startGame() {
  6. myGamePiece = new component(40, 60, charAnim[0], 0, 512, "image");
  7. myGamePiece.gravity = 0.05;
  8. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  9. myGameArea.start();
  10. }
  11. var myGameArea = {
  12. canvas : document.createElement("canvas"),
  13. start : function() {
  14. this.canvas.width = 512;
  15. this.canvas.height = 512;
  16. this.context = this.canvas.getContext("2d");
  17. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  18. document.addEventListener("keydown",keyDown,false);
  19. document.addEventListener("keyup",keyUp,false);
  20. this.frameNo = 0;
  21. this.interval = setInterval(updateGameArea, 10);
  22. },
  23. clear : function() {
  24. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  25. }
  26. }
  27. function component(width, height, color, x, y, type) {
  28. this.type = type;
  29. this.score = 0;
  30. this.width = width;
  31. this.height = height;
  32. this.speedX = 0;
  33. this.speedY = 0;
  34. this.x = x;
  35. this.y = y;
  36. this.gravity = 0;
  37. this.gravitySpeed = 0;
  38. this.isMoving = false;
  39. if (type == "image") {
  40. this.image = new Image();
  41. this.image.src = color;
  42. }
  43. this.update = function() {
  44. ctx = myGameArea.context;
  45. if (type == "image") {
  46. ctx.drawImage(this.image,
  47. this.x,
  48. this.y,
  49. this.width, this.height);
  50. } else {
  51. ctx.fillStyle = color;
  52. ctx.fillRect(this.x, this.y, this.width, this.height);
  53. }
  54. }
  55. this.newPos = function() {
  56. this.gravitySpeed += this.gravity;
  57. this.x += this.speedX;
  58. this.y += this.speedY + this.gravitySpeed;
  59. this.hitBottom();
  60. if(!myGamePiece.isMoving){
  61. myGamePiece.image.src = charAnim[0];
  62. }
  63. }
  64. this.hitBottom = function() {
  65. var rockbottom = myGameArea.canvas.height - this.height;
  66. if (this.y > rockbottom) {
  67. this.y = rockbottom;
  68. this.gravitySpeed = 0;
  69. }
  70. }
  71. this.crashWith = function(otherobj) {
  72. var myleft = this.x;
  73. var myright = this.x + (this.width);
  74. var mytop = this.y;
  75. var mybottom = this.y + (this.height);
  76. var otherleft = otherobj.x;
  77. var otherright = otherobj.x + (otherobj.width);
  78. var othertop = otherobj.y;
  79. var otherbottom = otherobj.y + (otherobj.height);
  80. var crash = true;
  81. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  82. crash = false;
  83. }
  84. return crash;
  85. }
  86. }
  87. function updateGameArea() {
  88. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  89. secs++;
  90. if(secs >= 0 && secs < 30 && myGamePiece.isMoving){
  91. myGamePiece.image.src = charAnim[1];
  92. }
  93. if(secs >= 30 && secs < 50 && myGamePiece.isMoving){
  94. myGamePiece.image.src = charAnim[2];
  95. }
  96. if(secs >=50){
  97. secs = 0;
  98. }
  99. myGameArea.clear();
  100. myGameArea.frameNo += 1;
  101. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  102. x = myGameArea.canvas.width;
  103. minHeight = 20;
  104. maxHeight = 200;
  105. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  106. minGap = 50;
  107. maxGap = 200;
  108. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  109. }
  110. myGamePiece.newPos();
  111. myGamePiece.update();
  112. }
  113. function everyinterval(n) {
  114. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  115. return false;
  116. }
  117. function moveRight(n){
  118. myGamePiece.speedX = n;
  119. myGamePiece.isMoving = true;
  120. }
  121. function keyDown(e){
  122. if(!e) e = event;
  123. if(e.keyCode == 39) moveRight(1);
  124. }
  125. function keyUp(e){
  126. myGamePiece.image.src = charAnim[0];
  127. myGamePiece.speedX = 0;
  128. myGamePiece.speedY = 0;
  129. myGamePiece.isMoving = false;
  130. }