Guest User

Untitled

a guest
Dec 15th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #include <SparkFunColorLCDShield.h>
  2. #include <LinkedList.h>
  3.  
  4. /*
  5. AHAS NG BAYAN
  6. DELA CRUZ, ONG
  7. BROUGHT TO YOU BY PISTING SAWA
  8. */
  9.  
  10. /*
  11. A BRIEF NOTE ON DIRECTION
  12. WHEN WE TALK OF INCREASING LENGTH (x), WE TALK OF MOVING DOWNWARDS
  13. WHEN WE TALK OF INCREASING HEIGHT (Y), WE TALK OF MOVING RIGHTWARDS
  14. THIS MAY SEEM UNINTUITIVE TO THE UNFAMILIAR READER, BUT PLEASE BEAR WITH US.
  15. HENCEFORTH, IF THE SNAKE IS MOVING "TO THE LEFT", THE NEW HEAD OF THE SNAKE
  16. WILL HAVE ITS Y-COORDINATE DECREASED.
  17. */
  18.  
  19. // length ("x") and height ("y") as number of blocks
  20. // as well as width per block
  21. #define LENGTH 26
  22. #define HEIGHT 26
  23. #define WIDTH 5
  24. #define DARKTHEME 1
  25. #if DARKTHEME == 0
  26. #define DARKGREEN 0x051
  27. #define FOREGROUND BLACK
  28. #define BACKGROUND WHITE
  29. #define PEACHCOLOR RED
  30. #else
  31. #define DARKGREEN 0x051
  32. #define FOREGROUND WHITE
  33. #define BACKGROUND BLACK
  34. #define PEACHCOLOR BLUE
  35. #endif
  36. LCDShield lcd;
  37. const unsigned int S1 = 2; // pin for S1
  38. const unsigned int JOYX = A0, JOYY = A1; // pins for josytick X and Y
  39. unsigned int del; // delay
  40. bool obstacles[LENGTH][HEIGHT] = {0}; // an array of obstacles (1 is impassable)
  41. struct Node {
  42. unsigned int xpos, ypos;
  43. Node (unsigned int x, unsigned int y) {
  44. this->xpos = x;
  45. this->ypos = y;
  46. }
  47. Node () {
  48. this->xpos = 0;
  49. this->ypos = 0;
  50. }
  51. bool operator== (const Node& d) {
  52. return (this->xpos == d.xpos && this->ypos == d.ypos);
  53. }
  54. bool operator!= (const Node& d) {
  55. return !(*this == d);
  56. }
  57. };
  58. LinkedList<Node> snek; // snek[0] is HEAD; snek[snek.length()-1] is TAIL
  59. Node prut; // the prut
  60. bool isLoss = 0; // check whether lost or not
  61. enum Direction {LEFT, RIGHT, UP, DOWN, CENTER};
  62. Direction dir; // determine direction of the snek
  63. unsigned int score; // score (no shit)
  64. char buff[16]; // char buffer for later.
  65.  
  66. void setup() {
  67. // put your setup code here, to run once:
  68. // define obstacles
  69. for (int i = 0; i < HEIGHT; ++i) {
  70. obstacles[0][i] = 1;
  71. obstacles[LENGTH - 1][i] = 1;
  72. }
  73. for (int i = 0; i < LENGTH; ++i) {
  74. obstacles[i][0] = 1;
  75. obstacles[i][HEIGHT - 1] = 1;
  76. }
  77.  
  78. // display the text
  79. lcd.init(PHILIPS);
  80. lcd.contrast(-58);
  81. lcd.clear(BACKGROUND);
  82.  
  83. Serial.begin(9600);
  84. attachInterrupt(digitalPinToInterrupt(S1), startGame, RISING);
  85. }
  86.  
  87. void drawNode(const Node & n, const int & color) {
  88. // draw the Node object n in the board
  89. // dependent on the WIDTH variable
  90. lcd.setRect(n.xpos * WIDTH, n.ypos * WIDTH, (n.xpos + 1)*WIDTH, (n.ypos + 1)*WIDTH - 1, 1, color);
  91. }
  92.  
  93. Direction whatDir() {
  94. // determine first whether it is UP or DOWN
  95. // then determine whether LEFT or RIGHT
  96. // which means joystick on upper-left will register as UP
  97. if (analogRead(JOYY) > 767) return UP;
  98. else if (analogRead(JOYY) < 255) return DOWN;
  99. else if (analogRead(JOYX) > 767) return LEFT;
  100. else if (analogRead(JOYX) < 255) return RIGHT;
  101. else return CENTER;
  102. }
  103.  
  104. void startGame() {
  105. detachInterrupt(digitalPinToInterrupt(S1));
  106. // clear everything bitch
  107. Serial.println("A NEW CHALLENGER HAS ENTERED THE RANKS");
  108. lcd.clear(BACKGROUND);
  109. del = 1200000;
  110. delay(1000);
  111. score = 0;
  112. isLoss = 0;
  113. randomSeed(analogRead(A5)); // set random seed for randomness
  114. // draw the obstacles ;)
  115. for (unsigned int i = 0; i < LENGTH; ++i) {
  116. for (unsigned int j = 0; j < HEIGHT; ++j) {
  117. if (obstacles[i][j]) drawNode(Node(i, j), FOREGROUND);
  118. }
  119. }
  120. // initialize the snake
  121. snek.clear();
  122. // first three elements
  123. snek.add(Node(floor(LENGTH / 2), floor(HEIGHT / 2)));
  124. snek.add(Node(floor(LENGTH / 2), floor(HEIGHT / 2) + 1));
  125. snek.add(Node(floor(LENGTH / 2), floor(HEIGHT / 2) + 2));
  126. dir = LEFT;
  127.  
  128. // initialize prut
  129. do {
  130. prut = Node(random(1, LENGTH), random(1, HEIGHT));
  131. } while (!isValid(prut.xpos, prut.ypos));
  132.  
  133. // now print the first snek
  134. for (int i = 0; i < snek.size(); ++i) {
  135. // print using WIDTH and stuff
  136. Node tmp = snek.get(i);
  137. drawNode(snek.get(i), DARKGREEN);
  138. }
  139. // and print the peach
  140. drawNode(prut, PEACHCOLOR);
  141.  
  142. // now let's start playing
  143. bool isEaten = 0;
  144. while (!isLoss) {
  145. // first determine the new Direction dir
  146. Direction newDir = whatDir();
  147. if (newDir == CENTER);
  148. else if (newDir < 2 xor dir < 2) dir = newDir;
  149. // then create the new snek by creating a new node
  150. Node newHead;
  151. switch (dir) {
  152. case LEFT:
  153. newHead = Node(snek.get(0).xpos, snek.get(0).ypos - 1);
  154. break;
  155. case RIGHT:
  156. newHead = Node(snek.get(0).xpos, snek.get(0).ypos + 1);
  157. break;
  158. case UP:
  159. newHead = Node(snek.get(0).xpos - 1, snek.get(0).ypos);
  160. break;
  161. case DOWN:
  162. newHead = Node(snek.get(0).xpos + 1, snek.get(0).ypos);
  163. break;
  164. };
  165. snek.unshift(newHead);
  166. // and then pop the last element
  167. Node byeTail;
  168. if (prut != newHead) {
  169. byeTail = snek.pop(); // goodbye~~~
  170. }
  171. else {
  172. isEaten = 1;
  173. do {
  174. prut = Node(random(1, LENGTH), random(1, HEIGHT));
  175. } while (!isValid(prut.xpos, prut.ypos));
  176. }
  177. // then check for validity and whatnot
  178. // first check the new head if it collides
  179. if (obstacles[newHead.xpos][newHead.ypos]) isLoss = 1;
  180. // then check for EVERY GODDAMN ELEMENT IN THE SNEK
  181. for (int i = 1; i < snek.size(); ++i) {
  182. if (snek.get(0) == snek.get(i)) isLoss = 1;
  183. }
  184. // then display the snek
  185. if (isEaten) {
  186. // display the new fruit
  187. drawNode(prut, PEACHCOLOR);
  188. isEaten = not isEaten;
  189. ++score;
  190. }
  191. else {
  192. drawNode(byeTail, BACKGROUND); // remove the byeTail
  193. }
  194. drawNode(newHead, DARKGREEN);
  195.  
  196. // and then delay
  197. delay(del);
  198. }
  199. // and then initiate game over
  200. lcd.setStr("GAME OVER", 43, 25, FOREGROUND, BACKGROUND);
  201. sprintf(buff, "Score: %03d", score);
  202. lcd.setStr(buff, 64, 20, FOREGROUND, BACKGROUND);
  203. lcd.setStr("S1 to play", 80, 20, FOREGROUND, BACKGROUND);
  204. Serial.println("Ooh game over.");
  205. while (1) {
  206. if (digitalRead(S1) == LOW) {
  207. Serial.println("Okay escape.");
  208. break;
  209. }
  210. delay(1000);
  211. }
  212. lcd.clear(BACKGROUND);
  213. delay(10000);
  214. attachInterrupt(digitalPinToInterrupt(S1), startGame, RISING);
  215. }
  216.  
  217. bool isValid (unsigned int x, unsigned int y) {
  218. // determine whether (x,y) is a valid place to put a fruit
  219. if (obstacles[x][y] == 1) return 0;
  220. for (int i = 0; i < snek.size(); ++i) {
  221. if (snek.get(i) == Node(x, y)) return 0;
  222. }
  223. return 1;
  224. }
  225.  
  226. void loop() {
  227. // put your main code here, to run repeatedly:
  228. lcd.setStr("SNEK", 23, 43, DARKGREEN, BACKGROUND);
  229. lcd.setStr("Version 1.69", 43, 18, DARKGREEN, BACKGROUND);
  230. lcd.setStr("Ong, Dela Cruz", 63, 7, DARKGREEN, BACKGROUND);
  231. lcd.setStr("S1 to play", 80, 20, FOREGROUND, BACKGROUND);
  232. delay(1000);
  233. }
Add Comment
Please, Sign In to add comment