仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
屏幕截圖 2024-09-14 140743.png (56.08 KB, 下載次數: 3)
下載附件
2024-9-14 14:08 上傳
單片機源程序如下:
#include <REGX52.H>
#include "Delay.h"
#include "MatrixLED.h"
#include "Timer0Init.h"
#include "StartGame.h"
#include "GAMEOVER.h"
#include "VICTORY.h"
sbit k2=P3^0; //K2按鍵
sbit k1=P3^1; //K1按鍵
int Place=3,numt=0,ball_LeftRight=1,ball_UpDown=1,Defeat=0,Victory=0,Score=0,start_flag=0;
unsigned char ball_position=0x80;
int racket[]={224,112,56,28,14,7};//用于表示球拍位置
//分別為1110 0000,0111 0000,0011 1000,0001 1100,0000 1110,0000 0111
void Gameinit()//游戲初始化
{
Place=3;
numt=0;
ball_LeftRight=1;//球的左右移動方向
ball_UpDown=1;//球的上下移動方向
Defeat=0;//失敗標志
Victory=0;//勝利標志
Score=0;//分數
start_flag=0;
ball_position=0x80;//球在第8行
}
void Showpicture()//用于顯示點陣屏
{
unsigned char k=1;
MatrixLED_ShowColumn(0,racket[Place]);
for(k=1;k<8;k++)
{
if(k!=7-numt)
MatrixLED_ShowColumn(k,0x00);
else
MatrixLED_ShowColumn(k,ball_position);
}
}
void Check()//判斷游戲勝利或失敗
{
if(numt==6)//表示當球在第二列時
{
if(ball_position==0x80)//如果球在最上方
{
if(Place!=0)//球拍位置不是1110 0000則游戲失敗
Defeat=1;
}
if(ball_position==0x20)
{
if((Place!=0)&&(Place!=1)&&(Place!=2))
Defeat=1;
}
if(ball_position==0x08)
{
if((Place!=2)&&(Place!=3)&&(Place!=4))
Defeat=1;
}
if(ball_position==0x02)
{
if((Place!=4)&&(Place!=5))
Defeat=1;
}
}
if(Score==6)//如果分數達到6分
Victory=1; //游戲勝利
}
void BallMove()//球的移動
{
Check();
numt+=ball_LeftRight;
if(ball_position==0x80)
{
ball_UpDown=1;
}
if(ball_position==0x01)
{
ball_UpDown=-1;
}
if(ball_UpDown==1)
{
ball_position=ball_position>>1;
}
if(ball_UpDown==-1)
{
ball_position=ball_position<<1;
}
if(numt==0)
{
ball_LeftRight=1;
Score+=1;
}
if(numt==6)
{
ball_LeftRight=-1;
}
}
void MoveUP()//球拍上移
{
Place-=1;
if(Place<0)
Place=0;
}
void MoveDOWN()//球拍下移
{
Place+=1;
if(Place>5)
Place=5;
}
void main()
{
MatrixLED_Init();
while(1)
{
while(start_flag==0) //如果K1按鍵按下
{
StartGame(start_flag);
start_flag=1;
}
Timer0Init();
while((Defeat==0)&&(Victory==0))
{
Showpicture();
if(k1==0) //如果K1按鍵按下
{
Delay(20);
while(k1==0);
Delay(20);
MoveUP();//拍子上移
}
if(k2==0) //如果K2按鍵按下
{
Delay(20);
while(k2==0);
Delay(20);
MoveDOWN();//拍子下移
}
}
if (Defeat==1)//如果游戲失敗
{
TR0=0;
GAMEOVER(Score);
Gameinit();
}
if (Victory==1)//如果游戲勝利
{
TR0=0;
VICTORY(Score);
Gameinit();
}
}
}
void Timer0() interrupt 1//定時器中斷程序
{
static unsigned int T0Count;
TL0 = 0x18; //設置定時初值
TH0 = 0xFC; //設置定時初值
T0Count++;
if((T0Count>=300)&&(k1==1)&&(k2==1)) //定時器分頻,300ms
{
T0Count=0;
BallMove();//每300ms球移動一次
}
}
仿真程序:
Simulation.7z
(48.94 KB, 下載次數: 7)
2024-9-14 14:34 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|