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.

286 lines
7.4 KiB

7 years ago
7 years ago
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 = 4;
  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. if(p.x > bb.x && p.x < bb.x + bb.width || p.x + p.width < bb.x + bb.width && p.x + p.width > bb.x)
  72. {
  73. 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)
  74. {
  75. alive = false;
  76. bb = -1;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. //player object
  84. var p = new Player(350, 850);
  85. //object that falls from the sky
  86. var Bamboo = function()
  87. {
  88. this.x = getRandomIntInclusive(0, width);
  89. this.y = 0;
  90. this.width = 25;
  91. this.height = 75;
  92. this.speed = 4;
  93. this.render = function()
  94. {
  95. context.fillStyle = "rgba(0, 255, 0 , 1)"; //green
  96. context.fillRect(this.x, this.y, this.width, this.height);
  97. }
  98. this.move = function()
  99. {
  100. this.y += this.speed;
  101. if(this.y> height)
  102. {
  103. //remove bamboo from array
  104. score++;
  105. return true;
  106. }
  107. }
  108. }
  109. var bamboos = [];
  110. //draws all the objects
  111. var render = function()
  112. {
  113. context.fillStyle = "rgba(0, 0,0 ,1)";
  114. context.fillRect(0,0, width, height);
  115. context.fillStyle = "rgba(255, 255,255 ,1)";
  116. context.font = "20px Georgia";
  117. context.fillText("Score: " + score, 10, 25);
  118. p.render();
  119. for(i= 0; i < bamboos.length; i++)
  120. {
  121. if(bamboos[i] != -1)
  122. {
  123. bamboos[i].render();
  124. }
  125. }
  126. //console.log('render was called');
  127. //context.fillStyle = "rgba(0, 128, 0 ,1)"; //green
  128. //context.fillRect(0, 0, 700, 700);
  129. //console.log(context);
  130. }
  131. var update = function()
  132. {
  133. addBamboo();
  134. p.move();
  135. for (i = 0; i< bamboos.length; i++)
  136. {
  137. if(bamboos[i] != -1)
  138. {
  139. if(bamboos[i].move())
  140. {
  141. bamboos[i] = -1;
  142. }
  143. }
  144. }
  145. addBamboo();
  146. }
  147. var count = 0;
  148. var limit = 30;
  149. var addBamboo = function()
  150. {
  151. count++;
  152. if(count == limit)
  153. {
  154. count = 0;
  155. var bnew = true;
  156. for(i = 0; i < bamboos.length; i++)
  157. {
  158. if(bamboos[i] == -1)
  159. {
  160. bamboos[i] = new Bamboo();
  161. bnew = false;
  162. }
  163. }
  164. if(bnew)
  165. {
  166. bamboos.push(new Bamboo());
  167. }
  168. if(limit > 10)
  169. {
  170. limit --;
  171. }
  172. }
  173. }
  174. var tic = function()
  175. {
  176. //console.log('tic was called');
  177. if(alive)
  178. {
  179. update();
  180. render();
  181. }
  182. else
  183. {
  184. context.fillStyle = "rgba(0, 0,0 ,1)";
  185. context.fillRect(0,0, width, height);
  186. context.fillStyle = "rgba(255, 255,255 ,1)";
  187. context.font = "20px Georgia";
  188. context.fillText("You died with a score of: " + score, 250, 325);
  189. }
  190. animate(tic);
  191. }
  192. window.addEventListener("keydown", function (event)
  193. {
  194. if(event.keyCode >=37 && event.keyCode <=40)
  195. {
  196. p.facing = event.keyCode;
  197. }
  198. keysDown[event.keyCode] = true;
  199. });
  200. window.addEventListener("keyup", function (event)
  201. {
  202. delete keysDown[event.keyCode];
  203. });
  204. //tic();
  205. animate(tic);
  206. }
  207. </script>
  208. <div><canvas id="canvas" width="700" height="700"></canvas></div>
  209. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();play()"/></div>