Simple website with some JavaScript games.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.3 KiB

7 years ago
  1. <script>
  2. window.onload = function()
  3. {
  4. var canvas = document.getElementById("canvas");
  5. var c = canvas.getContext('2d');
  6. c.fillStyle= "black";
  7. c.fillRect(0,0, canvas.width, canvas.height);
  8. };
  9. //entire game
  10. function play()
  11. {
  12. var animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback)
  13. {
  14. window.setTimeout(callback, 1000 / 60)
  15. };
  16. var canvas = document.getElementById("canvas");
  17. var width = 700;
  18. var height = 700;
  19. canvas.style.textAligh = 'center';
  20. var context = canvas.getContext('2d');
  21. var keysDown = {};
  22. //player variables;
  23. var score = 0;
  24. var alive = true;
  25. //usefull function
  26. function getRandomIntInclusive(min, max)
  27. {
  28. min = Math.ceil(min);
  29. max = Math.floor(max);
  30. return Math.floor(Math.random() * (max - min + 1)) + min;
  31. }
  32. //objects
  33. var Player = function(x,y)
  34. {
  35. this.x = 350
  36. this.y = 650;
  37. this.width = 10;
  38. this.height = 10;
  39. this.speed = 6;
  40. this.facing = 40;
  41. this.updateDirection = function(key)
  42. {
  43. facing = key;
  44. }
  45. this.render = function()
  46. {
  47. context.fillStyle = "rgba(255, 255, 255, 1)"; //white
  48. context.fillRect(this.x, this.y, this.width, this.height);
  49. }
  50. this.move = function()
  51. {
  52. for (var key in keysDown)
  53. {
  54. var value = Number(key);
  55. if(value == 37) //left
  56. {
  57. this.x = this.x - this.speed;
  58. }
  59. else if(value == 39)
  60. {
  61. this.x = this.x + this.speed;
  62. }
  63. }
  64. //collisions
  65. for(i = 0; i < bamboos.length; i++)
  66. {
  67. if(bamboos[i] != -1)
  68. {
  69. bb = bamboos[i];
  70. if(Math.abs(bb.x - p.x) <= bb.width)
  71. {
  72. if(p.y > bb.y && p.y < bb.y + bb.height || p.y + p.height < bb.y + bb.height && p.y + p.height > bb.y)
  73. {
  74. alive = false;
  75. bb = -1;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. //player object
  83. var p = new Player(350, 850);
  84. //object that falls from the sky
  85. var Bamboo = function()
  86. {
  87. this.x = getRandomIntInclusive(0, width);
  88. this.y = 0;
  89. this.width = 25;
  90. this.height = 75;
  91. this.speed = 4;
  92. this.render = function()
  93. {
  94. context.fillStyle = "rgba(0, 255, 0 , 1)"; //green
  95. context.fillRect(this.x, this.y, this.width, this.height);
  96. }
  97. this.move = function()
  98. {
  99. this.y += this.speed;
  100. if(this.y> height)
  101. {
  102. //remove bamboo from array
  103. score++;
  104. return true;
  105. }
  106. }
  107. }
  108. var bamboos = [];
  109. //draws all the objects
  110. var render = function()
  111. {
  112. context.fillStyle = "rgba(0, 0,0 ,1)";
  113. context.fillRect(0,0, width, height);
  114. context.fillStyle = "rgba(255, 255,255 ,1)";
  115. context.font = "20px Georgia";
  116. context.fillText("Score: " + score, 10, 25);
  117. p.render();
  118. for(i= 0; i < bamboos.length; i++)
  119. {
  120. if(bamboos[i] != -1)
  121. {
  122. bamboos[i].render();
  123. }
  124. }
  125. //console.log('render was called');
  126. //context.fillStyle = "rgba(0, 128, 0 ,1)"; //green
  127. //context.fillRect(0, 0, 700, 700);
  128. //console.log(context);
  129. }
  130. var update = function()
  131. {
  132. addBamboo();
  133. p.move();
  134. for (i = 0; i< bamboos.length; i++)
  135. {
  136. if(bamboos[i] != -1)
  137. {
  138. if(bamboos[i].move())
  139. {
  140. bamboos[i] = -1;
  141. }
  142. }
  143. }
  144. addBamboo();
  145. }
  146. var count = 0;
  147. var limit = 30;
  148. var addBamboo = function()
  149. {
  150. count++;
  151. if(count == limit)
  152. {
  153. count = 0;
  154. var bnew = true;
  155. for(i = 0; i < bamboos.length; i++)
  156. {
  157. if(bamboos[i] == -1)
  158. {
  159. bamboos[i] = new Bamboo();
  160. bnew = false;
  161. }
  162. }
  163. if(bnew)
  164. {
  165. bamboos.push(new Bamboo());
  166. }
  167. if(limit > 10)
  168. {
  169. limit --;
  170. }
  171. }
  172. }
  173. var tic = function()
  174. {
  175. //console.log('tic was called');
  176. if(alive)
  177. {
  178. update();
  179. render();
  180. }
  181. else
  182. {
  183. context.fillStyle = "rgba(0, 0,0 ,1)";
  184. context.fillRect(0,0, width, height);
  185. context.fillStyle = "rgba(255, 255,255 ,1)";
  186. context.font = "20px Georgia";
  187. context.fillText("You died with a score of: " + score, 250, 325);
  188. }
  189. animate(tic);
  190. }
  191. window.addEventListener("keydown", function (event)
  192. {
  193. if(event.keyCode >=37 && event.keyCode <=40)
  194. {
  195. p.facing = event.keyCode;
  196. }
  197. keysDown[event.keyCode] = true;
  198. });
  199. window.addEventListener("keyup", function (event)
  200. {
  201. delete keysDown[event.keyCode];
  202. });
  203. //tic();
  204. animate(tic);
  205. }
  206. </script>
  207. <div><canvas id="canvas" width="700" height="700"></canvas></div>
  208. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();play()"/></div>