|
基于STC15408AS的贪吃蛇游戏程序与硬件设计
制作出来的实物图如下:
Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- #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)
|
评分
-
查看全部评分
|