Advertisement
Guest User

Untitled

a guest
May 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.31 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 growAmount;
  11. int growCounter;
  12.  
  13.  
  14. Snake otherSnake;
  15. int initialY;
  16. String controls;
  17.  
  18.  
  19.  
  20. class SnakePoint
  21. {
  22. int x;
  23. int y;
  24. String nextPointDirection;
  25. boolean isSnake;
  26. SnakePoint(boolean _isSnake, String _nextPointDirection, int _x, int _y)
  27. {
  28. x = _x;
  29. y = _y;
  30. isSnake = _isSnake;
  31. nextPointDirection = _nextPointDirection;
  32. }
  33. }
  34.  
  35. Snake(int _initialY, String _controls)
  36. {
  37. growCounter = 0;
  38. controls = _controls;
  39. initialY = _initialY;
  40. otherSnake = null;
  41. speed = 6;
  42. growAmount = 3;
  43. for(int i = 0; i < 41; i++)
  44. {
  45. for(int j = 0; j < 41; j++)
  46. {
  47. snakeGrid[i][j] = new SnakePoint(false, "", i, j);
  48. }
  49. }
  50. initSnake();
  51. }
  52.  
  53. void setOtherSnake(Snake s)
  54. {
  55. otherSnake = s;
  56. }
  57.  
  58. void initSnake()
  59. {
  60. snakeGrid[10][initialY].isSnake = true;
  61. snakeGrid[10][initialY].nextPointDirection = "right";
  62.  
  63. snakeGrid[11][initialY].isSnake = true;
  64. snakeGrid[11][initialY].nextPointDirection = "right";
  65.  
  66. snakeGrid[12][initialY].isSnake = true;
  67. snakeGrid[12][initialY].nextPointDirection = "right";
  68.  
  69. head = snakeGrid[12][initialY];
  70. tail = snakeGrid[10][initialY];
  71.  
  72. snakeDirection = "right";
  73. }
  74.  
  75. boolean isSnakeSquare(int x, int y)
  76. {
  77. return snakeGrid[x][y].isSnake;
  78. }
  79.  
  80. boolean moveSnake(int _x, int _y)
  81. {
  82. boolean _moveTail = true;
  83. snakeGrid[head.x][head.y].nextPointDirection = snakeDirection;
  84. if (p.ghost() == true)
  85. {
  86. //move the snake only if
  87. if(
  88.  
  89. isSnakeSquare(head.x + _x, head.y + _y) ||
  90. (otherSnake != null && otherSnake.isSnakeSquare(head.x + _x, head.y + _y)) ||
  91. head.x + _x >=40 ||
  92. head.x + _x <=0 ||
  93. head.y + _y >=40 ||
  94. head.y + _y <=0
  95. )
  96. {
  97. //Collision
  98. return true;
  99. }
  100. }
  101. else
  102. {
  103. //move the snake only if
  104. if(
  105.  
  106. isSnakeSquare(head.x + _x, head.y + _y) ||
  107. (otherSnake != null && otherSnake.isSnakeSquare(head.x + _x, head.y + _y)) ||
  108. head.x + _x >=40 ||
  109. head.x + _x <=0 ||
  110. head.y + _y >=40 ||
  111. head.y + _y <=0
  112. )
  113. {
  114. //Collision
  115. return true;
  116. }
  117. }
  118.  
  119. if (apple.getApple(head.x + _x, head.y + _y)) growCounter = growAmount;
  120.  
  121.  
  122. if (p.getPowerup(head.x + _x, head.y + _y))
  123. {
  124. p.movePowerup();
  125. }
  126. this.speed = p.haste();
  127. this.growAmount = p.doubleLength();
  128.  
  129.  
  130. snakeGrid[head.x + _x][head.y + _y].isSnake = true;
  131. head = snakeGrid[head.x + _x][head.y + _y];
  132. if (growCounter > 0)
  133. {
  134. growCounter --;
  135. }
  136. else
  137. {
  138. moveTail();
  139. }
  140.  
  141. //No Collision
  142. return false;
  143. }
  144.  
  145. void moveTail()
  146. {
  147. tail.isSnake = false;
  148.  
  149. if(tail.nextPointDirection == "up") tail = snakeGrid[tail.x][tail.y - 1];
  150. else if(tail.nextPointDirection == "down") tail = snakeGrid[tail.x][tail.y + 1];
  151. else if(tail.nextPointDirection == "right") tail = snakeGrid[tail.x + 1][tail.y];
  152. else if(tail.nextPointDirection == "left") tail = snakeGrid[tail.x - 1][tail.y];
  153. }
  154. int countDown;
  155. boolean drawSnake()
  156. {
  157. countDown--;
  158. if(countDown <= 0)
  159. {
  160. keyPressed();
  161. if(snakeDirection == "up")
  162. {
  163. if (moveSnake(0, -1)) return true;
  164. }
  165. else if(snakeDirection == "down")
  166. {
  167. if (moveSnake(0, 1)) return true;
  168. }
  169. else if(snakeDirection == "right")
  170. {
  171. if (moveSnake(1, 0)) return true;
  172. }
  173. else if(snakeDirection == "left")
  174. {
  175. if (moveSnake(-1, 0)) return true;
  176. }
  177. countDown = speed;
  178. }
  179. return false;
  180. }
  181.  
  182. void keyPressed()
  183. {
  184. if(p.dizzy() == false)
  185. {
  186. //UP
  187. if (
  188. (keyCode == UP) && controls == "arrows" ||
  189. (key == 'w' || key == 'W') && controls == "wasd"
  190. )
  191. {
  192. if (snakeDirection == "up" || snakeDirection == "down") return;
  193. snakeDirection = "up";
  194. }
  195.  
  196. //DOWN
  197. else if (
  198. (keyCode == DOWN) && controls == "arrows" ||
  199. (key == 's' || key == 'S') && controls == "wasd"
  200. )
  201. {
  202. if (snakeDirection == "up" || snakeDirection == "down") return;
  203. snakeDirection = "down";
  204. }
  205.  
  206. //RIGHT
  207. else if (
  208. (keyCode == RIGHT) && controls == "arrows" ||
  209. (key == 'd' || key == 'D') && controls == "wasd"
  210. )
  211. {
  212. if (snakeDirection == "right" || snakeDirection == "left") return;
  213. snakeDirection = "right";
  214. }
  215.  
  216. //LEFT
  217. else if (
  218. (keyCode == LEFT) && controls == "arrows" ||
  219. (key == 'a' || key == 'A') && controls == "wasd"
  220. )
  221. {
  222. if (snakeDirection == "right" || snakeDirection == "left") return;
  223. snakeDirection = "left";
  224. }
  225. }
  226. //INVERT CONTROLS
  227. else
  228. {
  229. //UP
  230. if (
  231. (keyCode == UP) && controls == "arrows" ||
  232. (key == 'w' || key == 'W') && controls == "wasd"
  233. )
  234. {
  235. if (snakeDirection == "up" || snakeDirection == "down") return;
  236. snakeDirection = "down";
  237. }
  238.  
  239. //DOWN
  240. else if (
  241. (keyCode == DOWN) && controls == "arrows" ||
  242. (key == 's' || key == 'S') && controls == "wasd"
  243. )
  244. {
  245. if (snakeDirection == "up" || snakeDirection == "down") return;
  246. snakeDirection = "up";
  247. }
  248.  
  249. //RIGHT
  250. else if (
  251. (keyCode == RIGHT) && controls == "arrows" ||
  252. (key == 'd' || key == 'D') && controls == "wasd"
  253. )
  254. {
  255. if (snakeDirection == "right" || snakeDirection == "left") return;
  256. snakeDirection = "left";
  257. }
  258.  
  259. //LEFT
  260. else if (
  261. (keyCode == LEFT) && controls == "arrows" ||
  262. (key == 'a' || key == 'A') && controls == "wasd"
  263. )
  264. {
  265. if (snakeDirection == "right" || snakeDirection == "left") return;
  266. snakeDirection = "right";
  267. }
  268. }
  269. }
  270. }
  271.  
  272. class Apple
  273. {
  274. int x;
  275. int y;
  276. Apple()
  277. {
  278. moveApple();
  279. }
  280.  
  281. void moveApple()
  282. {
  283. x = (int)random(1,40);
  284. y = (int)random(1,40);
  285. }
  286.  
  287. boolean getApple(int _x, int _y)
  288. {
  289. if ( x == _x && y == _y)
  290. {
  291. moveApple();
  292. return true;
  293. }
  294. return false;
  295. }
  296.  
  297. boolean isApple(int _x, int _y)
  298. {
  299. if ( x == _x && y == _y)
  300. {
  301. return true;
  302. }
  303. return false;
  304. }
  305. }
  306.  
  307. class Renderer
  308. {
  309. void drawBoardSingle()
  310. {
  311. for(int i = 0; i < 41; i++)
  312. {
  313. for(int j = 0; j < 41; j++)
  314. {
  315. color colour;
  316. if (apple.isApple(i,j)) colour = #8B4513;
  317. else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
  318. else if (p.isPowerup(i,j)) colour = p.getColour();
  319. else colour = #5ba4d8;
  320. p.countDown--;
  321. fill(colour);
  322. stroke(255,255,255,80);
  323. rect(i*20, j*20, 20, 20);
  324. }
  325. }
  326. }
  327. void drawBoardMulti()
  328. {
  329. println("drawboardmulti");
  330. for(int i = 0; i < 41; i++)
  331. {
  332. for(int j = 0; j < 41; j++)
  333. {
  334. color colour;
  335. if (apple.isApple(i,j)) colour = #8B4513;
  336. else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
  337. else if (s2.isSnakeSquare(i,j)) colour = #ff0000;
  338. else if (p.isPowerup(i,j)) colour = p.getColour();
  339. else colour = #5ba4d8;
  340. p.countDown--;
  341. fill(colour);
  342. stroke(255,255,255,80);
  343. rect(i*20, j*20, 20, 20);
  344. }
  345. }
  346. }
  347. void drawCollision()
  348. {
  349. for(int i = 0; i < 41; i++)
  350. {
  351. color colour = #ff0000;
  352. fill(colour);
  353. stroke(255,255,255,80);
  354. rect(i*20, 0*20, 20, 20);
  355. rect(0*20, i*20, 20, 20);
  356. rect(i*20, 40*20, 20, 20);
  357. rect(40*20, i*20, 20, 20);
  358. }
  359. }
  360. }
  361.  
  362. class Powerup
  363. {
  364. int x;
  365. int y;
  366. int m = -1;
  367. long countDown = 0;
  368. color powerupColour;
  369. boolean isAvailable = true;
  370. String type = "none";
  371. int choose;
  372. int haste()
  373. {
  374. m--;
  375. println(m + " " + countDown + " " + s1.speed);
  376. if (choose == 1)
  377. {
  378. if (m<0)
  379. return 6;
  380. else
  381. return 4;
  382. }
  383. else if (choose == 4)
  384. {
  385. if (m<0)
  386. return 6;
  387. else
  388. return 12;
  389. }
  390. return 6;
  391. }
  392. int doubleLength()
  393. {
  394. println(m + " " + countDown);
  395. if (choose == 2)
  396. {
  397. if (m<0)
  398. return 3;
  399. else
  400. return 6;
  401. }
  402. return 3;
  403. }
  404. boolean dizzy()
  405. {
  406. if (choose == 4)
  407. {
  408. if (m<0)
  409. return true;
  410. else
  411. return false;
  412. }
  413. else return false;
  414. }
  415. boolean ghost()
  416. {
  417. if (choose == 3)
  418. {
  419. if (m<0)
  420. return true;
  421. else
  422. return false;
  423. }
  424. else return false;
  425. }
  426. void choosePowerup()
  427. {
  428. choose = 3;//(int)random(1,5);
  429. if (choose == 1) type = "haste";
  430. if (choose == 2) type = "doubleLength";
  431. if (choose == 3) type = "ghost";
  432. if (choose == 4) type = "dizzy";
  433. }
  434. Powerup()
  435. {
  436. movePowerup();
  437. }
  438. color getColour()
  439. {
  440. if (isAvailable) return #00AA00;
  441. else return #5ba4d8;
  442. }
  443. void movePowerup()
  444. {
  445. choosePowerup();
  446. x = (int)random(1,40);
  447. y = (int)random(1,40);
  448. }
  449.  
  450. boolean getPowerup(int _x, int _y)
  451. {
  452. countDown--;
  453. if (countDown <= 0)
  454. {
  455. isAvailable = true;
  456. if ( x == _x && y == _y)
  457. {
  458. movePowerup();
  459. countDown = 500000;
  460. isAvailable = false;
  461. m = 120;
  462. return true;
  463. }
  464. }
  465. return false;
  466. }
  467.  
  468. boolean isPowerup(int _x, int _y)
  469. {
  470. if ( x == _x && y == _y)
  471. {
  472. return true;
  473. }
  474. return false;
  475. }
  476. }
  477.  
  478. //make a new snake
  479. String gameMode = "single";
  480. Apple apple = new Apple();
  481. Renderer renderer = new Renderer();
  482. boolean gameOver;
  483. boolean restart;
  484. Snake s1;
  485. Snake s2;
  486. Powerup p = new Powerup();
  487.  
  488. int mode;
  489. void chooseGameMode(int select)
  490. {
  491. if (select == 1)
  492. {
  493. mode = 1;
  494. s1 = new Snake(10, "arrows");
  495. println("Single");
  496. }
  497. if (select == 2)
  498. {
  499. mode = 2;
  500. s1 = new Snake(10, "arrows");
  501. s2 = new Snake(20, "wasd");
  502. s1.setOtherSnake(s2);
  503. s2.setOtherSnake(s1);
  504. println("Multi");
  505. }
  506. }
  507.  
  508.  
  509.  
  510. void setup()
  511. {
  512. gameOver = false;
  513. chooseGameMode(1);
  514. size(1000,1000);
  515. frameRate(60);
  516. background(255,255,255);
  517. }
  518.  
  519.  
  520.  
  521. void keyReleased()
  522. {
  523. //RESTART
  524. if (key == 'p' || key == 'P')
  525. {
  526. if(restart == false)
  527. {
  528. restart = true;
  529. }
  530. }
  531. }
  532.  
  533. void draw()
  534. {
  535. if (!gameOver)
  536. {
  537. if (gameMode == "single")
  538. {
  539. if(s1.drawSnake())
  540. {
  541. renderer.drawCollision();
  542. println("game over");
  543. gameOver = true;
  544. }
  545. else
  546. {
  547. if (gameMode == "multi") renderer.drawBoardMulti();
  548. if (gameMode == "single") renderer.drawBoardSingle();
  549. }
  550. }
  551. else if (gameMode == "multi")
  552. {
  553. if(s1.drawSnake() || s2.drawSnake())
  554. {
  555. renderer.drawCollision();
  556. println("game over");
  557. gameOver = true;
  558. }
  559. else
  560. {
  561. if (gameMode == "multi") renderer.drawBoardMulti();
  562. if (gameMode == "single") renderer.drawBoardSingle();
  563. }
  564. }
  565. }
  566.  
  567. //restart the game
  568. if (gameOver)
  569. {
  570. if (restart)
  571. {
  572. gameOver = false;
  573. s1 = new Snake (10, "arrows");
  574. if (gameMode == "multi")
  575. {
  576. s2 = new Snake(20, "wasd");
  577. s1.setOtherSnake(s2);
  578. s2.setOtherSnake(s1);
  579. }
  580. println("restart");
  581. restart = false;
  582. }
  583. }
  584.  
  585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement