Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snake.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * snake.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #define UP 1
  24. #define DOWN 2
  25. #define LEFT 3
  26. #define RIGHT 4
  27. #define IN 5
  28. #define OUT 6
  29. void snake(void);
  30. void setPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  31. void clearPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  32. uint8_t pixelSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  33. void newCoin(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t *buf);
  34. void move(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t dir);
  35. void setPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  36. buf[(8 * z) + y] |= (1 << x);
  37. }
  38. void clearPixel(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  39. buf[(8 * z) + y] &= ~(1 << x);
  40. }
  41. uint8_t pixelSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  42. return buf[(8 * z) + y] &= (1 << x);
  43. }
  44. void displayBuffs(uint8_t *a, uint8_t *b) {
  45. uint8_t *buf = (uint8_t *)malloc(64);
  46. if (buf == NULL) {
  47. serialWriteString(getString(24));
  48. while(1);
  49. }
  50. uint8_t i;
  51. for (i = 0; i < 64; i++) {
  52. buf[i] = a[i] | b[i];
  53. }
  54. setImage(buf);
  55. free(buf);
  56. }
  57. void newCoin(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t *buf) {
  58. uint8_t tempX, tempY, tempZ;
  59. while (1) {
  60. tempX = (uint8_t)(((rand() / 256) / 32) - 1); // Num from 0 to 7
  61. tempY = (uint8_t)(((rand() / 256) / 32) - 1);
  62. tempZ = (uint8_t)(((rand() / 256) / 32) - 1);
  63. if (!pixelSet(tempX, tempY, tempZ, buf)) {
  64. break;
  65. }
  66. }
  67. *x = tempX;
  68. *y = tempY;
  69. *z = tempZ;
  70. }
  71. void move(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t dir) {
  72. switch (dir) {
  73. case UP:
  74. *y = (*y + 1);
  75. break;
  76. case DOWN:
  77. *y = (*y - 1);
  78. break;
  79. case LEFT:
  80. *x = (*x - 1);
  81. break;
  82. case RIGHT:
  83. *x = (*x + 1);
  84. break;
  85. case IN:
  86. *z = (*z - 1);
  87. break;
  88. case OUT:
  89. *z = (*z + 1);
  90. break;
  91. default:
  92. break;
  93. }
  94. while (*x > 7) {
  95. *x -= 8;
  96. }
  97. while (*y > 7) {
  98. *y -= 8;
  99. }
  100. while (*z > 8) {
  101. *z -= 8;
  102. }
  103. }
  104. uint8_t isSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t dir) {
  105. move(&x, &y, &z, dir);
  106. return pixelSet(x, y, z, buf);
  107. }
  108. uint8_t inverse(uint8_t dir) {
  109. if ((dir > 0) && (dir < 7)) {
  110. if ((dir % 2) == 0) {
  111. // even dir
  112. return (dir - 1);
  113. } else {
  114. // odd dir
  115. return (dir + 1);
  116. }
  117. } else {
  118. return 0;
  119. }
  120. }
  121. void clearTail(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t wrongDir) {
  122. // dirs from 1 to 6
  123. uint8_t i;
  124. for (i = 1; i <= 6; i++) {
  125. if (i != wrongDir) {
  126. if (isSet(x, y, z, buf, i)) {
  127. move(&x, &y, &z, i);
  128. clearTail(x, y, z, buf, inverse(i));
  129. return;
  130. }
  131. }
  132. }
  133. // If we reached this point, we are in the last call and clear the pixel
  134. clearPixel(x, y, z, buf);
  135. }
  136. void snake() {
  137. uint8_t *snake = (uint8_t *)malloc(64); // Snake
  138. if (snake == NULL) {
  139. serialWriteString(getString(24));
  140. while(1);
  141. }
  142. uint8_t i, xPos = 3, yPos = 3, zPos = 3, dir = UP, length = 1;
  143. uint8_t xCoin = 1, yCoin = 1, zCoin = 1;
  144. char c;
  145. for (i = 0; i < 64; i++) {
  146. snake[i] = 0;
  147. }
  148. setPixel(3, 3, 3, snake);
  149. serialWriteString(getString(23));
  150. while(1) {
  151. if (serialHasChar()) {
  152. c = serialGet();
  153. switch (c) {
  154. case 'x':
  155. free(snake);
  156. return;
  157. case 'w':
  158. dir = UP;
  159. break;
  160. case 'a':
  161. dir = LEFT;
  162. break;
  163. case 's':
  164. dir = DOWN;
  165. break;
  166. case 'd':
  167. dir = RIGHT;
  168. break;
  169. case 'q':
  170. dir = IN;
  171. break;
  172. case 'e':
  173. dir = OUT;
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. if (isFinished() > 6) {
  180. // Perform next game step and display it
  181. move(&xPos, &yPos, &zPos, dir); // move snake head coords
  182. setPixel(xPos, yPos, zPos, snake); // Add snake head pixel
  183. if ((xPos == xCoin) && (yPos == yCoin) && (zCoin == zPos)) {
  184. newCoin(&xCoin, &yCoin, &zCoin, snake); // Set new coin
  185. if (length < 255) { // change length
  186. length++;
  187. } else {
  188. // You won :)
  189. free(snake);
  190. return;
  191. }
  192. } else {
  193. // Delete last snake pixel to match length
  194. clearTail(xPos, yPos, zPos, snake, 0);
  195. }
  196. setPixel(xCoin, yCoin, zCoin, snake);
  197. setImage(snake); // Display snake and coin
  198. clearPixel(xCoin, yCoin, zCoin, snake);
  199. }
  200. }
  201. free(snake);
  202. }