Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

snake.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. uint8_t i;
  47. for (i = 0; i < 64; i++) {
  48. buf[i] = a[i] | b[i];
  49. }
  50. setImage(buf);
  51. free(buf);
  52. }
  53. void newCoin(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t *buf) {
  54. uint8_t tempX, tempY, tempZ;
  55. while (1) {
  56. tempX = (uint8_t)(((rand() / 256) / 32) - 1); // Num from 0 to 7
  57. tempY = (uint8_t)(((rand() / 256) / 32) - 1);
  58. tempZ = (uint8_t)(((rand() / 256) / 32) - 1);
  59. if (!pixelSet(tempX, tempY, tempZ, buf)) {
  60. break;
  61. }
  62. }
  63. *x = tempX;
  64. *y = tempY;
  65. *z = tempZ;
  66. }
  67. void move(uint8_t *x, uint8_t *y, uint8_t *z, uint8_t dir) {
  68. switch (dir) {
  69. case UP:
  70. *y = (*y + 1);
  71. break;
  72. case DOWN:
  73. *y = (*y - 1);
  74. break;
  75. case LEFT:
  76. *x = (*x - 1);
  77. break;
  78. case RIGHT:
  79. *x = (*x + 1);
  80. break;
  81. case IN:
  82. *z = (*z - 1);
  83. break;
  84. case OUT:
  85. *z = (*z + 1);
  86. break;
  87. default:
  88. break;
  89. }
  90. while (*x > 7) {
  91. *x -= 8;
  92. }
  93. while (*y > 7) {
  94. *y -= 8;
  95. }
  96. while (*z > 8) {
  97. *z -= 8;
  98. }
  99. }
  100. uint8_t isSet(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t dir) {
  101. move(&x, &y, &z, dir);
  102. return pixelSet(x, y, z, buf);
  103. }
  104. uint8_t inverse(uint8_t dir) {
  105. if ((dir > 0) && (dir < 7)) {
  106. if ((dir % 2) == 0) {
  107. // even dir
  108. return (dir - 1);
  109. } else {
  110. // odd dir
  111. return (dir + 1);
  112. }
  113. } else {
  114. return 0;
  115. }
  116. }
  117. void clearTail(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf, uint8_t wrongDir) {
  118. // dirs from 1 to 6
  119. uint8_t i;
  120. for (i = 1; i <= 6; i++) {
  121. if (i != wrongDir) {
  122. if (isSet(x, y, z, buf, i)) {
  123. move(&x, &y, &z, i);
  124. clearTail(x, y, z, buf, inverse(i));
  125. return;
  126. }
  127. }
  128. }
  129. // If we reached this point, we are in the last call and clear the pixel
  130. clearPixel(x, y, z, buf);
  131. }
  132. void snake() {
  133. uint8_t *snake = (uint8_t *)malloc(64); // Snake
  134. uint8_t i, xPos = 3, yPos = 3, zPos = 3, dir = UP, length = 1;
  135. uint8_t xCoin = 1, yCoin = 1, zCoin = 1;
  136. char c;
  137. for (i = 0; i < 64; i++) {
  138. snake[i] = 0;
  139. }
  140. setPixel(3, 3, 3, snake);
  141. serialWriteString(getString(23));
  142. while(1) {
  143. if (serialHasChar()) {
  144. c = serialGet();
  145. switch (c) {
  146. case 'x':
  147. free(snake);
  148. return;
  149. case 'w':
  150. dir = UP;
  151. break;
  152. case 'a':
  153. dir = LEFT;
  154. break;
  155. case 's':
  156. dir = DOWN;
  157. break;
  158. case 'd':
  159. dir = RIGHT;
  160. break;
  161. case 'q':
  162. dir = IN;
  163. break;
  164. case 'e':
  165. dir = OUT;
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. if (isFinished() > 6) {
  172. // Perform next game step and display it
  173. move(&xPos, &yPos, &zPos, dir); // move snake head coords
  174. setPixel(xPos, yPos, zPos, snake); // Add snake head pixel
  175. if ((xPos == xCoin) && (yPos == yCoin) && (zCoin == zPos)) {
  176. newCoin(&xCoin, &yCoin, &zCoin, snake); // Set new coin
  177. if (length < 255) { // change length
  178. length++;
  179. } else {
  180. // You won :)
  181. free(snake);
  182. return;
  183. }
  184. } else {
  185. // Delete last snake pixel to match length
  186. clearTail(xPos, yPos, zPos, snake, 0);
  187. }
  188. setPixel(xCoin, yCoin, zCoin, snake);
  189. setImage(snake); // Display snake and coin
  190. clearPixel(xCoin, yCoin, zCoin, snake);
  191. }
  192. }
  193. free(snake);
  194. }