|
按鍵控制,液晶屏顯示
捕獲.JPG (22.82 KB, 下載次數(shù): 65)
下載附件
外接按鍵原理圖,接單片機(jī)引腳PC10,PC11
2019-7-15 12:05 上傳
IMG_20190715_120204.jpg (2.72 MB, 下載次數(shù): 52)
下載附件
開(kāi)機(jī)動(dòng)畫(huà)
2019-7-15 12:03 上傳
IMG_20190715_120213.jpg (2.33 MB, 下載次數(shù): 47)
下載附件
游戲開(kāi)始界面
2019-7-15 12:03 上傳
單片機(jī)源程序如下:
- #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]; //最大長(zhǎng)度
- u8 Long;
- u8 Life;// 0活著 1死亡
- u8 Direction;//蛇移動(dòng)的方向
- }snake; //蛇的結(jié)構(gòu)體
- struct Food
- {
- u8 X;
- u8 Y;
- u8 Yes; //0有食物 1需要出現(xiàn)食物
- }food; //食物結(jié)構(gòu)體
- struct Game
- {
- u16 Score;
- u8 Life;
- }game; 游戲結(jié)構(gòu)體
- void KEY1_IRQHandler(void)
- {
- if(EXTI_GetITStatus(KEY1_INT_EXTI_LINE) != RESET) //判斷按鍵中斷
- {
- tiaozhuan=1; 跳轉(zhuǎn)到游戲開(kāi)始界面
- key_flag=2;
- if(key_flag==2&&snake.Direction!=1)
- snake.Direction=2; //看下面程序?qū)Ψ较虻拿枋?br />
- EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE); //清除標(biāo)志位
- }
- }
- 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() //游戲結(jié)束函數(shù)
- {
- LCD_SetTextColor(RED);
- ILI9341_DispStringLine_EN(LINE(10)," game over!");
- }
- void finish() //完成挑戰(zhàn)函數(shù)
- {
- LCD_SetTextColor(RED);
- sprintf(dispBuff," 恭喜你完成挑戰(zhàn)");
- ILI9341_DispStringLine_EN_CH(LINE(10),dispBuff);
- }
- void play(void) //開(kāi)始游戲函數(shù)
- {
- u16 i;
- snake.Long=2; //初始化蛇長(zhǎng)為2
- snake.Life=0;
- snake.Direction=1;//蛇的起始方向定義為右
- game.Life=1;//蛇的生命值
- food.Yes=1;//出現(xiàn)新食物
- snake.X[0]=0;snake.Y[0]=50; 初始化蛇坐標(biāo)
- 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,"分?jǐn)?shù) : %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; //隨機(jī)函數(shù)放置食物
- if((food.X<240)&&(food.Y<260&&food.Y>40)) //如果食物不在范圍內(nèi),則重新放置
- {
- 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) //對(duì)方向的描述,按鍵中斷控制
- {
- case 1:snake.X[0]+=10;break;//向右運(yùn)動(dòng)
- case 2:snake.X[0]-=10;break;//向左運(yùn)動(dòng)
- case 3:snake.Y[0]-=10;break;//向上運(yùn)動(dòng)
- case 4:snake.Y[0]+=10;break;//向下運(yùn)動(dòng)
- }
- 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(); //游戲結(jié)束
- game.Score =0;
-
- Delay(0xFFFFFF);
- Delay(0xFFFFFF); //延時(shí)大約兩秒重新開(kāi)始游戲
-
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- break;
- }
-
- //判斷蛇是否吃到了食物
- if(snake.X[0]==food.X&&snake.Y[0]==food.Y) //判斷蛇頭與實(shí)物的坐標(biāo)是否一樣
- {
- LCD_SetTextColor (BLACK) ;
- ILI9341_DrawRectangle(food.X,food.Y,10,10,1);
- snake.Long++; //蛇長(zhǎng)加1
- game.Score+=10; //分?jǐn)?shù)加10
- food.Yes=1;
- }
- if(game.Score==1000) //分?jǐn)?shù)達(dá)到1000
- {
- finish(); //完成挑戰(zhàn)
- game.Score =0; //分?jǐn)?shù)清零
-
- 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) //開(kāi)機(jī)動(dòng)畫(huà)
- {
- 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鍵開(kāi)始游戲......");
- ILI9341_DispStringLine_EN_CH(LINE(18),dispBuff1);
- Delay(0xFFFFF);
- }
- static void Delay ( __IO uint32_t nCount ) //延時(shí)函數(shù)
- {
- for ( ; nCount != 0; nCount -- );
- }
復(fù)制代碼
所有資料51hei提供下載:
貪吃蛇.7z
(217.19 KB, 下載次數(shù): 122)
2019-7-14 22:40 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
F103-指南者-原理圖.pdf
(172.93 KB, 下載次數(shù): 44)
2019-7-15 11:49 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|