1
0

snake.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Simple snake game
  2. // Use the arrow keys on the USB keyboard
  3. // If using a different HID, use the wasd keys
  4. // In any case, press esc to exit
  5. #define word char
  6. #include "lib/math.c"
  7. #include "lib/stdlib.c"
  8. #include "lib/sys.c"
  9. #include "lib/gfx.c"
  10. #define START_DELAY 120
  11. #define STEP_DELAY 20
  12. #define MIN_DELAY 30
  13. #define BOARD_WIDTH 40
  14. #define BOARD_HEIGHT 24
  15. #define SCORE_Y 24
  16. // Snake directions
  17. #define UP 0
  18. #define DOWN 1
  19. #define RIGHT 2
  20. #define LEFT 3
  21. #define CHAR_UP 30
  22. #define CHAR_DOWN 31
  23. #define CHAR_RIGHT 16
  24. #define CHAR_LEFT 17
  25. #define CHAR_WALL 219
  26. #define CHAR_BODY 177
  27. #define CHAR_FOOD 7
  28. #define BTN_LEFT 256
  29. #define BTN_RIGHT 257
  30. #define BTN_UP 258
  31. #define BTN_DOWN 259
  32. word score = 0;
  33. word delayTime = START_DELAY;
  34. word xFood = 0;
  35. word yFood = 0;
  36. struct coordinate{
  37. word x;
  38. word y;
  39. } snake[(BOARD_WIDTH-1)*(BOARD_HEIGHT-1)];
  40. word snakeLen;
  41. word dir = RIGHT;
  42. word dirOnScreen = RIGHT; // keep track of the direction of the rendered snake
  43. word gameOver = 0;
  44. unsigned word rngLfsr = 0xACE1;
  45. word rngBit = 0;
  46. word rngRand()
  47. {
  48. rngBit = ((rngLfsr >> 0) ^ (rngLfsr >> 2) ^ (rngLfsr >> 3) ^ (rngLfsr >> 5) ) & 1;
  49. rngLfsr = (rngLfsr >> 1) | (rngBit << 15);
  50. return rngLfsr;
  51. }
  52. void setTimer3(word ms)
  53. {
  54. // clear result
  55. timer3Value = 0;
  56. // set timer
  57. word *p = (word *) TIMER3_VAL;
  58. *p = ms;
  59. // start timer
  60. word *q = (word *) TIMER3_CTRL;
  61. *q = 1;
  62. }
  63. void clearScreen()
  64. {
  65. GFX_clearWindowtileTable();
  66. //GFX_clearWindowpaletteTable(); // no colors yet
  67. }
  68. void drawBorder()
  69. {
  70. word x, y;
  71. y = 0;
  72. char wallchar = CHAR_WALL;
  73. for (x = 0; x < BOARD_WIDTH; x++)
  74. {
  75. GFX_printWindowColored(&wallchar, 1, GFX_WindowPosFromXY(x, y), 0);
  76. }
  77. y = BOARD_HEIGHT-1;
  78. for (x = 0; x < BOARD_WIDTH; x++)
  79. {
  80. GFX_printWindowColored(&wallchar, 1, GFX_WindowPosFromXY(x, y), 0);
  81. }
  82. x = 0;
  83. for (y = 0; y < BOARD_HEIGHT; y++)
  84. {
  85. GFX_printWindowColored(&wallchar, 1, GFX_WindowPosFromXY(x, y), 0);
  86. }
  87. x = BOARD_WIDTH-1;
  88. for (y = 0; y < BOARD_HEIGHT; y++)
  89. {
  90. GFX_printWindowColored(&wallchar, 1, GFX_WindowPosFromXY(x, y), 0);
  91. }
  92. }
  93. void drawScore()
  94. {
  95. GFX_printWindowColored("Score:", 6, GFX_WindowPosFromXY(0, SCORE_Y), 0);
  96. char scoreBuf[32];
  97. itoa(score, scoreBuf);
  98. GFX_printWindowColored(scoreBuf, strlen(scoreBuf), GFX_WindowPosFromXY(7, SCORE_Y), 0);
  99. }
  100. void drawPlayer()
  101. {
  102. // draw head
  103. char bodyChar = 0;
  104. switch(dir)
  105. {
  106. case UP:
  107. bodyChar = CHAR_UP;
  108. break;
  109. case DOWN:
  110. bodyChar = CHAR_DOWN;
  111. break;
  112. case LEFT:
  113. bodyChar = CHAR_LEFT;
  114. break;
  115. case RIGHT:
  116. bodyChar = CHAR_RIGHT;
  117. break;
  118. }
  119. GFX_printWindowColored(&bodyChar, 1, GFX_WindowPosFromXY(snake[0].x, snake[0].y), 0);
  120. // draw body
  121. word i;
  122. for (i = 1; i < snakeLen; i++)
  123. {
  124. char bodyChar = CHAR_BODY;
  125. GFX_printWindowColored(&bodyChar, 1, GFX_WindowPosFromXY(snake[i].x, snake[i].y), 0);
  126. }
  127. }
  128. void drawFood()
  129. {
  130. char foodChar = CHAR_FOOD;
  131. GFX_printWindowColored(&foodChar, 1, GFX_WindowPosFromXY(xFood, yFood), 0);
  132. }
  133. word genNewFoodX()
  134. {
  135. // gen random number within screen width
  136. word randX = MATH_modU(rngRand(), BOARD_WIDTH);
  137. // fix on borders
  138. if (randX == 0)
  139. {
  140. randX = 1;
  141. }
  142. else if (randX == BOARD_WIDTH -1)
  143. {
  144. randX = BOARD_WIDTH -2;
  145. }
  146. return randX;
  147. }
  148. word genNewFoodY()
  149. {
  150. // gen random number within screen width
  151. word randY = MATH_modU(rngRand(), BOARD_HEIGHT);
  152. // fix on borders
  153. if (randY == 0)
  154. {
  155. randY = 1;
  156. }
  157. else if (randY == BOARD_HEIGHT -1)
  158. {
  159. randY = BOARD_HEIGHT -2;
  160. }
  161. return randY;
  162. }
  163. word collidesWithSnake(word x, word y)
  164. {
  165. // collision with snake
  166. word i;
  167. for (i = 0; i<snakeLen; i++)
  168. {
  169. if (x == snake[i].x && y == snake[i].y)
  170. {
  171. return 1;
  172. }
  173. }
  174. return 0;
  175. }
  176. word checkFoodCollision()
  177. {
  178. if (snake[0].x == xFood && snake[0].y == yFood)
  179. {
  180. score++;
  181. snakeLen++;
  182. xFood = genNewFoodX();
  183. yFood = genNewFoodY();
  184. // repeat if collision with snake, until no collision anymore
  185. // TODO: check and update only one axis at the time if collision
  186. while(collidesWithSnake(xFood, yFood))
  187. {
  188. xFood = genNewFoodX();
  189. yFood = genNewFoodY();
  190. }
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. void updatePlayer(word keepTail)
  196. {
  197. // check which button is held
  198. if (bdos_usbkey_held(BTN_LEFT))
  199. {
  200. if (dirOnScreen != RIGHT)
  201. {
  202. dir = LEFT;
  203. }
  204. }
  205. else if (bdos_usbkey_held(BTN_RIGHT))
  206. {
  207. if (dirOnScreen != LEFT)
  208. {
  209. dir = RIGHT;
  210. }
  211. }
  212. else if (bdos_usbkey_held(BTN_UP))
  213. {
  214. if (dirOnScreen != DOWN)
  215. {
  216. dir = UP;
  217. }
  218. }
  219. else if (bdos_usbkey_held(BTN_DOWN))
  220. {
  221. if (dirOnScreen != UP)
  222. {
  223. dir = DOWN;
  224. }
  225. }
  226. dirOnScreen = dir; // update
  227. // remove last part of body
  228. if (!keepTail)
  229. {
  230. char emptyTile = 0;
  231. GFX_printWindowColored(&emptyTile, 1, GFX_WindowPosFromXY(snake[snakeLen-1].x, snake[snakeLen-1].y), 0);
  232. }
  233. // move all snake pieces back
  234. word i;
  235. for (i = snakeLen-2; i >= 0; i--)
  236. {
  237. snake[i+1].x = snake[i].x;
  238. snake[i+1].y = snake[i].y;
  239. }
  240. // create new head
  241. switch(dir)
  242. {
  243. case UP:
  244. snake[0].x = snake[1].x;
  245. snake[0].y = snake[1].y -1;
  246. break;
  247. case DOWN:
  248. snake[0].x = snake[1].x;
  249. snake[0].y = snake[1].y +1;
  250. break;
  251. case LEFT:
  252. snake[0].x = snake[1].x -1;
  253. snake[0].y = snake[1].y;
  254. break;
  255. case RIGHT:
  256. snake[0].x = snake[1].x +1;
  257. snake[0].y = snake[1].y;
  258. break;
  259. }
  260. }
  261. word checkGameOver()
  262. {
  263. // collision with border
  264. if (snake[0].x == 0 || snake[0].x == BOARD_WIDTH-1 || snake[0].y == 0 || snake[0].y == BOARD_HEIGHT-1)
  265. {
  266. return 1;
  267. }
  268. // collision with self
  269. word i;
  270. for (i = 1; i<snakeLen; i++)
  271. {
  272. if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
  273. {
  274. return 1;
  275. }
  276. }
  277. return 0;
  278. }
  279. void doGameOver()
  280. {
  281. GFX_printWindowColored("GAME OVER!", 10, GFX_WindowPosFromXY((BOARD_WIDTH>>1) - 5, (BOARD_HEIGHT>>1)), 0);
  282. gameOver = 1;
  283. }
  284. void gameUpdate()
  285. {
  286. word keepTail = checkFoodCollision();
  287. updatePlayer(keepTail);
  288. drawScore();
  289. drawFood();
  290. drawPlayer();
  291. if (checkGameOver())
  292. {
  293. doGameOver();
  294. }
  295. }
  296. void init()
  297. {
  298. // start with a snake of length 3
  299. snakeLen = 3;
  300. dir = RIGHT;
  301. snake[0].x = (BOARD_WIDTH>>2) -1;
  302. snake[0].y = BOARD_HEIGHT>>1;
  303. snake[1].x = snake[0].x -1;
  304. snake[1].y = snake[0].y;
  305. snake[2].x = snake[1].x -1;
  306. snake[2].y = snake[1].y;
  307. // start food at top right ish
  308. xFood = (BOARD_WIDTH>>1) + (BOARD_WIDTH>>2);
  309. yFood = (BOARD_HEIGHT>>1) - (BOARD_HEIGHT>>2);
  310. clearScreen();
  311. drawBorder();
  312. drawScore();
  313. drawFood();
  314. drawPlayer();
  315. }
  316. int main()
  317. {
  318. init();
  319. setTimer3(delayTime);
  320. // main loop
  321. while (1)
  322. {
  323. if (hid_checkfifo())
  324. {
  325. word c = hid_fiforead();
  326. switch(c)
  327. {
  328. case 27: //escape
  329. // cleanup and exit
  330. clearScreen();
  331. bdos_printc('\n');
  332. return 'q';
  333. break;
  334. case 'a':
  335. if (dirOnScreen != RIGHT)
  336. {
  337. dir = LEFT;
  338. }
  339. break;
  340. case 'd':
  341. if (dirOnScreen != LEFT)
  342. {
  343. dir = RIGHT;
  344. }
  345. break;
  346. case 'w':
  347. if (dirOnScreen != DOWN)
  348. {
  349. dir = UP;
  350. }
  351. break;
  352. case 's':
  353. if (dirOnScreen != UP)
  354. {
  355. dir = DOWN;
  356. }
  357. break;
  358. case '=':
  359. case '+':
  360. // speed things up
  361. if (delayTime > MIN_DELAY + STEP_DELAY)
  362. {
  363. delayTime = delayTime - STEP_DELAY;
  364. }
  365. break;
  366. }
  367. }
  368. if (!gameOver)
  369. {
  370. if (timer3Value == 1)
  371. {
  372. gameUpdate();
  373. setTimer3(delayTime);
  374. }
  375. }
  376. rngRand(); //update rng
  377. }
  378. return 'q';
  379. }
  380. void interrupt()
  381. {
  382. // Handle all interrupts
  383. word i = get_int_id();
  384. switch(i)
  385. {
  386. case INTID_TIMER1:
  387. timer1Value = 1; // Notify ending of timer1
  388. break;
  389. case INTID_TIMER3:
  390. timer3Value = 1; // Notify ending of timer3
  391. break;
  392. }
  393. }