本项目使用STM32F103C8T6单片机控制器,IIC OLED液晶、按键等。
主要功能:
系统运行后,OLED显示游戏画面,可通过K1或K3键选择关卡,K2键开始。
/*
作者:嗨小易(技术交流QQ群:570487280)
*/
int main(void)
{
OLED_Init();
OLED_Clear();
Key_Init();
Timer_Init();
OLED_ShowCN_STR(0,64,0,3);
OLED_ShowCN_STR(2,64,3,4);
OLED_ShowChar(3,10,60);
OLED_ShowChar(3,16,62);
OLED_ShowNum(3,13,Plate_Num,1);
Show_Plate_Num(0,Plate_Num);
while(1)
{
Key_Num=Key_GetNum();
if(Key_Num)
{
if(Game_Interface==0)
{
if(Key_Num==1)
{
if(Plate_Num==3)
{
Plate_Num=3;
}
else
{
Plate_Num--;
}
OLED_ShowNum(3,13,Plate_Num,1);
Show_Plate_Num(0,Plate_Num);
}
else if(Key_Num==3)
{
if(Plate_Num==6)
{
Plate_Num=6;
}
else
{
Plate_Num++;
}
OLED_ShowNum(3,13,Plate_Num,1);
Show_Plate_Num(0,Plate_Num);
}
else if(Key_Num==2)
{
Game_Interface=1;
OLED_Clear();
Show_Plate_Num(1,Plate_Num);
Hanoi_Init();
TIM_Cmd(TIM2,ENABLE);
}
}
else if(Game_Interface==1)
{
if(Key_Num==1)
{
OLED_ShowChar(1,1,62);
OLED_ShowChar(1,5,32);
OLED_ShowChar(1,9,32);
}
else if(Key_Num==2)
{
OLED_ShowChar(1,5,62);
OLED_ShowChar(1,1,32);
OLED_ShowChar(1,9,32);
}
else if(Key_Num==3)
{
OLED_ShowChar(1,9,62);
OLED_ShowChar(1,1,32);
OLED_ShowChar(1,5,32);
}
Count++;
if(Count==1)
{
Pole_One=--Key_Num;
}
else if(Count==2)
{
Pole_Two=--Key_Num;
}
}
}
if(Game_Interface==1)
{
if(Count==2)
{
if(Motion_Plate(Pole_One,Pole_Two)){}
else
{
Renovate_Pole();
Step++;
}
OLED_ShowNum(4,14,Step,3);
if(Judge_Victory(Plate_Num)!=0)
{
OLED_Clear();
TIM_Cmd(TIM2,DISABLE);
Game_Interface=2;
}
Pole_One=3;Pole_Two=3;Count=0;
}
}
else if(Game_Interface==2)
{
OLED_ShowString(1,1,"Victory");
OLED_ShowString(1,13,"Time");
// OLED_ShowString(2,13,"0000");
OLED_ShowString(3,13,"Step");
OLED_ShowNum(4,14,Step,3);
// Key_Num=Key_GetNum();
if(Key_Num)
{
Game_Interface=0;
min=0;
sec=0;
Step=0;
OLED_Clear();
OLED_ShowCN_STR(0,64,0,3);
OLED_ShowCN_STR(2,64,3,4);
OLED_ShowChar(3,10,60);
OLED_ShowChar(3,16,62);
OLED_ShowNum(3,13,Plate_Num,1);
Show_Plate_Num(0,Plate_Num);
}
}
if(Game_Interface!=0)
{
OLED_ShowNum(2,13,min,2);
OLED_ShowNum(2,15,sec,2);
}
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
sec++;
if(sec==60)
{
min++;
if(min>99){min=0;}
sec=0;
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
B站演示视频:https://space.bilibili.com/444388619
专注于51单片机、STM32、国产32、DSP、Proteus、ardunio、ESP32、物联网软件开发,PCB设计,视频分享,技术交流。