按键控制,液晶屏显示
外接按键原理图,接单片机引脚PC10,PC11
开机动画
游戏开始界面
单片机源程序如下:
- #include "stm32f10x.h"
- #include <stdio.h>
- #include "./key/bsp_exti.h"
- #include "./usart/bsp_usart.h"
- #include "./lcd/bsp_ili9341_lcd.h"
- #include "./SysTick/bsp_SysTick.h"
- #include "./flash/bsp_spi_flash.h"
- #include "./key/bsp_key.h"
- static void LCD_Test(void);
- uint32_t time = 0;
- char str[20];
- uint16_t key_flag=0;
- uint16_t tiaozhuan=0;
- char dispBuff[100];
- #define SNAKE_Max_Long 100
- static void Delay ( __IO uint32_t nCount );
- void LCD_Test(void);
- void boot_animation(void);
- void play(void);
- struct Snake
- {
- uint16_t X[SNAKE_Max_Long];
- uint16_t Y[SNAKE_Max_Long]; //最大长度
- u8 Long;
- u8 Life;// 0活着 1死亡
- u8 Direction;//蛇移动的方向
- }snake; //蛇的结构体
- struct Food
- {
- u8 X;
- u8 Y;
- u8 Yes; //0有食物 1需要出现食物
- }food; //食物结构体
- struct Game
- {
- u16 Score;
- u8 Life;
- }game; 游戏结构体
- void KEY1_IRQHandler(void)
- {
- if(EXTI_GetITStatus(KEY1_INT_EXTI_LINE) != RESET) //判断按键中断
- {
- tiaozhuan=1; 跳转到游戏开始界面
- key_flag=2;
- if(key_flag==2&&snake.Direction!=1)
- snake.Direction=2; //看下面程序对方向的描述
- EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE); //清除标志位
- }
- }
- void KEY2_IRQHandler(void) //同按键1一样原理
- {
- if(EXTI_GetITStatus(KEY2_INT_EXTI_LINE) != RESET)
- {
- key_flag=1;
- if(key_flag==1&&snake.Direction!=2)
- snake.Direction=1;
- EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE);
- }
-
- if(EXTI_GetITStatus(KEY3_INT_EXTI_LINE) != RESET)
- {
- key_flag=3;
- if(key_flag==3&&snake.Direction!=4)
- snake.Direction=3;
- EXTI_ClearITPendingBit(KEY3_INT_EXTI_LINE);
- }
-
- if(EXTI_GetITStatus(KEY4_INT_EXTI_LINE) != RESET)
- {
- key_flag=4;
- if(key_flag==4&&snake.Direction!=3)
- snake.Direction=4;
- EXTI_ClearITPendingBit(KEY4_INT_EXTI_LINE);
- }
- }
- void gameover() //游戏结束函数
- {
- LCD_SetTextColor(RED);
- ILI9341_DispStringLine_EN(LINE(10)," game over!");
- }
- void finish() //完成挑战函数
- {
- LCD_SetTextColor(RED);
- sprintf(dispBuff," 恭喜你完成挑战");
- ILI9341_DispStringLine_EN_CH(LINE(10),dispBuff);
- }
- void play(void) //开始游戏函数
- {
- u16 i;
- snake.Long=2; //初始化蛇长为2
- snake.Life=0;
- snake.Direction=1;//蛇的起始方向定义为右
- game.Life=1;//蛇的生命值
- food.Yes=1;//出现新食物
- snake.X[0]=0;snake.Y[0]=50; 初始化蛇坐标
- snake.X[1]=10;snake.Y[1]=50;
- ILI9341_Clear(90,130,60,60);
- ILI9341_Clear(80,120,80,80);
- ILI9341_Clear(70,110,100,100);
- ILI9341_Clear(60,100,120,120);
- ILI9341_Clear(50,90,140,140);
- ILI9341_Clear(40,80,160,160);
- ILI9341_Clear(30,70,180,180);
- ILI9341_Clear(20,60,200,200);
- ILI9341_Clear(10,50,220,220);
- while(1)
- {
- LCD_SetColors(CYAN,BLACK);
- sprintf(dispBuff," 贪吃蛇游戏");
- ILI9341_DispStringLine_EN_CH(LINE(1),dispBuff);
-
- sprintf(dispBuff,"分数 : %d ",game.Score);
- LCD_SetColors(WHITE,BLACK);
- ILI9341_DispStringLine_EN_CH(LINE(18),dispBuff);
-
- LCD_SetColors(WHITE,BLACK);
- ILI9341_DrawRectangle(0,40,240,240,0);
- if(food.Yes==1)
- {
- food.X=rand()%(230/10)*10;
- food.Y=rand()%(230/10)*10; //随机函数放置食物
- if((food.X<240)&&(food.Y<260&&food.Y>40)) //如果食物不在范围内,则重新放置
- {
- food.Yes=0;
- }
- else
- {
- food.Yes=1;
- }
- }
- if(food.Yes==0)
- {
- LCD_SetColors(BLUE,BLACK);
-
- ILI9341_DrawRectangle (food.X,food.Y,10,10,1); 显示食物
- }
-
- for(i=snake.Long-1;i>0;i--) //刷新蛇
- {
- snake.X[i]=snake.X[i-1];
- snake.Y[i]=snake.Y[i-1];
- }
- switch(snake.Direction) //对方向的描述,按键中断控制
- {
- case 1:snake.X[0]+=10;break;//向右运动
- case 2:snake.X[0]-=10;break;//向左运动
- case 3:snake.Y[0]-=10;break;//向上运动
- case 4:snake.Y[0]+=10;break;//向下运动
- }
- for(i=0;i<snake.Long;i++)
-
- ILI9341_DrawRectangle(snake.X[i],snake.Y[i],10,10,1);
- Delay(0xFFFFF);
-
- LCD_SetTextColor (BLACK) ;
- ILI9341_DrawRectangle(snake.X[snake.Long-1],snake.Y[snake.Long-1],10,10,1);//消除蛇身
- LCD_SetTextColor (RED) ;
- //判断是否撞墙
- if(snake.X[0]<0||snake.X[0]>230||snake.Y[0]<40||snake.Y[0]>260)
- snake.Life=1;
-
- for(i=3;i<snake.Long;i++)
- {
- if(snake.X[i]==snake.X[0]&&snake.Y[i]==snake.Y[0]) //判断撞自己
- {
- game.Life=0;
- }
- }
- if(snake.Life==1||game.Life==0)
- {
- gameover(); //游戏结束
- game.Score =0;
-
- Delay(0xFFFFFF);
- Delay(0xFFFFFF); //延时大约两秒重新开始游戏
-
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- break;
- }
-
- //判断蛇是否吃到了食物
- if(snake.X[0]==food.X&&snake.Y[0]==food.Y) //判断蛇头与实物的坐标是否一样
- {
- LCD_SetTextColor (BLACK) ;
- ILI9341_DrawRectangle(food.X,food.Y,10,10,1);
- snake.Long++; //蛇长加1
- game.Score+=10; //分数加10
- food.Yes=1;
- }
- if(game.Score==1000) //分数达到1000
- {
- finish(); //完成挑战
- game.Score =0; //分数清零
-
- Delay(0xFFFFFF);
- Delay(0xFFFFFF);
-
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- break;
- }
- }
- }
-
- int main ( void )
- {
- ILI9341_GramScan ( 6 );
- ILI9341_Init ();
- Key_GPIO_Config();
- SysTick_Init();
- LCD_SetFont(&Font8x16);
- LCD_SetColors(YELLOW,BLACK);
- EXTI_Key_Config();
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- if(tiaozhuan==0)
- {
- boot_animation();
- }
- while ( 1 )
- {
- if(tiaozhuan==1)
-
- play();
- }
- }
- void LCD_Test(void)
- {
- char dispBuff[200];
-
- LCD_SetTextColor(GREEN);
- sprintf(dispBuff," 贪吃蛇游戏");
- ILI9341_DispStringLine_EN_CH(LINE(1),dispBuff);
- Delay(0xFFFFF);
-
- }
- void boot_animation(void) //开机动画
- {
- char dispBuff1[200];
-
- LCD_Test();
- LCD_SetColors(YELLOW,BLACK);
- ILI9341_DrawRectangle(0,40,240,240,0);
- Delay(0xFFFFF);
- LCD_SetColors(WHITE,BLACK);
- ILI9341_DrawRectangle(10,50,220,220,0);
- Delay(0xFFFFF);
- LCD_SetColors(BLUE,BLACK);
- ILI9341_DrawRectangle(20,60,200,200,0);
- Delay(0xFFFFF);
- LCD_SetColors(RED,BLACK);
- ILI9341_DrawRectangle(30,70,180,180,0);
- Delay(0xFFFFF);
- LCD_SetColors(MAGENTA,BLACK);
- ILI9341_DrawRectangle(40,80,160,160,0);
- Delay(0xFFFFF);
- LCD_SetColors(GREEN,BLACK);
- ILI9341_DrawRectangle(50,90,140,140,0);
- Delay(0xFFFFF);
- LCD_SetColors(CYAN,BLACK);
- ILI9341_DrawRectangle(60,100,120,120,0);
- Delay(0xFFFFF);
- LCD_SetColors(RED,BLACK);
- ILI9341_DrawRectangle(70,110,100,100,0);
- Delay(0xFFFFF);
- LCD_SetColors(GREY,BLACK);
- ILI9341_DrawRectangle(80,120,80,80,0);
- Delay(0xFFFFF);
- LCD_SetColors(GBLUE,BLACK);
- ILI9341_DrawRectangle(90,130,60,60,0);
- Delay(0xFFFFF);
-
-
- LCD_SetTextColor(GREEN);
- sprintf(dispBuff1," 按K1键开始游戏......");
- ILI9341_DispStringLine_EN_CH(LINE(18),dispBuff1);
- Delay(0xFFFFF);
- }
- static void Delay ( __IO uint32_t nCount ) //延时函数
- {
- for ( ; nCount != 0; nCount -- );
- }
复制代码
所有资料51hei提供下载:
贪吃蛇.7z
(217.19 KB, 下载次数: 122)
|