From 90c3859e32704c6a5930ed52ce7634b5db70654d Mon Sep 17 00:00:00 2001 From: "goprife@gmail.com" Date: Fri, 23 Nov 2012 16:29:39 +0000 Subject: [PATCH] update snake code in bsp/simulator git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2443 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/simulator/snake/snake.c | 14 ++++++ bsp/simulator/snake/snake_gui.c | 82 ++++++++++----------------------- 2 files changed, 39 insertions(+), 57 deletions(-) diff --git a/bsp/simulator/snake/snake.c b/bsp/simulator/snake/snake.c index 16a3a337e..a3ec77445 100644 --- a/bsp/simulator/snake/snake.c +++ b/bsp/simulator/snake/snake.c @@ -3,6 +3,12 @@ #include #include "snake.h" +#define ASSERT_RET(x, ret) \ + do{ \ + if (x) \ + return ret; \ + }while(0) + rt_list_t snake_head; SNAKE_DIR prevdir; @@ -106,6 +112,8 @@ rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir rt_int32_t inc_x, inc_y; point_t old = *start; + ASSERT_RET(!map || !start, RT_FALSE); + rt_list_init(&snake_head); if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN) @@ -161,6 +169,8 @@ rt_bool_t food_init(map_t *map, rt_uint32_t max_num) rt_uint32_t timeout, num; + ASSERT_RET(!map, RT_FALSE); + num = 0; timeout = rt_tick_get(); srand(rand()); @@ -219,6 +229,8 @@ SYS_STE snake_step(SNAKE_DIR dir, map_t *map) snake_t *tail, *head; point_t node; + ASSERT_RET(!map, RT_FALSE); + dir = dir_adjust(dir); // 取出头尾两个节点,其他节点不需要改变 @@ -246,6 +258,8 @@ SYS_STE snake_step(SNAKE_DIR dir, map_t *map) rt_bool_t snake_restart(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map) { + ASSERT_RET(!map || !start, RT_FALSE); + snake_deinit(); memset(map->range, NORMAL, map->width * map->height); diff --git a/bsp/simulator/snake/snake_gui.c b/bsp/simulator/snake/snake_gui.c index d197fa40c..8cbf5a952 100644 --- a/bsp/simulator/snake/snake_gui.c +++ b/bsp/simulator/snake/snake_gui.c @@ -13,9 +13,10 @@ #define FOOD_MAX (8) #define WALL_COLOR RTGUI_RGB(255, 0, 0) -#define SNAKE_COLOR RTGUI_RGB(111, 0, 255) +#define SNAKE_COLOR RTGUI_RGB(0, 100, 200) +#define SNAKE_HEAD_COLOR RTGUI_RGB(180, 70, 130) #define BACKGROUND_COLOR RTGUI_RGB(153, 153, 0) -#define FOOD_COLOR RTGUI_RGB(0, 111, 111) +#define FOOD_COLOR RTGUI_RGB(128, 0, 0) #define min(a, b) ((a) < (b) ? (a) : (b)) static rtgui_timer_t *timer; @@ -27,6 +28,7 @@ map_t* map; SNAKE_DIR run_state; rt_int32_t snake_len; rt_int32_t food_num; +point_t second_node; static void snake_fill_lattice(struct rtgui_dc *dc, rt_uint32_t x, @@ -57,8 +59,6 @@ static void snake_draw(struct rtgui_widget *widget) struct rtgui_rect rect; rt_uint32_t i; -// rt_kprintf("snake_draw\r\n"); - dc = rtgui_dc_begin_drawing(widget); if (dc == RT_NULL) { @@ -135,7 +135,6 @@ static void snake_draw(struct rtgui_widget *widget) for(i=1; iheight; y++) { for (x=0; xwidth; x++) @@ -168,7 +168,17 @@ static void snake_draw(struct rtgui_widget *widget) snake_fill_lattice(dc, x, y, FOOD_COLOR); break; case OVER: - snake_fill_lattice(dc, x, y, SNAKE_COLOR); + if (first_node) + { + first_node = RT_FALSE; + second_node.x = x; + second_node.y = y; + snake_fill_lattice(dc, x, y, SNAKE_HEAD_COLOR); + } + else + { + snake_fill_lattice(dc, x, y, SNAKE_COLOR); + } break; } } @@ -187,8 +197,6 @@ static void snake_update(struct rtgui_widget *widget) rt_int32_t x, y; rt_uint32_t i; -// rt_kprintf("snake_update\r\n"); - dc = rtgui_dc_begin_drawing(widget); if (dc == RT_NULL) { @@ -196,6 +204,9 @@ static void snake_update(struct rtgui_widget *widget) return; } + snake_fill_lattice(dc, second_node.x, second_node.y, SNAKE_COLOR); + second_node = map->snake_flush[0]; + for(i=0; i<3; i++) { if(i < 2) @@ -211,10 +222,6 @@ static void snake_update(struct rtgui_widget *widget) if((x >= 0) && (y >= 0)) { -// rt_kprintf("snake_flush[%d].x:%d, snake_flush[%d].y:%d\r\n", -// i, x, -// i, y); - switch (map->range[(map->width * y) + x]) { case NORMAL: @@ -224,35 +231,14 @@ static void snake_update(struct rtgui_widget *widget) snake_fill_lattice(dc, x, y, FOOD_COLOR); break; case OVER: - snake_fill_lattice(dc, x, y, SNAKE_COLOR); + if (0 == i) + snake_fill_lattice(dc, x, y, SNAKE_HEAD_COLOR); + else + snake_fill_lattice(dc, x, y, SNAKE_COLOR); break; } } } -// rt_kprintf("\r\n"); - - -// if((map->snake_flush[1].x >= 0) && (map->snake_flush[1].y >= 0)) -// { -// rt_kprintf("snake_flush[1].x:%d, snake_flush[1].y:%d\r\n", -// map->snake_flush[1].x, map->snake_flush[1].y); -// snake_fill_lattice(dc, -// map->snake_flush[1].x, -// map->snake_flush[1].y, -// SNAKE_COLOR); -// } - -// x = map->food_flush[0].x; -// y = map->food_flush[0].y; -// if((map->food_flush[0].x >= 0) && (map->food_flush[0].y >= 0)) -// { -// rt_kprintf("food_flush[0].x:%d, food_flush[0].y:%d\r\n", -// map->food_flush[0].x, map->food_flush[0].y); -// snake_fill_lattice(dc, -// map->food_flush[0].x, -// map->food_flush[0].y, -// FOOD_COLOR); -// } rtgui_dc_end_drawing(dc); return; @@ -269,26 +255,18 @@ static void snake_handler(struct rtgui_widget *widget, rtgui_event_t *event) { case RTGUIK_UP: rt_kprintf("RTGUIK_UP\r\n"); - //snake_key(1); - //snake_step(map, UP); run_state = SNAKE_DIR_UP; break; case RTGUIK_DOWN: rt_kprintf("RTGUIK_DOWN\r\n"); - //snake_key(2); - //snake_step(map, DOWN); run_state = SNAKE_DIR_DOWN; break; case RTGUIK_LEFT: rt_kprintf("RTGUIK_LEFT\r\n"); - //snake_key(3); - //snake_step(map, LEFT); run_state = SNAKE_DIR_LEFT; break; case RTGUIK_RIGHT: rt_kprintf("RTGUIK_RIGHT\r\n"); - //snake_key(4); - //snake_step(map, RIGHT); run_state = SNAKE_DIR_RIGHT; break; default: @@ -306,7 +284,6 @@ static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event if (event->type == RTGUI_EVENT_PAINT) { rt_kprintf("RTGUI_EVENT_PAINT\r\n"); -// rtgui_container_event_handler(object, event); rtgui_win_event_handler((struct rtgui_object*)object, event); snake_draw(widget); rtgui_timer_start(timer); @@ -314,7 +291,6 @@ static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event else if (event->type == RTGUI_EVENT_SHOW) { rt_kprintf("RTGUI_EVENT_SHOW\r\n"); -// rtgui_container_event_handler(object, event); rtgui_win_event_handler((struct rtgui_object*)object, event); snake_draw(widget); rtgui_timer_start(timer); @@ -322,28 +298,23 @@ static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event else if (event->type == RTGUI_EVENT_HIDE) { rt_kprintf("RTGUI_EVENT_HIDE\r\n"); -// rtgui_container_event_handler(object, event); rtgui_win_event_handler((struct rtgui_object*)object, event); rtgui_timer_stop(timer); } else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE) { rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n"); -// rtgui_container_event_handler(object, event); rtgui_win_event_handler((struct rtgui_object*)object, event); rtgui_timer_stop(timer); } else if (event->type == RTGUI_EVENT_KBD) { -// rt_kprintf("RTGUI_EVENT_KBD\r\n"); -// rtgui_container_event_handler(object, event); rtgui_win_event_handler((struct rtgui_object*)object, event); snake_handler(widget, event); } else { rt_kprintf("event->type:%d\r\n", event->type); -// return rtgui_container_event_handler(object, event); return rtgui_win_event_handler((struct rtgui_object*)object, event); } @@ -359,7 +330,6 @@ static void timeout(struct rtgui_timer *timer, void *parameter) if (snake_step(run_state, map) == FOOD) { - // food--; snake_len++; if (snake_len >= (map->width * map->height) / 3) { @@ -373,6 +343,7 @@ static void timeout(struct rtgui_timer *timer, void *parameter) if (!snake_restart(&start, snake_len, run_state, map)) { map_deinit(map); + snake_deinit(); map = RT_NULL; } } @@ -381,10 +352,7 @@ static void timeout(struct rtgui_timer *timer, void *parameter) } widget = RTGUI_WIDGET(parameter); -// snake_draw(widget); snake_update(widget); - - return; } void snake_main(void) -- GitLab