Advertisement
Guest User

SuperSnake Charles and Jonathan 5 game

a guest
May 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. class Snake
  2. {
  3. SnakePoint snakeGrid [][] = new SnakePoint[41][41];
  4. SnakePoint head;
  5. SnakePoint tail;
  6. String snakeDirection;
  7.  
  8. //powerups
  9. int speed;
  10. int growCounter;
  11.  
  12.  
  13. Snake otherSnake;
  14. int initialY;
  15. String controls;
  16.  
  17.  
  18.  
  19. class SnakePoint
  20. {
  21. int x;
  22. int y;
  23. String nextPointDirection;
  24. boolean isSnake;
  25. SnakePoint(boolean _isSnake, String _nextPointDirection, int _x, int _y)
  26. {
  27. x = _x;
  28. y = _y;
  29. isSnake = _isSnake;
  30. nextPointDirection = _nextPointDirection;
  31. }
  32. }
  33.  
  34. Snake(int _initialY, String _controls)
  35. {
  36. growCounter = 0;
  37. controls = _controls;
  38. initialY = _initialY;
  39. otherSnake = null;
  40. speed = 6;
  41. growAmount = 3;
  42. for(int i = 0; i < 41; i++)
  43. {
  44. for(int j = 0; j < 41; j++)
  45. {
  46. snakeGrid[i][j] = new SnakePoint(false, "", i, j);
  47. }
  48. }
  49. initSnake();
  50. }
  51.  
  52. void setOtherSnake(Snake s)
  53. {
  54. otherSnake = s;
  55. }
  56.  
  57. void initSnake()
  58. {
  59. snakeGrid[10][initialY].isSnake = true;
  60. snakeGrid[10][initialY].nextPointDirection = "right";
  61.  
  62. snakeGrid[11][initialY].isSnake = true;
  63. snakeGrid[11][initialY].nextPointDirection = "right";
  64.  
  65. snakeGrid[12][initialY].isSnake = true;
  66. snakeGrid[12][initialY].nextPointDirection = "right";
  67.  
  68. head = snakeGrid[12][initialY];
  69. tail = snakeGrid[10][initialY];
  70.  
  71. snakeDirection = "right";
  72. }
  73.  
  74. boolean isSnakeSquare(int x, int y)
  75. {
  76. return snakeGrid[x][y].isSnake;
  77. }
  78.  
  79. boolean moveSnake(int _x, int _y)
  80. {
  81. boolean _moveTail = true;
  82. snakeGrid[head.x][head.y].nextPointDirection = snakeDirection;
  83. if(
  84.  
  85. isSnakeSquare(head.x + _x, head.y + _y) ||
  86. (otherSnake != null && otherSnake.isSnakeSquare(head.x + _x, head.y + _y)) ||
  87. head.x + _x >=40 ||
  88. head.x + _x <=0 ||
  89. head.y + _y >=40 ||
  90. head.y + _y <=0
  91. )
  92. {
  93. //Collision
  94. return true;
  95. }
  96.  
  97. if (apple.getApple(head.x + _x, head.y + _y)) growCounter = growAmount;
  98.  
  99. snakeGrid[head.x + _x][head.y + _y].isSnake = true;
  100. head = snakeGrid[head.x + _x][head.y + _y];
  101. if (growCounter > 0)
  102. {
  103. growCounter --;
  104. }
  105. else
  106. {
  107. moveTail();
  108. }
  109.  
  110. //No Collision
  111. return false;
  112. }
  113.  
  114. void moveTail()
  115. {
  116. tail.isSnake = false;
  117.  
  118. if(tail.nextPointDirection == "up") tail = snakeGrid[tail.x][tail.y - 1];
  119. else if(tail.nextPointDirection == "down") tail = snakeGrid[tail.x][tail.y + 1];
  120. else if(tail.nextPointDirection == "right") tail = snakeGrid[tail.x + 1][tail.y];
  121. else if(tail.nextPointDirection == "left") tail = snakeGrid[tail.x - 1][tail.y];
  122. }
  123. int countDown;
  124. boolean drawSnake()
  125. {
  126. countDown--;
  127. if(countDown <= 0)
  128. {
  129. keyPressed();
  130. if(snakeDirection == "up")
  131. {
  132. if (moveSnake(0, -1)) return true;
  133. }
  134. else if(snakeDirection == "down")
  135. {
  136. if (moveSnake(0, 1)) return true;
  137. }
  138. else if(snakeDirection == "right")
  139. {
  140. if (moveSnake(1, 0)) return true;
  141. }
  142. else if(snakeDirection == "left")
  143. {
  144. if (moveSnake(-1, 0)) return true;
  145. }
  146. countDown = speed;
  147. }
  148. return false;
  149. }
  150.  
  151. void keyPressed()
  152. {
  153. //UP
  154. if (
  155. (keyCode == UP) && controls == "arrows" ||
  156. (key == 'w' || key == 'W') && controls == "wasd"
  157. )
  158. {
  159. if (snakeDirection == "up" || snakeDirection == "down") return;
  160. snakeDirection = "up";
  161. }
  162.  
  163. //DOWN
  164. else if (
  165. (keyCode == DOWN) && controls == "arrows" ||
  166. (key == 's' || key == 'S') && controls == "wasd"
  167. )
  168. {
  169. if (snakeDirection == "up" || snakeDirection == "down") return;
  170. snakeDirection = "down";
  171. }
  172.  
  173. //RIGHT
  174. else if (
  175. (keyCode == RIGHT) && controls == "arrows" ||
  176. (key == 'd' || key == 'D') && controls == "wasd"
  177. )
  178. {
  179. if (snakeDirection == "right" || snakeDirection == "left") return;
  180. snakeDirection = "right";
  181. }
  182.  
  183. //LEFT
  184. else if (
  185. (keyCode == LEFT) && controls == "arrows" ||
  186. (key == 'a' || key == 'A') && controls == "wasd"
  187. )
  188. {
  189. if (snakeDirection == "right" || snakeDirection == "left") return;
  190. snakeDirection = "left";
  191. }
  192. }
  193. }
  194.  
  195. class Apple
  196. {
  197. int x;
  198. int y;
  199. Apple()
  200. {
  201. moveApple();
  202. }
  203.  
  204. void moveApple()
  205. {
  206. x = (int)random(1,40);
  207. y = (int)random(1,40);
  208. }
  209.  
  210. boolean getApple(int _x, int _y)
  211. {
  212. if ( x == _x && y == _y)
  213. {
  214. g+=3;
  215. moveApple();
  216. return true;
  217. }
  218. return false;
  219. }
  220.  
  221. boolean isApple(int _x, int _y)
  222. {
  223. if ( x == _x && y == _y)
  224. {
  225. return true;
  226. }
  227. return false;
  228. }
  229. }
  230.  
  231. class Renderer
  232. {
  233. void drawBoardSingle()
  234. {
  235. for(int i = 0; i < 41; i++)
  236. {
  237. for(int j = 0; j < 41; j++)
  238. {
  239. color colour;
  240. if (apple.isApple(i,j)) colour = #8B4513;
  241. else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
  242. else colour = backgroundColour();
  243. fill(colour);
  244. stroke(255,255,255,80);
  245. rect(i*20, j*20, 20, 20);
  246. }
  247. }
  248. }
  249. void drawBoardMulti()
  250. {
  251. for(int i = 0; i < 41; i++)
  252. {
  253. for(int j = 0; j < 41; j++)
  254. {
  255. color colour;
  256. if (apple.isApple(i,j)) colour = #8B4513;
  257. else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
  258. else if (s2.isSnakeSquare(i,j)) colour = #ff0000;
  259. else colour = #5ba4d8;
  260. fill(colour);
  261. stroke(255,255,255,80);
  262. rect(i*20, j*20, 20, 20);
  263. }
  264. }
  265. }
  266. void drawCollision()
  267. {
  268. for(int i = 0; i < 41; i++)
  269. {
  270. color colour = #ff0000;
  271. fill(colour);
  272. stroke(255,255,255,80);
  273. rect(i*20, 0*20, 20, 20);
  274. rect(0*20, i*20, 20, 20);
  275. rect(i*20, 40*20, 20, 20);
  276. rect(40*20, i*20, 20, 20);
  277. }
  278. }
  279. }
  280.  
  281.  
  282. //make a new snake
  283. Apple apple = new Apple();
  284. Renderer renderer = new Renderer();
  285. boolean gameOver;
  286. boolean restart;
  287. Snake s1;
  288. Snake s2;
  289.  
  290. int mode;
  291. void chooseGameMode(int select)
  292. {
  293. if (select == 1)
  294. {
  295. mode = 1;
  296. s1 = new Snake(10, "arrows");
  297. println("Single");
  298. }
  299. if (select == 2)
  300. {
  301. mode = 2;
  302. s1 = new Snake(10, "arrows");
  303. s2 = new Snake(20, "wasd");
  304. s1.setOtherSnake(s2);
  305. s2.setOtherSnake(s1);
  306. println("Multi");
  307. }
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. void keyReleased()
  316. {
  317. //RESTART
  318. if (key == 'p' || key == 'P')
  319. {
  320. if(restart == false)
  321. {
  322. restart = true;
  323. }
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement