高中生
最后登录1970-1-1
在线时间 小时
注册时间2017-7-22
|
自己扩展了两个按键
可以实现基础的贪吃蛇游戏功能,
后续想加入游戏等级功能的话只要修改刷新的延时数值就好
希望能有些用吧,毕竟也折腾了几天才弄成
- #include "stm32f10x.h"
-
- #include <stdio.h>
- #include "./led/bsp_led.h"
- #include "./key/bsp_exti.h"
- #include "./i2c/bsp_i2c_gpio.h"
- #include "./lcd/bsp_ili9341_lcd.h"
- #include "./usart/bsp_usart.h"
- #include "./SysTick/bsp_SysTick.h"
-
- #include "./key/bsp_key.h"
- static void LCD_Test(void);
- uint32_t time = 0;
- char str[20];
- uint16_t key_flag=0;
- extern mk;
- char dispBuff[100];
- #define SNAKE_Max_Long 60//éßμÄ×î′ó3¤¶è
- static void Delay ( __IO uint32_t nCount );
- //éß½á11ìå
- struct Snake
- {
- uint16_t X[SNAKE_Max_Long];
- uint16_t Y[SNAKE_Max_Long];
- u8 Long;//éßμÄ3¤¶è
- u8 Life;//éßμÄéúÃü 0»î×Å 1Ëàíö
- u8 Direction;//éßòƶˉμÄ·½Ïò
- }snake;
- //ê3Îï½á11ìå
- struct Food
- {
- u8 X;//ê3Îïoá×ø±ê
- u8 Y;//ê3Îï×Y×ø±ê
- u8 Yes;//ÅD¶ÏêÇ·ñòa3öÏÖê3Îïμıäá¿ 0óDê3Îï 1Dèòa3öÏÖê3Îï
- }food;
- //óÎÏ·μ輶·Öêy
- struct Game
- {
- u16 Score;//·Öêy
- u8 Life;//óÎÏ·μ輶
- }game;
- void KEY1_IRQHandler(void)
- {
- //è·±£êÇ·ñ2úéúáËEXTI LineÖD¶Ï
- if(EXTI_GetITStatus(KEY1_INT_EXTI_LINE) != RESET)
- { key_flag=3;
- printf("key 1\n");
- if(key_flag=3&&snake.Direction!=4)
- snake.Direction=3;
- printf("Direction=%d\n",snake.Direction);
- EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE);
- }
- }
- void KEY2_IRQHandler(void)
- {
- //è·±£êÇ·ñ2úéúáËEXTI LineÖD¶Ï
- if(EXTI_GetITStatus(KEY2_INT_EXTI_LINE) != RESET)
- {
- key_flag=4;
- printf("key 2\n");
- if(key_flag=4&&snake.Direction!=3)
- snake.Direction=4;
- printf("Direction=%d\n",snake.Direction);
- EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE);
- }
-
- if(EXTI_GetITStatus(KEY3_INT_EXTI_LINE) != RESET)
- {
- key_flag=1;
- printf("key 3\n");
- if(key_flag=2&&snake.Direction!=2)
- snake.Direction=1;
- printf("Direction=%d\n",snake.Direction);
- EXTI_ClearITPendingBit(KEY3_INT_EXTI_LINE);
- }
-
-
- if(EXTI_GetITStatus(KEY4_INT_EXTI_LINE) != RESET)
- {
- key_flag=2;
- printf("key 4\n");
- if(key_flag=2&&snake.Direction!=1)
- snake.Direction=2;
- printf("Direction=%d\n",snake.Direction);
- EXTI_ClearITPendingBit(KEY4_INT_EXTI_LINE);
- }
- }
- void USART1_IRQHandler(u8 GetData)//′®¿úÖD¶Ï
- {
- if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
- {
- USART_ClearITPendingBit(USART1,USART_IT_RXNE);
- GetData=USART1->DR;
- mk=GetData;
- printf("êÕμ½μÄêy¾Y£o\n");
- printf("%d\n",GetData);
-
- printf("0x%x\n",mk);
- if(mk==0x1)
- {
- snake.Direction=1;
-
- }
- if(GetData==0x2)
- {
-
- snake.Direction=2;
-
-
- }
-
-
- }
-
- }
- //óÎÏ·½áêø
- void gameover()
- {
- ILI9341_DispStringLine_EN(LINE(1)," game over!");
-
- }
-
- //íæóÎÏ·
- void play()
- {
- u16 i;
- snake.Long=2;//¶¨òåéßμÄ3¤¶è
- snake.Life=0;//éß»1»î×Å
- snake.Direction=1;//éßμÄÆeê¼·½Ïò¶¨òåÎaóò
- game.Score=0;//·ÖêyÎa0
- game.Life=4;//éßμÄéúÃüÖμ
- food.Yes=1;//3öÏÖDÂê3Îï
- snake.X[0]=0;snake.Y[0]=50;
- snake.X[1]=10;snake.Y[1]=50;
- while(1)
- {
-
- ILI9341_DrawRectangle(0,0,240,270,0);
-
- sprintf(dispBuff,"SCORE : %d ",game.Score);
-
- ILI9341_DispStringLine_EN(LINE(18),dispBuff);
- if(food.Yes==1)//3öÏÖDÂμÄê3Îï
- {
- //Ôúé趨μÄÇøóòÄúÏÔê¾ê3Îï
- food.X=rand()%(230/10)*10;
- food.Y=rand()%(260/10)*10;
- food.Yes=0;
- }
- if(food.Yes==0)//óDê3Îï¾íòaÏÔê¾
- {
-
- LCD_SetColors(RED,BLACK);
- ILI9341_DrawRectangle (food.X,food.Y,10,10,1);
-
- }
- //è¡μÃDèòaÖØD»-μÄéßμĽúêy
- 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++)//»-3öéß
-
- 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);//Ïû3yéßéí
-
- LCD_SetTextColor (RED ) ;
- //ÅD¶ÏêÇ·ñ×2ǽ
- if(snake.X[0]==0||snake.X[0]>240||snake.Y[0]==0||snake.Y[0]>270)
- snake.Life=1;//éßËàμôáË
-
- //μ±éßμÄéíìå3¬1y3½úoóÅD¶Ïéß×ÔéíμÄÅö×2
- for(i=3;i<snake.Long;i++)
- {
- if(snake.X[i]==snake.X[0]&&snake.Y[i]==snake.Y[0])//×ÔéíμÄèÎò»×ø±êÖμóëéßí·×ø±êÏàμè¾íèÏÎaêÇ×ÔéíÅö×2
- {
- game.Life-=1;
- }
- }
- if(snake.Life==1||game.Life==0)//òÔéÏá½ÖÖÅD¶ÏòÔoóèç1ûéèËàμôáËìø3öÄúÑ-»·£¬ÖØD¿aê¼
- {
- gameover();
-
- Delay(0xFFFFFF);
- Delay(0xFFFFFF);//Ñóê±á½ÃëoóÖØD¿aê¼óÎÏ·
-
-
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- break;
- }
- //ÅD¶ÏéßêÇ·ñ3Ôμ½áËê3Îï
- if(snake.X[0]==food.X&&snake.Y[0]==food.Y)
- {
-
- LCD_SetTextColor (BLACK ) ;
- // ILI9341_DrawRectangle(food.X,food.Y,food.X+1,food.Y+1,1);//°Ñ3Ôμ½μÄê3ÎïÏû3y
-
- ILI9341_DrawRectangle(food.X,food.Y,10,10,1);//°Ñ3Ôμ½μÄê3ÎïÏû3y
- LCD_SetTextColor (RED ) ;
- snake.Long++;//éßμÄéíìå3¤ò»½ú
- game.Score+=10;
- //LCD_ShowNum(40,165,game.Score,3,16);//ÏÔê¾3鼨
- food.Yes=1;//DèòaÖØDÂÏÔê¾ê3Îï
- }
- //LCD_ShowNum(224,165,game.Life,1,16);//ÏÔê¾éúÃüÖμ
- }
- }
- int main ( void )
- {
-
-
- ILI9341_GramScan ( 6 );
-
-
- ILI9341_Init ();
- LED_GPIO_Config();
- USART_Config();
- Key_GPIO_Config();
- SysTick_Init();
- LCD_SetFont(&Font8x16);
-
- LCD_SetColors(RED,BLACK);
- EXTI_Key_Config();
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);
- while ( 1 )
- {
-
-
- ILI9341_DrawRectangle(0,0,240,270,0);
-
-
- printf("test\n");
- play();
- }
- }
- static void Delay ( __IO uint32_t nCount )
- {
- for ( ; nCount != 0; nCount -- );
-
- }
复制代码
|
|