add snake demo (run in realtouch)
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2439 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
60fc67cb3d
commit
5057b28523
|
@ -0,0 +1,20 @@
|
|||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
|
||||
# remove no need file.
|
||||
if GetDepend('RT_USING_RTGUI') == False:
|
||||
SrcRemove(src, 'sdl_fb.c')
|
||||
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_ELMFAT') == False:
|
||||
SrcRemove(src, 'sd_sim.c')
|
||||
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_UFFS') == False:
|
||||
SrcRemove(src, 'nand_sim.c')
|
||||
if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_JFFS2') == False:
|
||||
SrcRemove(src, 'sst25vfxx_mtd_sim.c')
|
||||
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
|
@ -0,0 +1,253 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <rtthread.h>
|
||||
#include "snake.h"
|
||||
|
||||
rt_list_t snake_head;
|
||||
SNAKE_DIR prevdir;
|
||||
|
||||
static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
|
||||
{
|
||||
if ( (SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
|
||||
|| (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
|
||||
|| (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
|
||||
|| (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
|
||||
)
|
||||
{
|
||||
prevdir = dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("dirction change error\n\r");
|
||||
}
|
||||
|
||||
return prevdir;
|
||||
}
|
||||
|
||||
static void across_XY(point_t *node, const map_t *map)
|
||||
{
|
||||
RT_ASSERT(node != RT_NULL && map != RT_NULL);
|
||||
|
||||
// 如果长度超出当前边框则可以穿越墙到对面
|
||||
node->x = (node->x + map->width) % map->width;
|
||||
node->y = (node->y + map->height) % map->height;
|
||||
}
|
||||
|
||||
static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
|
||||
{
|
||||
SYS_STE ret;
|
||||
point_t *pos;
|
||||
|
||||
RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);
|
||||
|
||||
pos = map->snake_flush;
|
||||
pos[0].x = pos[0].y = -1;
|
||||
pos[1].x = pos[1].y = -1;
|
||||
|
||||
ret = (SYS_STE)map->range[node->y * map->width + node->x];
|
||||
|
||||
if (FOOD == map->range[node->y * map->width + node->x])
|
||||
{
|
||||
// 吃一个食物增加一个节点
|
||||
snake_t *new = (snake_t*)rt_malloc(sizeof(snake_t));
|
||||
if (!new)
|
||||
return NORMAL;
|
||||
|
||||
pos[0] = *node;
|
||||
new->body = *node;
|
||||
rt_list_insert_after(&snake_head, &new->list);
|
||||
}
|
||||
else if (NORMAL == map->range[node->y * map->width + node->x])
|
||||
{
|
||||
// 将尾巴修改后拿到头部,其他不变
|
||||
rt_list_remove(&tail->list);
|
||||
map->range[tail->body.y * map->width + tail->body.x] = NORMAL;
|
||||
|
||||
pos[0] = *node;
|
||||
pos[1] = tail->body;
|
||||
|
||||
tail->body = *node;
|
||||
rt_list_insert_after(&snake_head, &tail->list);
|
||||
}
|
||||
|
||||
map->range[node->y * map->width + node->x] = OVER;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
map_t* map_init(rt_uint32_t width, rt_uint32_t heigth)
|
||||
{
|
||||
map_t *map = rt_malloc(sizeof(map_t));
|
||||
|
||||
if (map != RT_NULL)
|
||||
{
|
||||
map->range = rt_malloc(heigth * width);
|
||||
|
||||
if (!map->range)
|
||||
{
|
||||
rt_free(map);
|
||||
map = RT_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
map->width = width;
|
||||
map->height = heigth;
|
||||
memset(map->range, NORMAL, heigth * width);
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// 构造一条指定长度的蛇在指定点
|
||||
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
|
||||
{
|
||||
rt_int32_t i;
|
||||
rt_int32_t inc_x, inc_y;
|
||||
point_t old = *start;
|
||||
|
||||
rt_list_init(&snake_head);
|
||||
|
||||
if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
|
||||
{
|
||||
if (map->height <= length)
|
||||
return RT_FALSE;
|
||||
|
||||
inc_x = 0;
|
||||
inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1; // 反向延长身子,头部在指定位置
|
||||
old.y -= inc_y;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (map->width <= length)
|
||||
return RT_FALSE;
|
||||
|
||||
inc_y = 0;
|
||||
inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
|
||||
old.x -= inc_x;
|
||||
}
|
||||
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
snake_t *new = (snake_t*)rt_malloc(sizeof(snake_t));
|
||||
if (!new)
|
||||
return RT_FALSE;
|
||||
|
||||
new->body.y = inc_y + old.y;
|
||||
new->body.x = inc_x + old.x;
|
||||
|
||||
// 如果长度超出当前边框则可以穿越墙到对面
|
||||
across_XY(&new->body, map);
|
||||
|
||||
map->range[new->body.y * map->width + new->body.x] = OVER;
|
||||
|
||||
old = new->body;
|
||||
rt_list_insert_before(&snake_head, &new->list);
|
||||
}
|
||||
|
||||
prevdir = dir;
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
// 构造出食物
|
||||
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
|
||||
{
|
||||
point_t food;
|
||||
|
||||
#ifndef FOOD_TIMEOUT
|
||||
#define FOOD_TIMEOUT 10
|
||||
#endif
|
||||
|
||||
rt_uint32_t timeout, num;
|
||||
|
||||
num = 0;
|
||||
timeout = rt_tick_get();
|
||||
srand(rand());
|
||||
|
||||
map->food_flush[0].x = map->food_flush[0].y = -1;
|
||||
|
||||
do
|
||||
{
|
||||
food.x = rand() % map->width;
|
||||
food.y = rand() % map->height;
|
||||
|
||||
if (map->range[food.y * map->width + food.x] == NORMAL)
|
||||
{
|
||||
map->food_flush[0] = food;
|
||||
map->range[food.y * map->width + food.x] = FOOD;
|
||||
num++;
|
||||
}
|
||||
}
|
||||
while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void map_deinit(map_t *map)
|
||||
{
|
||||
if (map)
|
||||
{
|
||||
if (map->range)
|
||||
{
|
||||
rt_free(map->range);
|
||||
map->range = RT_NULL;
|
||||
}
|
||||
rt_free(map);
|
||||
}
|
||||
}
|
||||
|
||||
void snake_deinit(void)
|
||||
{
|
||||
snake_t *node;
|
||||
|
||||
while (!rt_list_isempty(&snake_head))
|
||||
{
|
||||
node = rt_list_entry(snake_head.prev, snake_t, list);
|
||||
rt_list_remove(&node->list);
|
||||
rt_free(node);
|
||||
}
|
||||
}
|
||||
|
||||
void food_deinit(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
|
||||
{
|
||||
snake_t *tail, *head;
|
||||
point_t node;
|
||||
|
||||
dir = dir_adjust(dir);
|
||||
|
||||
// 取出头尾两个节点,其他节点不需要改变
|
||||
tail = rt_list_entry(snake_head.prev, snake_t, list);
|
||||
head = rt_list_entry(snake_head.next, snake_t, list);
|
||||
|
||||
node = head->body;
|
||||
|
||||
// 构造一个新的蛇头坐标
|
||||
switch (dir)
|
||||
{
|
||||
case SNAKE_DIR_UP:
|
||||
case SNAKE_DIR_DOWN:
|
||||
node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
|
||||
break;
|
||||
case SNAKE_DIR_LEFT:
|
||||
case SNAKE_DIR_RIGHT:
|
||||
node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
|
||||
break;
|
||||
}
|
||||
across_XY(&node, map);
|
||||
|
||||
return node_update(tail, &node, map);
|
||||
}
|
||||
|
||||
rt_bool_t snake_restart(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
|
||||
{
|
||||
snake_deinit();
|
||||
memset(map->range, NORMAL, map->width * map->height);
|
||||
|
||||
return snake_init(start, length, dir, map);
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
#ifndef _SNAKE_H_
|
||||
#define _SNAKE_H_
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define snake_length_max 20 //最大蛇长
|
||||
#define snake_length_init 3 //初始化蛇长
|
||||
#define snake_room_size_hight 8 //房子高 0-255 建议>8
|
||||
#define snake_room_size_widht 8 //房子宽 0-255 建议>8
|
||||
|
||||
#define snake_init_pointx 1
|
||||
#define snake_init_pointy 2
|
||||
|
||||
typedef struct
|
||||
{
|
||||
rt_int32_t x, y;
|
||||
}point_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
rt_int32_t width; // max x
|
||||
rt_int32_t height; // max y
|
||||
rt_uint8_t *range; // map, map->range[y * map->width + x]
|
||||
point_t snake_flush[2];
|
||||
point_t food_flush[1];
|
||||
}map_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SNAKE_DIR_UP,
|
||||
SNAKE_DIR_DOWN,
|
||||
SNAKE_DIR_LEFT,
|
||||
SNAKE_DIR_RIGHT
|
||||
}SNAKE_DIR;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FOOD, // 吃到水果
|
||||
OVER, // 咬到自身
|
||||
NORMAL // 正常行动
|
||||
}SYS_STE;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
point_t body;
|
||||
rt_list_t list;
|
||||
}snake_t;
|
||||
|
||||
// 构造一个地图
|
||||
map_t* map_init(rt_uint32_t width, rt_uint32_t heigth);
|
||||
|
||||
// 构造一条指定长度的蛇在指定点
|
||||
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map);
|
||||
|
||||
// 构造出食物
|
||||
rt_bool_t food_init(map_t *map, rt_uint32_t max_num);
|
||||
|
||||
void map_deinit(map_t *map);
|
||||
|
||||
void snake_deinit(void);
|
||||
|
||||
void food_deinit(void);
|
||||
|
||||
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);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,425 @@
|
|||
#include <string.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <rtgui/rtgui.h>
|
||||
#include <rtgui/rtgui_app.h>
|
||||
#include <rtgui/widgets/container.h>
|
||||
#include <rtgui/widgets/window.h>
|
||||
#include <rtgui/widgets/button.h>
|
||||
|
||||
#include "snake.h"
|
||||
|
||||
#define LATTICE_SIZE (20)
|
||||
#define FOOD_MAX (8)
|
||||
|
||||
#define WALL_COLOR RTGUI_RGB(255, 0, 0)
|
||||
#define SNAKE_COLOR RTGUI_RGB(111, 0, 255)
|
||||
#define BACKGROUND_COLOR RTGUI_RGB(153, 153, 0)
|
||||
#define FOOD_COLOR RTGUI_RGB(0, 111, 111)
|
||||
#define min(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
static rtgui_timer_t *timer;
|
||||
static rt_size_t room_size_x, room_size_y;
|
||||
static rt_size_t lattice_size_x, lattice_size_y;
|
||||
static struct rtgui_rect room_rect, lattice_rect;
|
||||
|
||||
map_t* map;
|
||||
SNAKE_DIR run_state;
|
||||
rt_int32_t snake_len;
|
||||
rt_int32_t food_num;
|
||||
|
||||
static void snake_fill_lattice(struct rtgui_dc *dc,
|
||||
rt_uint32_t x,
|
||||
rt_uint32_t y,
|
||||
rtgui_color_t color)
|
||||
{
|
||||
struct rtgui_rect rect;
|
||||
|
||||
// coordinate conversion
|
||||
y = (lattice_size_y - 1) - y;
|
||||
|
||||
RTGUI_DC_BC(dc) = color;
|
||||
|
||||
rect.x1 = lattice_rect.x1 + (LATTICE_SIZE * x);
|
||||
rect.x2 = rect.x1 + LATTICE_SIZE;
|
||||
rect.x1 += 2;
|
||||
|
||||
rect.y1 = lattice_rect.y1 + (LATTICE_SIZE * y);
|
||||
rect.y2 = rect.y1 + LATTICE_SIZE;
|
||||
rect.y1 += 2;
|
||||
|
||||
rtgui_dc_fill_rect(dc, &rect);
|
||||
}
|
||||
|
||||
static void snake_draw(struct rtgui_widget *widget)
|
||||
{
|
||||
struct rtgui_dc *dc;
|
||||
struct rtgui_rect rect;
|
||||
rt_uint32_t i;
|
||||
|
||||
// rt_kprintf("snake_draw\r\n");
|
||||
|
||||
dc = rtgui_dc_begin_drawing(widget);
|
||||
if (dc == RT_NULL)
|
||||
{
|
||||
rt_kprintf("dc == RT_NULL\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* get room size, run once frist. */
|
||||
if((room_size_x == 0) || (room_size_y == 0))
|
||||
{
|
||||
rt_size_t tmp;
|
||||
|
||||
rtgui_widget_get_rect(widget, &rect);
|
||||
rt_kprintf("rect => x1:%d x2:%d, y1:%d y2:%d\r\n", rect.x1, rect.x2, rect.y1, rect.y2);
|
||||
|
||||
room_size_x = rect.x2 - rect.x1;
|
||||
room_size_y = rect.y2 - rect.y1;
|
||||
memcpy(&room_rect, &rect, sizeof(struct rtgui_rect));
|
||||
rt_kprintf("room_rect => x1:%d x2:%d, y1:%d y2:%d\r\n",
|
||||
room_rect.x1, room_rect.x2,
|
||||
room_rect.y1, room_rect.y2);
|
||||
|
||||
lattice_size_x = (room_rect.x2 - room_rect.x1) / LATTICE_SIZE;
|
||||
lattice_size_y = (room_rect.y2 - room_rect.y1) / LATTICE_SIZE;
|
||||
lattice_size_x -= 2;
|
||||
lattice_size_y -= 2;
|
||||
rt_kprintf("lattice_size_x:%d lattice_size_y:%d\r\n",
|
||||
lattice_size_x,
|
||||
lattice_size_y);
|
||||
|
||||
tmp = (room_rect.x2 - room_rect.x1) - (LATTICE_SIZE * lattice_size_x);
|
||||
lattice_rect.x1 = room_rect.x1 + (tmp / 2);
|
||||
lattice_rect.x2 = lattice_rect.x1 + (LATTICE_SIZE * lattice_size_x);
|
||||
|
||||
tmp = (room_rect.y2 - room_rect.y1) - (LATTICE_SIZE * lattice_size_y);
|
||||
lattice_rect.y1 = room_rect.y1 + (tmp / 2);
|
||||
lattice_rect.y2 = lattice_rect.y1 + (LATTICE_SIZE * lattice_size_y);
|
||||
rt_kprintf("lattice_rect => x1:%d x2:%d, y1:%d y2:%d\r\n",
|
||||
lattice_rect.x1, lattice_rect.x2,
|
||||
lattice_rect.y1, lattice_rect.y2);
|
||||
|
||||
/* create snake. */
|
||||
{
|
||||
point_t start;
|
||||
map = map_init(lattice_size_x, lattice_size_y);
|
||||
if (map != RT_NULL)
|
||||
{
|
||||
start.x = snake_init_pointx;
|
||||
start.y = snake_init_pointy;
|
||||
run_state = SNAKE_DIR_DOWN;
|
||||
|
||||
if (snake_init(&start, snake_length_init, run_state, map))
|
||||
{
|
||||
food_num = 1;
|
||||
food_init(map, food_num);
|
||||
}
|
||||
else
|
||||
{
|
||||
map_deinit(map);
|
||||
map = RT_NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RTGUI_DC_BC(dc) = BACKGROUND_COLOR;
|
||||
rtgui_dc_fill_rect(dc, &room_rect);
|
||||
|
||||
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
|
||||
rect.x2 += 1;
|
||||
rect.y2 += 1;
|
||||
RTGUI_DC_FC(dc) = WALL_COLOR;
|
||||
rtgui_dc_draw_rect(dc, &rect);
|
||||
|
||||
for(i=1; i<lattice_size_y; i++)
|
||||
{
|
||||
// rtgui_dc_draw_horizontal_line(struct rtgui_dc* dc, int x1, int x2, int y);
|
||||
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
|
||||
rect.x1 += 1;
|
||||
rect.x2 -= 1;
|
||||
rtgui_dc_draw_horizontal_line(dc, rect.x1, rect.x2,
|
||||
rect.y1 + (LATTICE_SIZE * i));
|
||||
}
|
||||
|
||||
for(i=1; i<lattice_size_x; i++)
|
||||
{
|
||||
// rtgui_dc_draw_vertical_line(struct rtgui_dc* dc, int x, int y1, int y2);
|
||||
memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect));
|
||||
rect.y1 += 1;
|
||||
rect.y2 -= 1;
|
||||
rtgui_dc_draw_vertical_line(dc, rect.x1 + (LATTICE_SIZE * i),
|
||||
rect.y1, rect.y2);
|
||||
}
|
||||
|
||||
/* draw snake. */
|
||||
{
|
||||
rt_uint32_t x, y;
|
||||
for (y=0; y<map->height; y++)
|
||||
{
|
||||
for (x=0; x<map->width; x++)
|
||||
{
|
||||
switch (map->range[y * map->width + x])
|
||||
{
|
||||
case NORMAL:
|
||||
break;
|
||||
case FOOD:
|
||||
snake_fill_lattice(dc, x, y, FOOD_COLOR);
|
||||
break;
|
||||
case OVER:
|
||||
snake_fill_lattice(dc, x, y, SNAKE_COLOR);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
rtgui_dc_end_drawing(dc);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void snake_update(struct rtgui_widget *widget)
|
||||
{
|
||||
struct rtgui_dc *dc;
|
||||
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)
|
||||
{
|
||||
rt_kprintf("dc == RT_NULL\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for(i=0; i<3; i++)
|
||||
{
|
||||
if(i < 2)
|
||||
{
|
||||
x = map->snake_flush[i].x;
|
||||
y = map->snake_flush[i].y;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = map->food_flush[0].x;
|
||||
y = map->food_flush[0].y;
|
||||
}
|
||||
|
||||
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:
|
||||
snake_fill_lattice(dc, x, y, BACKGROUND_COLOR);
|
||||
break;
|
||||
case FOOD:
|
||||
snake_fill_lattice(dc, x, y, FOOD_COLOR);
|
||||
break;
|
||||
case OVER:
|
||||
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;
|
||||
}
|
||||
|
||||
static void snake_handler(struct rtgui_widget *widget, rtgui_event_t *event)
|
||||
{
|
||||
struct rtgui_event_kbd* ekbd;
|
||||
|
||||
ekbd = (struct rtgui_event_kbd*) event;
|
||||
if (ekbd->type == RTGUI_KEYDOWN)
|
||||
{
|
||||
switch (ekbd->key)
|
||||
{
|
||||
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:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event)
|
||||
{
|
||||
struct rtgui_widget *widget = RTGUI_WIDGET(object);
|
||||
|
||||
rt_kprintf("event_handler\r\n");
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
static void timeout(struct rtgui_timer *timer, void *parameter)
|
||||
{
|
||||
struct rtgui_widget *widget;
|
||||
|
||||
if (!map)
|
||||
return;
|
||||
|
||||
if (snake_step(run_state, map) == FOOD)
|
||||
{
|
||||
// food--;
|
||||
snake_len++;
|
||||
if (snake_len >= (map->width * map->height) / 3)
|
||||
{
|
||||
point_t start;
|
||||
|
||||
start.x = snake_init_pointx;
|
||||
start.y = snake_init_pointy;
|
||||
run_state = SNAKE_DIR_DOWN;
|
||||
snake_len = snake_length_init;
|
||||
|
||||
if (!snake_restart(&start, snake_len, run_state, map))
|
||||
{
|
||||
map_deinit(map);
|
||||
map = RT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
food_init(map, 1);
|
||||
}
|
||||
|
||||
widget = RTGUI_WIDGET(parameter);
|
||||
// snake_draw(widget);
|
||||
snake_update(widget);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void snake_main(void)
|
||||
{
|
||||
struct rtgui_app* application;
|
||||
struct rtgui_win* win;
|
||||
|
||||
application = rtgui_app_create(rt_thread_self(), "sanke_app");
|
||||
if (application != RT_NULL)
|
||||
{
|
||||
win = rtgui_mainwin_create(RT_NULL,
|
||||
"sanke_win",
|
||||
RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
|
||||
if(win == RT_NULL)
|
||||
{
|
||||
rt_kprintf("sanke_win create fail!\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
rtgui_object_set_event_handler(RTGUI_OBJECT(win), event_handler);
|
||||
|
||||
timer = rtgui_timer_create(RT_TICK_PER_SECOND / 2,
|
||||
RT_TIMER_FLAG_PERIODIC,
|
||||
timeout,
|
||||
(void *)win);
|
||||
|
||||
rtgui_win_show(win, RT_TRUE);
|
||||
|
||||
//Í˳öºó²Å·µ»Ø
|
||||
map_deinit(map);
|
||||
snake_deinit();
|
||||
food_deinit();
|
||||
rtgui_app_destroy(application);
|
||||
}
|
||||
}
|
||||
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(snake_main, snake run)
|
Loading…
Reference in New Issue