Advertisement
Guest User

Untitled

a guest
Mar 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. let ctx;
  2. let mySnake;
  3. let myFood, score;
  4. let foodPos;
  5. let newTail;
  6. let snakeSize = 30;
  7. let scoreNumber = 0;
  8. let snakeX;
  9. let snakeY;
  10. let tailX = [];
  11. let tailY = [];
  12. let objectList = [];
  13.  
  14.  
  15.  
  16. function startGame() {
  17. mySnakeGame.start();
  18. mySnake = new Component(snakeSize, snakeSize, "red", foodFlooring()*2, foodFlooring()*2);
  19. myFood = new Component(30, 30, "blue", foodFlooring()*2, foodFlooring()*2);
  20. score = new Component("50px", "Consolas", "black", 50, 50, "text");
  21. }
  22.  
  23. function Component(width, height, color, x, y, type) {
  24. this.type = type;
  25. this.width = width;
  26. this.height = height;
  27. this.speedX = 0;
  28. this.speedY = 0;
  29. this.x = x;
  30. this.y = y;
  31. ctx = mySnakeGame.context;
  32. ctx.fillStyle = color;
  33. ctx.strokeStyle = "black";
  34. ctx.strokeRect(this.x, this.y, this.width, this.height);
  35. ctx.fillRect(this.x, this.y, this.width, this.height);
  36. this.updatePiece = function() {
  37. ctx = mySnakeGame.context;
  38. if (this.type == "text") {
  39. ctx.font = this.width + " " + this.height;
  40. ctx.fillStyle = color;
  41. ctx.fillText(this.text, this.x, this.y);
  42. ctx.strokeStyle = "black";
  43. ctx.strokeRect(this.x, this.y, this.width, this.height);
  44. } else {
  45. ctx.fillStyle = color;
  46. ctx.fillRect(this.x, this.y, this.width, this.height);
  47. ctx.strokeStyle = "black";
  48. ctx.strokeRect(this.x, this.y, this.width, this.height);
  49. }
  50. };
  51. this.newPos = function() {
  52. this.x += this.speedX*30;
  53. this.y += this.speedY*30;
  54. };
  55. this.eat = function(object){
  56. let myLeft = this.x;
  57. let myRight = this.x + (this.width/2);
  58. let myTop = this.y;
  59. let myBottom = this.y + (this.height/2);
  60. let otherLeft = object.x;
  61. let otherRight = object.x + (object.width/2);
  62. let otherTop = object.y;
  63. let otherBottom = object.y + (object.height/2);
  64. let crash = true;
  65. if ((myBottom < otherTop) ||
  66. (myTop > otherBottom) ||
  67. (myRight < otherLeft) ||
  68. (myLeft > otherRight)) {
  69. crash = false;
  70. }
  71. return crash;
  72. };
  73. this.snakeGrow = function() {
  74. // if(mySnakeGame.scoreNumber === this.tail.length){
  75. // for(var i = 0; i < this.tail.length-1; i++){
  76. // this.tail[i] = this.tail[i+1];
  77. // console.log(this.tail.length);
  78. // }
  79. // }
  80. // tail[mySnakeGame.scoreNumber - 1] = ctx.fillRect(this.x, this.y, 30, 30);
  81. //
  82. // for(var i = 0; i < mySnakeGame.scoreNumber; i++){
  83. // ctx.rect(this.tail[i].x, this.tail[i].y, this.tail[i].width, this.tail[i].height);
  84. // }
  85. // var tail = [];
  86. // var lastX;
  87. // var lastY;
  88. // for(var i = 0; i < mySnakeGame.scoreNumber - 1; i++){
  89. // lastX = mySnake.x;
  90. // lastY = mySnake.y;
  91. // this.tail[i] = this.tail[i+1];
  92. // console.log(lastX + " " + lastY + " " + tail.length);
  93. // }
  94. // this.tail[mySnakeGame.scoreNumber - 1] = new Component(30, 30, "red", lastX, lastY);
  95. //
  96. // for(var i = 0; i < mySnakeGame.scoreNumber; i++){
  97. // this.tail[mySnakeGame.scoreNumber] = new Component(30, 30, "red", tail[i].x, tail[i].y);
  98. // }
  99. tailX.push(snakeX);
  100. tailY.push(snakeY);
  101. for(var i = 0; i < tailX.length - 1; i++){
  102. tailX[i] = tailX[i+1];
  103. }
  104.  
  105. for(let i = 0; i < tailY.length - 1; i++){
  106. tailY[i] = tailY[i+1];
  107. }
  108.  
  109. for(let i = 0; i < scoreNumber; i++){
  110. tailX[i] = new Component(30, 30, "orange", tailX[i], tailY[i]);
  111. tailY[i] = new Component(30, 30, "orange", tailX[i - 1], tailY[i - 1]);
  112. }
  113. tailX[scoreNumber - 1] = new Component(30, 30, "orange", tailX[scoreNumber], tailY[scoreNumber]);
  114. }
  115. }
  116.  
  117. function updateGameArea() {
  118. if (mySnake.eat(myFood)) {
  119. myFood = new Component(30, 30, "blue", foodFlooring()*2, foodFlooring()*2);
  120. scoreNumber++;
  121. // objectList[scoreNumber] = new Component(30, 30, "orange", snakeX, snakeY);
  122. this.ctx = mySnakeGame.context;
  123. this.ctx.fillStyle = "orange";
  124. ctx.fillRect(snakeX, snakeY, 30, 30);
  125. mySnake.snakeGrow();
  126.  
  127. }
  128.  
  129. if(mySnake.x < mySnakeGame.canvas.width){
  130. mySnake.x += mySnakeGame.canvas.width;
  131. mySnake.updatePiece();
  132. }
  133. if(mySnake.x >= mySnakeGame.canvas.width){
  134. mySnake.x -= mySnakeGame.canvas.width;
  135. mySnake.updatePiece();
  136. }
  137. if(mySnake.y < mySnakeGame.canvas.height){
  138. mySnake.y += mySnakeGame.canvas.height;
  139. mySnake.updatePiece();
  140. }
  141. if(mySnake.y >= mySnakeGame.canvas.height){
  142. mySnake.y -= mySnakeGame.canvas.height;
  143. mySnake.updatePiece();
  144. }
  145. if(myFood.x >= mySnakeGame.canvas.width){
  146. myFood.x -= mySnakeGame.canvas.width;
  147. myFood.updatePiece();
  148. }
  149. if(myFood.y >= mySnakeGame.canvas.height){
  150. myFood.y -= mySnakeGame.canvas.height;
  151. myFood.updatePiece();
  152. }
  153.  
  154.  
  155. mySnakeGame.clearField();
  156. mySnake.speedX = 0;
  157. mySnake.speedY = 0;
  158. snakeX = mySnake.x;
  159. snakeY = mySnake.y;
  160. if(mySnakeGame.key && mySnakeGame.key == 37){
  161. mySnake.speedX = -1;
  162. if(mySnakeGame.scoreNumber > 0){
  163. for(let i = 0; i < mySnakeGame.scoreNumber; i++){
  164. }
  165. }
  166. }
  167. if(mySnakeGame.key && mySnakeGame.key == 39){
  168. mySnake.speedX = 1;
  169. if(mySnakeGame.scoreNumber > 0){
  170. for(let i = 0; i < mySnakeGame.scoreNumber; i++){
  171. }
  172. }
  173. }
  174. if(mySnakeGame.key && mySnakeGame.key == 38){
  175. mySnake.speedY = -1;
  176. if(mySnakeGame.scoreNumber > 0){
  177. for(let i = 0; i < mySnakeGame.scoreNumber; i++){
  178. }
  179. }
  180. }
  181. if(mySnakeGame.key && mySnakeGame.key == 40){
  182. mySnake.speedY = 1;
  183. if(mySnakeGame.scoreNumber > 0){
  184. for(let i = 0; i < mySnakeGame.scoreNumber; i++){
  185. }
  186. }
  187. }
  188. mySnake.newPos();
  189. myFood.newPos();
  190. score.text = "SCORE: " + scoreNumber;
  191. score.updatePiece();
  192. if(scoreNumber > 0){
  193. // tailX[scoreNumber - 1].updatePiece();
  194. for(let i = 0; i < scoreNumber; i++){
  195. // ctx.fillRect(tailX[i].x, tailX[i].y, 30, 30);
  196. tailX[i].x = snakeX;
  197. tailX[i].y = snakeY;
  198. tailX[scoreNumber - 1].x = tailX[i].x;
  199. tailX[scoreNumber - 1].Y = tailX[i].y;
  200. // tailX[scoreNumber - 1].newPos();
  201. // tailX[scoreNumber - 1].updatePiece();
  202. tailX[i].newPos();
  203. tailX[i].updatePiece();
  204. tailY[i].newPos();
  205. tailY[i].updatePiece();
  206. }
  207. }
  208.  
  209. mySnake.updatePiece();
  210. myFood.updatePiece();
  211. }
  212.  
  213. function randNumber(min, max) {
  214. return Math.floor(Math.random() * (max - min)) + min;
  215. }
  216.  
  217. function foodFlooring() {
  218. for(let i = 0; i < randNumber(1, mySnakeGame.canvas.width); i+=30){
  219. foodPos = i;
  220. }
  221. return foodPos;
  222. }
  223.  
  224. function restartGame() {
  225. /**
  226. * HELP ME I DON'T KNOW WHAT IM DOING HERE
  227. */
  228. }
  229.  
  230. let mySnakeGame = {
  231. canvas: document.getElementById("canvas"),
  232. scoreNumber : 0,
  233. randomNumber: Math.floor(Math.random() * (this.canvas.width - 1)) + 1,
  234. start: function () {
  235. this.canvas.width = 900;
  236. this.canvas.height = 900;
  237. this.context = this.canvas.getContext("2d");
  238. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  239. this.interval = setInterval(updateGameArea, 100);
  240. window.addEventListener('keydown', function (e) {
  241. mySnakeGame.key = e.keyCode;
  242. console.log(e.which);
  243. e.preventDefault();
  244. });
  245. },
  246. clearField : function() {
  247. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  248. },
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement