剛拿到星火一號板子想搞事情,逛論壇發(fā)現(xiàn) @zym_0208 發(fā)布了一個貪吃蛇的demo,于是下載下來玩了一下發(fā)現(xiàn)有些許bug,于是我在他的基礎上修改了一半,且把邏輯優(yōu)化了一下
工程中使用到的devices
上下左右四個按鈕,以及LCD屏幕
獲取pin
#define PIN_KEY0 GET_PIN(C, 0)
#define PIN_KEY1 GET_PIN(C, 1)
#define PIN_KEY2 GET_PIN(C, 4)
#define PIN_KEY3 GET_PIN(C, 5)
#define PIN_LED_R GET_PIN(F, 12)
在main函數中設置pin模式,綁定觸發(fā)模式,事件,使能中斷
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY3, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(PIN_KEY0, PIN_IRQ_MODE_FALLING, keyDown, (void*)3);
rt_pin_attach_irq(PIN_KEY1, PIN_IRQ_MODE_FALLING, keyDown, (void*)2);
rt_pin_attach_irq(PIN_KEY2, PIN_IRQ_MODE_FALLING, keyDown, (void*)4);
rt_pin_attach_irq(PIN_KEY3, PIN_IRQ_MODE_FALLING, keyDown, (void*)1);
rt_pin_irq_enable(PIN_KEY0, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY1, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY2, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY3, PIN_IRQ_ENABLE);
貪吃蛇的游戲設置
圖案屬性,雖然lcd屏幕的顯示上限很高,但是本工程只用簡單的字符打印代替圖案,所有字符都是16*16的正方形,所以下面所有東西的位置(x,y)都是16的倍數,如果想要更高端的顯示,可以去了解一下lvgl庫
蛇的屬性:
struct {
int speed;
int len;
int x[SNAKESIZE];
int y[SNAKESIZE];
}snake;
食物屬性:
struct {
int x;
int y;
}food;
游戲邏輯
蛇碰到墻會GAMEOVER
蛇頭碰到蛇身會GAMEOVER
蛇頭碰到食物會變長一個單位
沒有操作時蛇會延記錄的方向移動一個單位,我設置為300ms的delay
GAMEOVER后會顯示分數
具體實現(xiàn)
每一個循環(huán),不管有沒有吃東西,直接增加蛇長度,也就是在移動方向上頭前面加一個頭(抽象說法),再把蛇身體數組集體往前移一格,把原來的尾巴設為“ ”,然后再進行判斷有沒有吃到東西。
lcd_show_string(snake.x[0], snake.y[0],16,"@");
lcd_show_string(snake.x[snake.len - 1], snake.y[snake.len - 1],16," ");
int tailx = snake.x[snake.len-1];
int taily = snake.y[snake.len-1];
for(int i = snake.len - 1; i > 0; i--){
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
吃到東西了,那就把尾巴再生成出來,同時搞個循環(huán)生成食物,這里的邏輯就是食物不能生在邊框,也不能生在蛇已有的身體上,就這么一直生下去直到生成合法食物。
//新生成一個合法食物
while (1)
{
int sameRegion = 0;
food.x = rand() % (MAPWIDTH/16)*16+16 ;
food.y = rand() % (MAPHEIGHT/16)*16 + 16;
//生成的食物橫坐標的奇偶必須和初試時蛇頭所在坐標的奇偶一致,因為一個字符占兩個字節(jié)位置,不一致
//會導致吃食物的時候只吃到一半
//檢查是否食物生成到邊上
if(food.x % 2 == 0 && food.x>16 && food.x16 &&food.y {
//檢查是否食物與蛇身體重合
for (int i = 0; i < snake.len; i++)
{
if (snake.x[i] == food.x && snake.y[i] == food.y)
{
sameRegion = 1;
break;
}
}
}
else {
sameRegion = 1;
}
if(!sameRegion){
break;
}
}
沒吃到東西就無事發(fā)生,因為我們已經在一開始把尾巴設空過了
完整代碼
/*
Copyright (c) 2006-2021, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2023-5-10 ShiHao first version
/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PIN_KEY0 GET_PIN(C, 0)
#define PIN_KEY1 GET_PIN(C, 1)
#define PIN_KEY2 GET_PIN(C, 4)
#define PIN_KEY3 GET_PIN(C, 5)
#define PIN_LED_R GET_PIN(F, 12)
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include
#define SNAKESIZE 100//蛇的身體最大節(jié)數
#define MAPWIDTH 240 //寬度
#define MAPHEIGHT 240//高度
volatile int key = 3;
int score = 0;
struct {
int x;
int y;
}food;
struct {
int speed;
int len;
int x[SNAKESIZE];
int y[SNAKESIZE];
}snake;
void drawMap()
{
//打印上下邊框
for (int i = 0; i <= MAPWIDTH/16+4; i++)
{
//將光標移動依次到(i,0)處打印上邊框
lcd_show_string(i12,0,16,"#");
//將光標移動依次到(i,MAPHEIGHT)處打印下邊框
lcd_show_string(i*12,MAPHEIGHT-16,16,"#");
}
//打印左右邊框
for (int i = 1; i < MAPHEIGHT/16; i++)
{
//將光標移動依次到(0,i)處打印左邊框
lcd_show_string(0,i*16,16,"#");
//將光標移動依次到(MAPWIDTH, i)處打印左邊框
lcd_show_string(MAPHEIGHT-16,i*16,16,"#");
}
//隨機生成初始食物
while (1)
{
srand((unsigned int)time(NULL));
food.x = rand() % (MAPWIDTH/16)*16+16 ;
food.y = rand() % (MAPHEIGHT/16)*16+16;
if (food.x % 2 == 0){
if(food.x>16 && food.x16 &&food.y }
}
lcd_show_string(food.x, food.y,16,"*");
//初始化蛇的屬性
snake.len = 3;
snake.speed = 16;
//在屏幕中間生成蛇頭
snake.x[0] = 160;//x坐標為偶數
snake.y[0] = 160;
//打印蛇頭
lcd_show_string(snake.x[0], snake.y[0],16,"@");
//生成初始的蛇身
for (int i = 1; i < snake.len; i++)
{
//蛇身的打印,縱坐標不變,橫坐標為上一節(jié)蛇身的坐標值+16
snake.x[i] = (snake.x[i - 1] + 16);
snake.y[i] = snake.y[i - 1];
lcd_show_string(snake.x[i], snake.y[i],16,"#");
}
return;
}
void handleFood()
{
srand(time(RT_NULL));
lcd_show_string(snake.x[0], snake.y[0],16,"@");
lcd_show_string(snake.x[snake.len - 1], snake.y[snake.len - 1],16," ");
int tailx = snake.x[snake.len-1];
int taily = snake.y[snake.len-1];
for(int i = snake.len - 1; i > 0; i--){
snake.x[i] = snake.x[i-1];
snake.y[i] = snake.y[i-1];
}
if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇頭碰到食物
{
snake.x[snake.len] = tailx;
snake.y[snake.len] = taily;
lcd_show_string(tailx, taily,16,"#");
snake.len++;//吃到食物,蛇身長度加1
score += 10;//每個食物得10分
//新生成一個合法食物
while (1)
{
int sameRegion = 0;
food.x = rand() % (MAPWIDTH/16)*16+16 ;
food.y = rand() % (MAPHEIGHT/16)*16 + 16;
//生成的食物橫坐標的奇偶必須和初試時蛇頭所在坐標的奇偶一致,因為一個字符占兩個字節(jié)位置,不一致
//會導致吃食物的時候只吃到一半
//檢查是否食物生成到邊上
if(food.x % 2 == 0 && food.x>16 && food.x16 &&food.y {
//檢查是否食物與蛇身體重合
for (int i = 0; i < snake.len; i++)
{
if (snake.x[i] == food.x && snake.y[i] == food.y)
{
sameRegion = 1;
break;
}
}
}
else {
sameRegion = 1;
}
if(!sameRegion){
break;
}
}
lcd_show_string(food.x, food.y,16,"*");
}
rt_kprintf("new cycle!");
int pre_key = key ;//記錄前一個按鍵的方向
//判斷蛇頭應該往哪個方向移動
switch (pre_key)
{
case 3:
snake.x[0] -= snake.speed;//往左
break;
case 4:
snake.x[0] += snake.speed;//往右
break;
case 1:
snake.y[0]=snake.y[0]-snake.speed;//往上
break;
case 2:
snake.y[0]=snake.y[0]+snake.speed;//往下
break;
}
return;
}
void whereHead(){
rt_kprintf("x:%d ;",snake.x[0]);
rt_kprintf("y:%dn",snake.y[0]);
}
rt_bool_t snakeStatus()
{
whereHead();
//蛇頭碰到上下邊界,游戲結束
if (snake.y[0] <= 0|| snake.y[0] >= MAPHEIGHT-16)
return RT_ERROR;
//蛇頭碰到左右邊界,游戲結束
if (snake.x[0] <= 0 || snake.x[0] >= MAPWIDTH-16)
return RT_ERROR;
//蛇頭碰到蛇身,游戲結束
for (int i = 1; i < snake.len; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
return RT_ERROR;
}
return RT_EOK;
}
void keyDown(void *args)
{
rt_kprintf("key %d rn",(int)args);
key = (int)args;
}
int main(void)
{
drawMap();
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(PIN_KEY3, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(PIN_KEY0, PIN_IRQ_MODE_FALLING, keyDown, (void*)3);
rt_pin_attach_irq(PIN_KEY1, PIN_IRQ_MODE_FALLING, keyDown, (void*)2);
rt_pin_attach_irq(PIN_KEY2, PIN_IRQ_MODE_FALLING, keyDown, (void*)4);
rt_pin_attach_irq(PIN_KEY3, PIN_IRQ_MODE_FALLING, keyDown, (void*)1);
rt_pin_irq_enable(PIN_KEY0, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY1, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY2, PIN_IRQ_ENABLE);
rt_pin_irq_enable(PIN_KEY3, PIN_IRQ_ENABLE);
while (1)
{
if (snakeStatus())
break;
handleFood();
rt_thread_mdelay(300);
}
lcd_show_string(MAPWIDTH / 2-32, MAPHEIGHT / 2,16,"Game Over!");
lcd_show_string(MAPWIDTH / 2-32, MAPHEIGHT / 2+16,16,"Score:");
lcd_show_num(MAPWIDTH / 2+16, MAPHEIGHT / 2+16,score,2, 16);
rt_thread_mdelay(5000);
return 0;
}
-
LCD屏
+關注
關注
0文章
122瀏覽量
15424 -
字符串
+關注
關注
1文章
578瀏覽量
20510 -
RT-Thread
+關注
關注
31文章
1286瀏覽量
40102 -
LVGL
+關注
關注
1文章
83瀏覽量
2959
發(fā)布評論請先 登錄
相關推薦
評論