找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机单人弹球游戏程序Proteus仿真图分享

[复制链接]
跳转到指定楼层
楼主
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:

#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, 下载次数: 0)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表