|
基于STC15408AS的貪吃蛇游戲程序與硬件設計
制作出來的實物圖如下:
51hei圖片20211213165028.jpg (178.08 KB, 下載次數: 53)
下載附件
實物圖
2021-12-13 16:50 上傳
Altium Designer畫的原理圖和PCB圖如下:(51hei附件中可下載工程文件)
51hei.png (109.9 KB, 下載次數: 57)
下載附件
2021-12-14 01:35 上傳
51hei圖片20211213165314.png (87.16 KB, 下載次數: 57)
下載附件
貪吃蛇PCB設計
2021-12-13 16:54 上傳
51hei圖片20211213165341.png (120.68 KB, 下載次數: 58)
下載附件
貪吃蛇原理圖設計
2021-12-13 16:53 上傳
51hei圖片20211213165518.png (92.53 KB, 下載次數: 57)
下載附件
2021-12-13 16:55 上傳
單片機源程序如下:
- #include "snake.h"
- #include "oled.h"
- #include "adc.h"
- #include "stdlib.h"
- #include "timer.h"
- int snake_Grid[32][2];//蛇身的存儲數組,snake_Grid[][0]代表x坐標,snake_Grid[][1]代表y坐標
- extern u8 if_end;
- u8 snake_length = 5;//蛇身初始長度
- unsigned char squre[8]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- void snake_delay_ms(int ms) //延時函數
- {
- unsigned char i, j;
- while(ms--)
- {
- _nop_();
- _nop_();
- _nop_();
- i = 11;
- j = 190;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- void Draw_squre(unsigned char x,unsigned char y)//畫方塊,即地圖顯示部分
- {
- u8 i;
- OLED_Set_Pos(x*8,y);
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(squre[i],OLED_DATA);
- }
- }
- void Draw_Map()//畫地圖
- {
- u8 i;
- for(i=0;i<15;i++)
- {
- Draw_squre(i,0);
- Draw_squre(i,7);
- }
- for(i=0;i<8;i++)
- {
- Draw_squre(0,i);
- Draw_squre(15,i);
- }
- }
- unsigned char snake_head[8] = {0xFF,0x9F,0x9D,0xFD,0xFD,0x9D,0x9F,0xFF};
- void Draw_Head(unsigned char x,unsigned char y)//畫蛇頭函數
- {
- u8 i;
- OLED_Set_Pos(x*8,y);
- for(i=7;i>0;i--)
- {
- OLED_WR_Byte(snake_head[i],OLED_DATA);
- }
- }
- u8 Direction = 0;
- Coordinate old1 = {0};
- Coordinate old2 = {0};
- Coordinate snake_food = {1,1};
- Coordinate snake_body = {0,0};
- void Check_KEY()//搖桿檢測函數
- {
- int x_value,y_value;
- x_value = Get_ADC10bitResult(0);
- y_value = Get_ADC10bitResult(1);
- if(x_value<30&&Direction!=Left_Direction)Direction = Right_Direction;
- if(x_value>1000&&Direction!=Right_Direction)Direction = Left_Direction;
- if(y_value>1000&&Direction!=Up_Direction)Direction = Down_Direction;
- if(y_value<30&&Direction!=Down_Direction)Direction = Up_Direction;
- }
- void Record_Coordinate()//記錄上一次的坐標
- {
- old1.x = snake_Grid[0][0];
- old1.y = snake_Grid[0][1];
- old2.x = snake_Grid[snake_length-1][0];
- old2.y = snake_Grid[snake_length-1][1];
- }
- int score = 0;
- void Check_State()//檢測蛇的狀態
- {
- u8 i;
- snake_body.x = snake_Grid[0][0];
- snake_body.y = snake_Grid[0][1];
- if(snake_Grid[0][0]==snake_food.x&&snake_Grid[0][1]==snake_food.y)//當食物的坐標和蛇頭一致則記錄一次
- {
- score++;
- snake_food.x = rand()%14+1;
- snake_food.y = rand()%6+1;
- snake_length++;
- Rrfresh_Body(snake_food.x,snake_food.y);
- Draw_Food(snake_food.x,snake_food.y);
-
- }
- for(i=1;i<snake_length-1;i++)
- if(snake_body.x==snake_Grid[i][0]&&snake_body.y==snake_Grid[i][1])
- if_end = 1;
- for(i=0;i<snake_length-1;i++)
- while(snake_Grid[0][0]==snake_food.x&&snake_Grid[0][1]==snake_food.y)
- {
- snake_food.x = rand()%13+1;//隨機生成食物
- snake_food.y = rand()%6+1;
- }
- }
- unsigned char food[8] = {0xFF,0xFF,0xC3,0xDB,0xDB,0xC3,0xFF,0xFF,};
- void Draw_Food(unsigned char x,unsigned char y)//畫食物
- {
- u8 i;
- OLED_Set_Pos(x*8,y);
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(food[i],OLED_DATA);
- }
- }
- void Snake_Move()//移動函數
- {
- u8 i;
- Check_State();
- for(i=snake_length-1;i>0;i--)
- {
- snake_Grid[i][0] = snake_Grid[i-1][0];
- snake_Grid[i][1] = snake_Grid[i-1][1];
- }
- switch(Direction)
- {
- case Right_Direction:
- Record_Coordinate();
- snake_Grid[0][0]++;
- Rrfresh_Body(old1.x,old1.y);
- Rrfresh_Body(old2.x,old2.y);
- if(snake_Grid[0][0]>15)
- {
- if_end = 1;
- OLED_Clear();
- }
- break;
- case Left_Direction:
- Record_Coordinate();
- snake_Grid[0][0]--;
- Rrfresh_Body(old1.x,old1.y);
- Rrfresh_Body(old2.x,old2.y);
- if(snake_Grid[0][0]<=0)
- {
- if_end = 1;
- OLED_Clear();
- }
- break;
- case Up_Direction:
- Record_Coordinate();
- snake_Grid[0][1]--;
- Rrfresh_Body(old1.x,old1.y);
- Rrfresh_Body(old2.x,old2.y);
- if(snake_Grid[0][1]<=0)
- {
- if_end = 1;
- OLED_Clear();
- }
- break;
- case Down_Direction:
- Record_Coordinate();
- snake_Grid[0][1]++;
- Rrfresh_Body(old1.x,old1.y);
- Rrfresh_Body(old2.x,old2.y);
- if(snake_Grid[0][1]>7)
- {
- if_end = 1;
- OLED_Clear();
- }
- break;
- }
- }
- void Draw_Start_Interface()//開始界面的繪制
- {
- u8 i;
- for(i=0;i<6;i++)
- {
- OLED_ShowCHinese(16+i*16,3,i+4);
- OLED_ShowCHinese(16+i*16,5,i+10);
- }
- snake_delay_ms(500);
- OLED_Clear();
- }
- int snake_sum = 3;
- void Snake_Init()//初始化蛇
- {
- u8 i;
- snake_Grid[0][0] = 5;//身體為5
- snake_Grid[0][1] = 5;
- Draw_Start_Interface();
- while(snake_sum)//加載界面
- {
- snake_sum--;
- for(i=0;i<3;i++)
- {
- OLED_ShowCHinese(16+i*16,3,i+16);
- }
- for(i=0;i<3;i++)
- {
- OLED_ShowString(64+i*16,3,".");
- snake_delay_ms(500);
- }
- OLED_Clear();
- }
- OLED_Clear();
- Draw_Food(snake_food.x,snake_food.y);
- Draw_Map();
- for(i = snake_length-1;i>0;i--)
- {
- snake_Grid[i][0] = snake_Grid[0][0]-i;
- snake_Grid[i][1] = snake_Grid[0][1];
- }
- LED1 = 0;
- LED2 = 0;
- LED3 = 0;
- }
- const unsigned char code body[8]={0xFF,0xFF,0xC3,0xC3,0xC3,0xC3,0xFF,0xFF};
- void Draw_Body(unsigned char x,unsigned char y)//畫身體
- {
- u8 i;
- OLED_Set_Pos(x*8,y);
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(body[i],OLED_DATA);
- }
- }
- void Rrfresh_Body(unsigned char x,unsigned char y)//刷新身體
- {
- u8 i;
- OLED_Set_Pos(x*8,y);
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(0,OLED_DATA);
- }
- }
- void Game_if_continue()//游戲是否繼續
- {
- Direction = 5;
- Check_KEY();
- switch(Direction)
- {
- case Left_Direction:
- Continue_Game();
- Direction = Right_Direction;
- snake_Grid[0][0] = 5;
- snake_Grid[0][1] = 5;
- Draw_Map();
- snake_food.x = 1;
- snake_food.y = 1;
- score = 0;
- Draw_Food(snake_food.x,snake_food.y);
- break;
- case Right_Direction:
- End_Game();
- break;
- }
- }
- void Continue_Game()//繼續游戲
- {
- if_end = 0;
- snake_length = 5;
- OLED_Clear();
- }
- void End_Game()//結束游戲
- {
- static u8 n = 1;
- if(n)
- {
- n = 0;
- OLED_Clear();
- }
- OLED_ShowString(0,3,"Thank you for your help",16);
- while(1);
- }
復制代碼
所有資料51hei附件下載:
貪吃蛇代碼及pcB.7z
(3.16 MB, 下載次數: 39)
2021-12-14 01:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|