snake_gui.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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(0, 100, 200)
  13. #define SNAKE_HEAD_COLOR RTGUI_RGB(180, 70, 130)
  14. #define BACKGROUND_COLOR RTGUI_RGB(153, 153, 0)
  15. #define FOOD_COLOR RTGUI_RGB(128, 0, 0)
  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. point_t second_node;
  25. static void snake_fill_lattice(struct rtgui_dc *dc,
  26. rt_uint32_t x,
  27. rt_uint32_t y,
  28. rtgui_color_t color)
  29. {
  30. struct rtgui_rect rect;
  31. // coordinate conversion
  32. y = (lattice_size_y - 1) - y;
  33. RTGUI_DC_BC(dc) = color;
  34. rect.x1 = lattice_rect.x1 + (LATTICE_SIZE * x);
  35. rect.x2 = rect.x1 + LATTICE_SIZE;
  36. rect.x1 += 2;
  37. rect.y1 = lattice_rect.y1 + (LATTICE_SIZE * y);
  38. rect.y2 = rect.y1 + LATTICE_SIZE;
  39. rect.y1 += 2;
  40. rtgui_dc_fill_rect(dc, &rect);
  41. }
  42. static void snake_draw(struct rtgui_widget *widget)
  43. {
  44. struct rtgui_dc *dc;
  45. struct rtgui_rect rect;
  46. rt_uint32_t i;
  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. memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
  113. rect.x1 += 1;
  114. rect.x2 -= 1;
  115. rtgui_dc_draw_horizontal_line(dc, rect.x1, rect.x2,
  116. rect.y1 + (LATTICE_SIZE * i));
  117. }
  118. for (i = 1; i < lattice_size_x; i++)
  119. {
  120. memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
  121. rect.y1 += 1;
  122. rect.y2 -= 1;
  123. rtgui_dc_draw_vertical_line(dc, rect.x1 + (LATTICE_SIZE * i),
  124. rect.y1, rect.y2);
  125. }
  126. /* draw snake. */
  127. {
  128. rt_int32_t x, y;
  129. rt_bool_t first_node = RT_TRUE;
  130. for (y = 0; y < map->height; y++)
  131. {
  132. for (x = 0; x < map->width; x++)
  133. {
  134. switch (map->range[y * map->width + x])
  135. {
  136. case NORMAL:
  137. break;
  138. case FOOD:
  139. snake_fill_lattice(dc, x, y, FOOD_COLOR);
  140. break;
  141. case OVER:
  142. if (first_node)
  143. {
  144. first_node = RT_FALSE;
  145. second_node.x = x;
  146. second_node.y = y;
  147. snake_fill_lattice(dc, x, y, SNAKE_HEAD_COLOR);
  148. }
  149. else
  150. {
  151. snake_fill_lattice(dc, x, y, SNAKE_COLOR);
  152. }
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. rtgui_dc_end_drawing(dc);
  159. return;
  160. }
  161. static void snake_update(struct rtgui_widget *widget)
  162. {
  163. struct rtgui_dc *dc;
  164. rt_int32_t x, y;
  165. rt_uint32_t i;
  166. dc = rtgui_dc_begin_drawing(widget);
  167. if (dc == RT_NULL)
  168. {
  169. rt_kprintf("dc == RT_NULL\r\n");
  170. return;
  171. }
  172. snake_fill_lattice(dc, second_node.x, second_node.y, SNAKE_COLOR);
  173. second_node = map->snake_flush[0];
  174. for (i = 0; i < 3; i++)
  175. {
  176. if (i < 2)
  177. {
  178. x = map->snake_flush[i].x;
  179. y = map->snake_flush[i].y;
  180. }
  181. else
  182. {
  183. x = map->food_flush[0].x;
  184. y = map->food_flush[0].y;
  185. }
  186. if ((x >= 0) && (y >= 0))
  187. {
  188. switch (map->range[(map->width * y) + x])
  189. {
  190. case NORMAL:
  191. snake_fill_lattice(dc, x, y, BACKGROUND_COLOR);
  192. break;
  193. case FOOD:
  194. snake_fill_lattice(dc, x, y, FOOD_COLOR);
  195. break;
  196. case OVER:
  197. if (0 == i)
  198. snake_fill_lattice(dc, x, y, SNAKE_HEAD_COLOR);
  199. else
  200. snake_fill_lattice(dc, x, y, SNAKE_COLOR);
  201. break;
  202. }
  203. }
  204. }
  205. rtgui_dc_end_drawing(dc);
  206. return;
  207. }
  208. static void snake_handler(struct rtgui_widget *widget, rtgui_event_t *event)
  209. {
  210. struct rtgui_event_kbd *ekbd;
  211. ekbd = (struct rtgui_event_kbd *) event;
  212. if (ekbd->type == RTGUI_KEYDOWN)
  213. {
  214. switch (ekbd->key)
  215. {
  216. case RTGUIK_UP:
  217. rt_kprintf("RTGUIK_UP\r\n");
  218. run_state = SNAKE_DIR_UP;
  219. break;
  220. case RTGUIK_DOWN:
  221. rt_kprintf("RTGUIK_DOWN\r\n");
  222. run_state = SNAKE_DIR_DOWN;
  223. break;
  224. case RTGUIK_LEFT:
  225. rt_kprintf("RTGUIK_LEFT\r\n");
  226. run_state = SNAKE_DIR_LEFT;
  227. break;
  228. case RTGUIK_RIGHT:
  229. rt_kprintf("RTGUIK_RIGHT\r\n");
  230. run_state = SNAKE_DIR_RIGHT;
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. }
  237. static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event)
  238. {
  239. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  240. rt_kprintf("event_handler\r\n");
  241. if (event->type == RTGUI_EVENT_PAINT)
  242. {
  243. rt_kprintf("RTGUI_EVENT_PAINT\r\n");
  244. rtgui_win_event_handler((struct rtgui_object *)object, event);
  245. snake_draw(widget);
  246. rtgui_timer_start(timer);
  247. }
  248. else if (event->type == RTGUI_EVENT_SHOW)
  249. {
  250. rt_kprintf("RTGUI_EVENT_SHOW\r\n");
  251. rtgui_win_event_handler((struct rtgui_object *)object, event);
  252. snake_draw(widget);
  253. rtgui_timer_start(timer);
  254. }
  255. else if (event->type == RTGUI_EVENT_HIDE)
  256. {
  257. rt_kprintf("RTGUI_EVENT_HIDE\r\n");
  258. rtgui_win_event_handler((struct rtgui_object *)object, event);
  259. rtgui_timer_stop(timer);
  260. }
  261. else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
  262. {
  263. rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n");
  264. rtgui_win_event_handler((struct rtgui_object *)object, event);
  265. rtgui_timer_stop(timer);
  266. }
  267. else if (event->type == RTGUI_EVENT_KBD)
  268. {
  269. rtgui_win_event_handler((struct rtgui_object *)object, event);
  270. snake_handler(widget, event);
  271. }
  272. else
  273. {
  274. rt_kprintf("event->type:%d\r\n", event->type);
  275. return rtgui_win_event_handler((struct rtgui_object *)object, event);
  276. }
  277. return RT_FALSE;
  278. }
  279. static void timeout(struct rtgui_timer *timer, void *parameter)
  280. {
  281. struct rtgui_widget *widget;
  282. SYS_STE ret;
  283. if (!map)
  284. return;
  285. ret = snake_step(run_state, map);
  286. if (OVER == ret)
  287. return;
  288. if (FOOD == ret)
  289. {
  290. snake_len++;
  291. if (snake_len >= (map->width * map->height) / 3)
  292. {
  293. point_t start;
  294. start.x = snake_init_pointx;
  295. start.y = snake_init_pointy;
  296. run_state = SNAKE_DIR_DOWN;
  297. snake_len = snake_length_init;
  298. if (!snake_restart(&start, snake_len, run_state, map))
  299. {
  300. map_deinit(map);
  301. snake_deinit();
  302. map = RT_NULL;
  303. }
  304. }
  305. food_init(map, 1);
  306. }
  307. widget = RTGUI_WIDGET(parameter);
  308. snake_update(widget);
  309. }
  310. void snake_main(void)
  311. {
  312. struct rtgui_app *application;
  313. struct rtgui_win *win;
  314. rtgui_rect_t rect;
  315. application = rtgui_app_create("sanke_app");
  316. if (application != RT_NULL)
  317. {
  318. rtgui_get_screen_rect(&rect);
  319. rtgui_set_mainwin_rect(&rect);
  320. win = rtgui_mainwin_create(RT_NULL,
  321. "sanke_win",
  322. RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
  323. if (win == RT_NULL)
  324. {
  325. rt_kprintf("sanke_win create fail!\r\n");
  326. return;
  327. }
  328. rtgui_object_set_event_handler(RTGUI_OBJECT(win), event_handler);
  329. timer = rtgui_timer_create(RT_TICK_PER_SECOND / 2,
  330. RT_TIMER_FLAG_PERIODIC,
  331. timeout,
  332. (void *)win);
  333. rtgui_win_show(win, RT_TRUE);
  334. //Í˳öºó²Å·µ»Ø
  335. map_deinit(map);
  336. snake_deinit();
  337. food_deinit();
  338. rtgui_app_destroy(application);
  339. }
  340. }
  341. #include <finsh.h>
  342. FINSH_FUNCTION_EXPORT(snake_main, snake run)