Advertisement
Guest User

Untitled

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