Advertisement
Guest User

Walid la voiture

a guest
Feb 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. window.onload = function(){
  2. var canvasWidth = 900;
  3. var canvasHeight = 600;
  4. var blockSize = 30;
  5. var ctx;
  6. var delay = 100;
  7. var xCoord = 0;
  8. var yCoord = 0;
  9. var snakee;
  10. var applee;
  11. var widthInBlocks = canvasWidth/blockSize;
  12. var heightInBlocks = canvasHeight/blockSize;
  13. var score;
  14. var timeOut;
  15.  
  16. init();
  17.  
  18. function init(){
  19. var canvas = document.createElement('canvas');
  20. canvas.width = canvasWidth;
  21. canvas.height = canvasHeight;
  22. canvas.style.border = "30px solid gray";
  23. canvas.style.margin = "50px auto";
  24. canvas.style.display = "block";
  25. canvas.style.backgroundColor = "#ddd";
  26. document.body.appendChild(canvas);
  27. ctx = canvas.getContext('2d');
  28. snakee = new Snake([[6,4],[5,4],[4,4],[3,4],[2,4]],"right");
  29. applee = new Apple([10,10]);
  30. score = 0;
  31. refreshCanvas();
  32. }
  33.  
  34. function refreshCanvas(){
  35. snakee.advance();
  36. if (snakee.checkCollision()){
  37. gameOver();
  38. } else {
  39. if (snakee.isEatingApple(applee)){
  40. score++;
  41. snakee.ateApple = true;
  42. do {
  43. applee.setNewPosition();
  44. } while(applee.isOnSnake(snakee));
  45. }
  46. ctx.clearRect(0,0,canvasWidth,canvasHeight);
  47. drawScore();
  48. snakee.draw();
  49. applee.draw();
  50. timeOut = setTimeout(refreshCanvas,delay);
  51. }
  52. }
  53.  
  54. function gameOver(){
  55. ctx.save();
  56. ctx.font = "bold 70px sans-serif";
  57. ctx.fillStyle = "#000";
  58. ctx.textAlign = "center";
  59. ctx.textBaseline = "middle";
  60. ctx.strokeStyle = "white";
  61. ctx.lineWidth = 5;
  62. var centreX = canvasWidth / 2;
  63. var centreY = canvasHeight / 2;
  64. ctx.strokeText("Game Over", centreX, centreY - 180);
  65. ctx.fillText("Game Over", centreX, centreY - 180);
  66. ctx.font = "bold 30px sans-serif";
  67. ctx.strokeText("Appuyer sur la touche Espace pour rejouer", centreX, centreY - 120);
  68. ctx.fillText("Appuyer sur la touche Espace pour rejouer", centreX, centreY - 120);
  69. ctx.restore();
  70. }
  71.  
  72. function restart(){
  73. snakee = new Snake([[6,4],[5,4],[4,4],[3,4],[2,4]],"right");
  74. applee = new Apple([10,10]);
  75. score = 0;
  76. clearTimeout(timeOut);
  77. refreshCanvas();
  78. }
  79.  
  80. function drawScore(){
  81. ctx.save();
  82. ctx.font = "bold 200px sans-serif";
  83. ctx.fillStyle = "gray";
  84. ctx.textAlign = "center";
  85. ctx.textBaseline = "middle";
  86. var centreX = canvasWidth / 2;
  87. var centreY = canvasHeight / 2;
  88. ctx.fillText(score.toString(), centreX, centreY);
  89. ctx.restore();
  90. }
  91.  
  92. function drawBlock(ctx, position){
  93. var x = position[0]*blockSize;
  94. var y = position[1]*blockSize;
  95. ctx.fillRect(x,y,blockSize,blockSize);
  96. }
  97.  
  98. function Snake(body, direction){
  99. this.body = body;
  100. this.direction = direction;
  101. this.ateApple = false;
  102.  
  103. this.draw = function(){
  104. ctx.save();
  105. ctx.fillStyle="#ff0000";
  106. for (var i=0 ; i < this.body.length ; i++){
  107. drawBlock(ctx,this.body[i]);
  108. }
  109. ctx.restore();
  110. };
  111.  
  112. this.advance = function(){
  113. var nextPosition = this.body[0].slice();
  114. switch(this.direction){
  115. case "left":
  116. nextPosition[0] -= 1;
  117. break;
  118. case "right":
  119. nextPosition[0] += 1;
  120. break;
  121. case "down":
  122. nextPosition[1] += 1;
  123. break;
  124. case "up":
  125. nextPosition[1] -= 1;
  126. break;
  127. default:
  128. throw("invalid direction");
  129. }
  130. this.body.unshift(nextPosition);
  131. if (!this.ateApple)
  132. this.body.pop();
  133. else
  134. this.ateApple = false;
  135. };
  136.  
  137. this.setDirection = function(newDirection){
  138. var allowedDirections;
  139. switch(this.direction){
  140. case "left":
  141. case "right":
  142. allowedDirections=["up","down"];
  143. break;
  144. case "down":
  145. case "up":
  146. allowedDirections=["left","right"];
  147. break;
  148. default:
  149. throw("invalid direction");
  150. }
  151. if (allowedDirections.indexOf(newDirection) > -1){
  152. this.direction = newDirection;
  153. }
  154. };
  155.  
  156. this.checkCollision = function(){
  157. var wallCollision = false;
  158. var snakeCollision = false;
  159. var head = this.body[0];
  160. var rest = this.body.slice(1);
  161. var snakeX = head[0];
  162. var snakeY = head[1];
  163. var minX = 0;
  164. var minY = 0;
  165. var maxX = widthInBlocks - 1;
  166. var maxY = heightInBlocks - 1;
  167. var isNotBetweenHorizontalWalls = snakeX < minX || snakeX > maxX;
  168. var isNotBetweenVerticalWalls = snakeY < minY || snakeY > maxY;
  169.  
  170. if (isNotBetweenHorizontalWalls || isNotBetweenVerticalWalls)
  171. wallCollision = true;
  172.  
  173. for (var i=0 ; i<rest.length ; i++){
  174. if (snakeX === rest[i][0] && snakeY === rest[i][1])
  175. snakeCollision = true;
  176. }
  177.  
  178. return wallCollision || snakeCollision;
  179. };
  180.  
  181. this.isEatingApple = function(appleToEat){
  182. var head = this.body[0];
  183. if (head[0] === appleToEat.position[0] && head[1] === appleToEat.position[1])
  184. return true;
  185. else
  186. return false;
  187. }
  188.  
  189. }
  190.  
  191. function Apple(position){
  192. this.position = position;
  193.  
  194. this.draw = function(){
  195. ctx.save();
  196. ctx.fillStyle = "#33cc33";
  197. ctx.beginPath();
  198. var radius = blockSize/2;
  199. var x = this.position[0]*blockSize + radius;
  200. var y = this.position[1]*blockSize + radius;
  201. ctx.arc(x, y, radius, 0, Math.PI*2, true);
  202. ctx.fill();
  203. ctx.restore();
  204. };
  205.  
  206. this.setNewPosition = function(){
  207. var newX = Math.round(Math.random()*(widthInBlocks-1));
  208. var newY = Math.round(Math.random()*(heightInBlocks-1));
  209. this.position = [newX,newY];
  210. };
  211.  
  212. this.isOnSnake = function(snakeToCheck){
  213. var isOnSnake = false;
  214. for (var i=0 ; i < snakeToCheck.body.length ; i++){
  215. if(this.position[0] === snakeToCheck.body[i][0] && this.position[1] === snakeToCheck.body[i][1]){
  216. isOnSnake = true;
  217. }
  218. }
  219. return isOnSnake;
  220. };
  221.  
  222. }
  223.  
  224. document.onkeydown = function handleKeyDown(e){
  225. var key = e.keyCode;
  226. var newDirection;
  227. switch(key){
  228. case 37:
  229. newDirection = "left";
  230. break;
  231. case 38:
  232. newDirection = "up";
  233. break;
  234. case 39:
  235. newDirection = "right";
  236. break;
  237. case 40:
  238. newDirection = "down";
  239. break;
  240. case 32:
  241. restart();
  242. return;
  243. default:
  244. return;
  245. }
  246. snakee.setDirection(newDirection);
  247. };
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement