snake_gui.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include <string.h>
  2. #include <rtthread.h>
  3. #include <rtgui/rtgui.h>
  4. #include <rtgui/rtgui_app.h>
  5. #include <rtgui/widgets/container.h>
  6. #include <rtgui/widgets/window.h>
  7. #include <rtgui/widgets/button.h>
  8. #include "snake.h"
  9. #define LATTICE_SIZE (20)
  10. #define FOOD_MAX (8)
  11. #define WALL_COLOR RTGUI_RGB(255, 0, 0)
  12. #define SNAKE_COLOR RTGUI_RGB(111, 0, 255)
  13. #define BACKGROUND_COLOR RTGUI_RGB(153, 153, 0)
  14. #define FOOD_COLOR RTGUI_RGB(0, 111, 111)
  15. #define min(a, b) ((a) < (b) ? (a) : (b))
  16. static rtgui_timer_t *timer;
  17. static rt_size_t room_size_x, room_size_y;
  18. static rt_size_t lattice_size_x, lattice_size_y;
  19. static struct rtgui_rect room_rect, lattice_rect;
  20. map_t* map;
  21. SNAKE_DIR run_state;
  22. rt_int32_t snake_len;
  23. rt_int32_t food_num;
  24. static void snake_fill_lattice(struct rtgui_dc *dc,
  25. rt_uint32_t x,
  26. rt_uint32_t y,
  27. rtgui_color_t color)
  28. {
  29. struct rtgui_rect rect;
  30. // coordinate conversion
  31. y = (lattice_size_y - 1) - y;
  32. RTGUI_DC_BC(dc) = color;
  33. rect.x1 = lattice_rect.x1 + (LATTICE_SIZE * x);
  34. rect.x2 = rect.x1 + LATTICE_SIZE;
  35. rect.x1 += 2;
  36. rect.y1 = lattice_rect.y1 + (LATTICE_SIZE * y);
  37. rect.y2 = rect.y1 + LATTICE_SIZE;
  38. rect.y1 += 2;
  39. rtgui_dc_fill_rect(dc, &rect);
  40. }
  41. static void snake_draw(struct rtgui_widget *widget)
  42. {
  43. struct rtgui_dc *dc;
  44. struct rtgui_rect rect;
  45. rt_uint32_t i;
  46. // rt_kprintf("snake_draw\r\n");
  47. dc = rtgui_dc_begin_drawing(widget);
  48. if (dc == RT_NULL)
  49. {
  50. rt_kprintf("dc == RT_NULL\r\n");
  51. return;
  52. }
  53. /* get room size, run once frist. */
  54. if((room_size_x == 0) || (room_size_y == 0))
  55. {
  56. rt_size_t tmp;
  57. rtgui_widget_get_rect(widget, &rect);
  58. rt_kprintf("rect => x1:%d x2:%d, y1:%d y2:%d\r\n", rect.x1, rect.x2, rect.y1, rect.y2);
  59. room_size_x = rect.x2 - rect.x1;
  60. room_size_y = rect.y2 - rect.y1;
  61. memcpy(&room_rect, &rect, sizeof(struct rtgui_rect));
  62. rt_kprintf("room_rect => x1:%d x2:%d, y1:%d y2:%d\r\n",
  63. room_rect.x1, room_rect.x2,
  64. room_rect.y1, room_rect.y2);
  65. lattice_size_x = (room_rect.x2 - room_rect.x1) / LATTICE_SIZE;
  66. lattice_size_y = (room_rect.y2 - room_rect.y1) / LATTICE_SIZE;
  67. lattice_size_x -= 2;
  68. lattice_size_y -= 2;
  69. rt_kprintf("lattice_size_x:%d lattice_size_y:%d\r\n",
  70. lattice_size_x,
  71. lattice_size_y);
  72. tmp = (room_rect.x2 - room_rect.x1) - (LATTICE_SIZE * lattice_size_x);
  73. lattice_rect.x1 = room_rect.x1 + (tmp / 2);
  74. lattice_rect.x2 = lattice_rect.x1 + (LATTICE_SIZE * lattice_size_x);
  75. tmp = (room_rect.y2 - room_rect.y1) - (LATTICE_SIZE * lattice_size_y);
  76. lattice_rect.y1 = room_rect.y1 + (tmp / 2);
  77. lattice_rect.y2 = lattice_rect.y1 + (LATTICE_SIZE * lattice_size_y);
  78. rt_kprintf("lattice_rect => x1:%d x2:%d, y1:%d y2:%d\r\n",
  79. lattice_rect.x1, lattice_rect.x2,
  80. lattice_rect.y1, lattice_rect.y2);
  81. /* create snake. */
  82. {
  83. point_t start;
  84. map = map_init(lattice_size_x, lattice_size_y);
  85. if (map != RT_NULL)
  86. {
  87. start.x = snake_init_pointx;
  88. start.y = snake_init_pointy;
  89. run_state = SNAKE_DIR_DOWN;
  90. if (snake_init(&start, snake_length_init, run_state, map))
  91. {
  92. food_num = 1;
  93. food_init(map, food_num);
  94. }
  95. else
  96. {
  97. map_deinit(map);
  98. map = RT_NULL;
  99. }
  100. }
  101. }
  102. }
  103. RTGUI_DC_BC(dc) = BACKGROUND_COLOR;
  104. rtgui_dc_fill_rect(dc, &room_rect);
  105. memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
  106. rect.x2 += 1;
  107. rect.y2 += 1;
  108. RTGUI_DC_FC(dc) = WALL_COLOR;
  109. rtgui_dc_draw_rect(dc, &rect);
  110. for(i=1; i<lattice_size_y; i++)
  111. {
  112. // rtgui_dc_draw_horizontal_line(struct rtgui_dc* dc, int x1, int x2, int y);
  113. memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
  114. rect.x1 += 1;
  115. rect.x2 -= 1;
  116. rtgui_dc_draw_horizontal_line(dc, rect.x1, rect.x2,
  117. rect.y1 + (LATTICE_SIZE * i));
  118. }
  119. for(i=1; i<lattice_size_x; i++)
  120. {
  121. // rtgui_dc_draw_vertical_line(struct rtgui_dc* dc, int x, int y1, int y2);
  122. memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
  123. rect.y1 += 1;
  124. rect.y2 -= 1;
  125. rtgui_dc_draw_vertical_line(dc, rect.x1 + (LATTICE_SIZE * i),
  126. rect.y1, rect.y2);
  127. }
  128. /* draw snake. */
  129. {
  130. rt_uint32_t x, y;
  131. for (y=0; y<map->height; y++)
  132. {
  133. for (x=0; x<map->width; x++)
  134. {
  135. switch (map->range[y * map->width + x])
  136. {
  137. case NORMAL:
  138. break;
  139. case FOOD:
  140. snake_fill_lattice(dc, x, y, FOOD_COLOR);
  141. break;
  142. case OVER:
  143. snake_fill_lattice(dc, x, y, SNAKE_COLOR);
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. rtgui_dc_end_drawing(dc);
  150. return;
  151. }
  152. static void snake_update(struct rtgui_widget *widget)
  153. {
  154. struct rtgui_dc *dc;
  155. rt_int32_t x, y;
  156. rt_uint32_t i;
  157. // rt_kprintf("snake_update\r\n");
  158. dc = rtgui_dc_begin_drawing(widget);
  159. if (dc == RT_NULL)
  160. {
  161. rt_kprintf("dc == RT_NULL\r\n");
  162. return;
  163. }
  164. for(i=0; i<3; i++)
  165. {
  166. if(i < 2)
  167. {
  168. x = map->snake_flush[i].x;
  169. y = map->snake_flush[i].y;
  170. }
  171. else
  172. {
  173. x = map->food_flush[0].x;
  174. y = map->food_flush[0].y;
  175. }
  176. if((x >= 0) && (y >= 0))
  177. {
  178. // rt_kprintf("snake_flush[%d].x:%d, snake_flush[%d].y:%d\r\n",
  179. // i, x,
  180. // i, y);
  181. switch (map->range[(map->width * y) + x])
  182. {
  183. case NORMAL:
  184. snake_fill_lattice(dc, x, y, BACKGROUND_COLOR);
  185. break;
  186. case FOOD:
  187. snake_fill_lattice(dc, x, y, FOOD_COLOR);
  188. break;
  189. case OVER:
  190. snake_fill_lattice(dc, x, y, SNAKE_COLOR);
  191. break;
  192. }
  193. }
  194. }
  195. // rt_kprintf("\r\n");
  196. // if((map->snake_flush[1].x >= 0) && (map->snake_flush[1].y >= 0))
  197. // {
  198. // rt_kprintf("snake_flush[1].x:%d, snake_flush[1].y:%d\r\n",
  199. // map->snake_flush[1].x, map->snake_flush[1].y);
  200. // snake_fill_lattice(dc,
  201. // map->snake_flush[1].x,
  202. // map->snake_flush[1].y,
  203. // SNAKE_COLOR);
  204. // }
  205. // x = map->food_flush[0].x;
  206. // y = map->food_flush[0].y;
  207. // if((map->food_flush[0].x >= 0) && (map->food_flush[0].y >= 0))
  208. // {
  209. // rt_kprintf("food_flush[0].x:%d, food_flush[0].y:%d\r\n",
  210. // map->food_flush[0].x, map->food_flush[0].y);
  211. // snake_fill_lattice(dc,
  212. // map->food_flush[0].x,
  213. // map->food_flush[0].y,
  214. // FOOD_COLOR);
  215. // }
  216. rtgui_dc_end_drawing(dc);
  217. return;
  218. }
  219. static void snake_handler(struct rtgui_widget *widget, rtgui_event_t *event)
  220. {
  221. struct rtgui_event_kbd* ekbd;
  222. ekbd = (struct rtgui_event_kbd*) event;
  223. if (ekbd->type == RTGUI_KEYDOWN)
  224. {
  225. switch (ekbd->key)
  226. {
  227. case RTGUIK_UP:
  228. rt_kprintf("RTGUIK_UP\r\n");
  229. //snake_key(1);
  230. //snake_step(map, UP);
  231. run_state = SNAKE_DIR_UP;
  232. break;
  233. case RTGUIK_DOWN:
  234. rt_kprintf("RTGUIK_DOWN\r\n");
  235. //snake_key(2);
  236. //snake_step(map, DOWN);
  237. run_state = SNAKE_DIR_DOWN;
  238. break;
  239. case RTGUIK_LEFT:
  240. rt_kprintf("RTGUIK_LEFT\r\n");
  241. //snake_key(3);
  242. //snake_step(map, LEFT);
  243. run_state = SNAKE_DIR_LEFT;
  244. break;
  245. case RTGUIK_RIGHT:
  246. rt_kprintf("RTGUIK_RIGHT\r\n");
  247. //snake_key(4);
  248. //snake_step(map, RIGHT);
  249. run_state = SNAKE_DIR_RIGHT;
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. }
  256. static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event)
  257. {
  258. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  259. rt_kprintf("event_handler\r\n");
  260. if (event->type == RTGUI_EVENT_PAINT)
  261. {
  262. rt_kprintf("RTGUI_EVENT_PAINT\r\n");
  263. // rtgui_container_event_handler(object, event);
  264. rtgui_win_event_handler((struct rtgui_object*)object, event);
  265. snake_draw(widget);
  266. rtgui_timer_start(timer);
  267. }
  268. else if (event->type == RTGUI_EVENT_SHOW)
  269. {
  270. rt_kprintf("RTGUI_EVENT_SHOW\r\n");
  271. // rtgui_container_event_handler(object, event);
  272. rtgui_win_event_handler((struct rtgui_object*)object, event);
  273. snake_draw(widget);
  274. rtgui_timer_start(timer);
  275. }
  276. else if (event->type == RTGUI_EVENT_HIDE)
  277. {
  278. rt_kprintf("RTGUI_EVENT_HIDE\r\n");
  279. // rtgui_container_event_handler(object, event);
  280. rtgui_win_event_handler((struct rtgui_object*)object, event);
  281. rtgui_timer_stop(timer);
  282. }
  283. else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
  284. {
  285. rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n");
  286. // rtgui_container_event_handler(object, event);
  287. rtgui_win_event_handler((struct rtgui_object*)object, event);
  288. rtgui_timer_stop(timer);
  289. }
  290. else if (event->type == RTGUI_EVENT_KBD)
  291. {
  292. // rt_kprintf("RTGUI_EVENT_KBD\r\n");
  293. // rtgui_container_event_handler(object, event);
  294. rtgui_win_event_handler((struct rtgui_object*)object, event);
  295. snake_handler(widget, event);
  296. }
  297. else
  298. {
  299. rt_kprintf("event->type:%d\r\n", event->type);
  300. // return rtgui_container_event_handler(object, event);
  301. return rtgui_win_event_handler((struct rtgui_object*)object, event);
  302. }
  303. return RT_FALSE;
  304. }
  305. static void timeout(struct rtgui_timer *timer, void *parameter)
  306. {
  307. struct rtgui_widget *widget;
  308. if (!map)
  309. return;
  310. if (snake_step(run_state, map) == FOOD)
  311. {
  312. // food--;
  313. snake_len++;
  314. if (snake_len >= (map->width * map->height) / 3)
  315. {
  316. point_t start;
  317. start.x = snake_init_pointx;
  318. start.y = snake_init_pointy;
  319. run_state = SNAKE_DIR_DOWN;
  320. snake_len = snake_length_init;
  321. if (!snake_restart(&start, snake_len, run_state, map))
  322. {
  323. map_deinit(map);
  324. map = RT_NULL;
  325. }
  326. }
  327. food_init(map, 1);
  328. }
  329. widget = RTGUI_WIDGET(parameter);
  330. // snake_draw(widget);
  331. snake_update(widget);
  332. return;
  333. }
  334. void snake_main(void)
  335. {
  336. struct rtgui_app* application;
  337. struct rtgui_win* win;
  338. application = rtgui_app_create(rt_thread_self(), "sanke_app");
  339. if (application != RT_NULL)
  340. {
  341. win = rtgui_mainwin_create(RT_NULL,
  342. "sanke_win",
  343. RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
  344. if(win == RT_NULL)
  345. {
  346. rt_kprintf("sanke_win create fail!\r\n");
  347. return;
  348. }
  349. rtgui_object_set_event_handler(RTGUI_OBJECT(win), event_handler);
  350. timer = rtgui_timer_create(RT_TICK_PER_SECOND / 2,
  351. RT_TIMER_FLAG_PERIODIC,
  352. timeout,
  353. (void *)win);
  354. rtgui_win_show(win, RT_TRUE);
  355. //Í˳öºó²Å·µ»Ø
  356. map_deinit(map);
  357. snake_deinit();
  358. food_deinit();
  359. rtgui_app_destroy(application);
  360. }
  361. }
  362. #include <finsh.h>
  363. FINSH_FUNCTION_EXPORT(snake_main, snake run)