Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

snake.c 4.6KB

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