微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51单片机的双人对战小游戏的实现

基于51单片机的双人对战小游戏的实现

时间:11-30 来源:互联网 点击:
#include
unsigned char position1=1;//position一共有3种状态,0代表最边上,1代表初始位置,2代表最前的位置unsigned char position2=1;//position一共有3种状态,0代表最边上,1代表初始位置,2代表最前的位置unsigned char fist1=0;//1没有出拳unsigned char fist2=0;//2没有出拳unsigned char leg1=0;//1没有出脚unsigned char leg2=0;//2没有出脚unsigned char count_back1=0;unsigned char count_back2=0;unsigned char count_forth1=0;unsigned char count_forth2=0;unsigned char time0_count=0;unsigned char time1_count=0;unsigned char blood1=7;unsigned char blood2=7;unsigned char buff1[16]={0x00,0x18,0x24,0x24,0x18,0x18,0x7E,0x66,//右边小人的初始形态0x66,0x24,0x3C,0x24,0x24,0x24,0x24,0x00};unsigned char buff2[16]={0x00,0x18,0x24,0x24,0x18,0x18,0x7E,0x66,//右边小人的初始形态0x66,0x24,0x3C,0x24,0x24,0x24,0x24,0x00};//定义两名选手的操作键//选手1#define S11 P1_0//向前#define S12 P1_1//后退#define S13 P1_2//出拳#define S14 P1_3//出腿//选手2#define S21 P1_4//向前#define S22 P1_5//向后#define S23 P1_6//出出#define S24 P1_7//腿拳//两个选手的led点阵行扫描使能端#define A_1 P0_0#define B_1 P0_1#define C_1 P0_2#define D_1 P0_3//两个选手的2个595使能端#define SCLK P2_0#define SI P2_1#define RCLK P2_2//当按下S1时 开始游戏,游戏结束时显示哪方获胜,然后按下S2键后重新开始新一轮游戏#define S1 P2_6#define S2 P2_7//第一个人的74HC154区域行选#define NUMLED1OPEN(); {D_1=0;C_1=0;B_1=0;A_1=0;};#define NUMLED2OPEN(); {D_1=0;C_1=0;B_1=0;A_1=1;};#define NUMLED3OPEN(); {D_1=0;C_1=0;B_1=1;A_1=0;};#define NUMLED4OPEN(); {D_1=0;C_1=0;B_1=1;A_1=1;};#define NUMLED5OPEN(); {D_1=0;C_1=1;B_1=0;A_1=0;};#define NUMLED6OPEN(); {D_1=0;C_1=1;B_1=0;A_1=1;};#define NUMLED7OPEN(); {D_1=0;C_1=1;B_1=1;A_1=0;};#define NUMLED8OPEN(); {D_1=0;C_1=1;B_1=1;A_1=1;};#define NUMLED9OPEN(); {D_1=1;C_1=0;B_1=0;A_1=0;};#define NUMLED10OPEN(); {D_1=1;C_1=0;B_1=0;A_1=1;};#define NUMLED11OPEN(); {D_1=1;C_1=0;B_1=1;A_1=0;};#define NUMLED12OPEN(); {D_1=1;C_1=0;B_1=1;A_1=1;};#define NUMLED13OPEN(); {D_1=1;C_1=1;B_1=0;A_1=0;};#define NUMLED14OPEN(); {D_1=1;C_1=1;B_1=0;A_1=1;};#define NUMLED15OPEN(); {D_1=1;C_1=1;B_1=1;A_1=0;};#define NUMLED16OPEN(); {D_1=1;C_1=1;B_1=1;A_1=1;};//0是第一个选手的索引表的开始 80是第二个选手开始在表中的位置#define START1 0#define START2 80#define INIT 16//第一个选手从的初始状态在中间 为16#define JUMP_UP_AND_BACK1 48//向后跳起的缓冲动作的位置#define JUMP_UP_AND_BACK2 64//向后跳起的缓冲动作的位置#define BACK 0//在最边后面的状态的位置#define FORTH 32//在最前面的状态的位置#define GO 0//游戏的开始画面 然后按S1开始游戏//#define KO 32#define P1_WIN 64//显示P1胜利在索引表中的位置#define P2_WIN 96//显示P2胜利在索引表中的位置//图像表//人物的各种形态和位置unsigned char code role_action[]={0x00,0x0C,0x12,0x12,0x0C,0x0C,0x3F,0x33,//左边小人后退一步的状态00x33,0x12,0x1E,0x12,0x12,0x12,0x12,0x00,0x00,0x18,0x24,0x24,0x18,0x18,0x7E,0x66,//左边小人的初始形态160x66,0x24,0x3C,0x24,0x24,0x24,0x24,0x00,0x00,0x30,0x48,0x48,0x30,0x30,0xFC,0xCC,//左边小人前进一步的状态320xCC,0x48,0x78,0x48,0x48,0x48,0x48,0x00,0x18,0x24,0x24,0x18,0x18,0x66,0x66,0x65,//左边小人向右后方向跳起的动作1 480x24,0x3C,0x24,0x12,0x12,0x12,0x00,0x00,0x0C,0x12,0x12,0x0C,0x0C,0x33,0x33,0x53,//左边小人向右后方向跳起的动作2 640x12,0x1E,0x12,0x24,0x24,0x24,0x00,0x00,0x00,0x30,0x48,0x48,0x30,0x30,0xCC,0xCC,//右边小人后退一步的动作800xCC,0x48,0x78,0x48,0x48,0x48,0x48,0x00,0x00,0x18,0x24,0x24,0x18,0x18,0x7E,0x66,//左边小人的初始形态960x66,0x24,0x3C,0x24,0x24,0x24,0x24,0x00,0x00,0x0C,0x12,0x12,0x0C,0x0C,0x33,0x33,//右边小人前进一步的状态1120x33,0x12,0x1E,0x12,0x12,0x12,0x12,0x00,0x18,0x24,0x24,0x18,0x18,0x66,0x66,0xA6,//右边小人向左后方向跳起的动作1 1280x24,0x3C,0x24,0x48,0x48,0x48,0x00,0x00,0x30,0x48,0x48,0x30,0x30,0xCC,0xCC,0xCA,//右边小人向左后方向跳起的动作2 1440x48,0x78,0x48,0x24,0x24,0x24,0x00,0x00};//开机画面和结束时显示哪方胜利的索引表unsigned char code welecome_and_end[]={0x00,0x00,0x00,0x04,0xF9,0xF4,0xF9,0xF4,0x00,0x04,0xF9,0xF4,0xF9,0xF4,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//GO!!!  00x00,0x00,0x22,0x38,0x24,0x44,0x28,0x44,0x30,0x44,0x28,0x44,0x24,0x44,0x22,0x44,0x22,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x92,0x49,0x45,0x14,0x28,0xA2,0x92,0x49,//KO 320x00,0x00,0x00,0x00,0x00,0x00,0x25,0x12,0x51,0x13,0x55,0x12,0x55,0x52,0x54,0xA7,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//P1 WIN 640x00,0x00,0x00,0x00,0x00,0x00,0x4A,0x24,0xA2,0x2A,0xAA,0x24,0xAA,0xA2,0xA9,0x4E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00//P2 WIN  96};void delayu(unsigned int time);//延时函数 延时0.1msvoid delay(unsigned int time);//延时函数 延时1msvoid NUMLEDOPEN(int i);//74HC154进行行选择,第i行void dat_in(unsigned char dat);//将8为数据从597传送过去void display_one_page();//先加载各方“血”的状态,并显示一张图片,显示人的状态和血的状态void display_scene(int scene);//显示开机画面和胜负画面的函数void change_buffer_to1(int position);//修改1的图像状态缓冲 从人物索引表的第position个8位数个开始显示void change_buffer_to2(int position);//修改2的图像状态缓冲 从人物索引表的第position个8位数个开始显示void action_back1();//第1个人向后一步 并将position减1 直到它不能后退void action_back2();//第2个人向后一步 并将position减1 直到它不能后退void action_forth1();//第1个人向前一步 并将position加1 直到它不能后退void action_forth2();//第2个人向前一步 并将position加1 直到它不能后退void fist_change_buff1();//由于1出拳对画面的改动 要调用change_buff1_to_bit(int pos)和change_buff1_to_bit_low(int pos)函数void leg_change_buff1();//由于1出腿对画面的改动        要调用change_buff1_to_bit(int pos)和change_buff1_to_bit_low(int pos)函数void fist_change_buff2();//由于2出拳对画面的改动 要调用change_buff1_to_bit(int pos)和change_buff1_to_bit_low(int pos)函数void leg_change_buff2();//由于2出腿对画面的改动 要调用change_buff1_to_bit(int pos)和change_buff1_to_bit_low(int pos)函数void change_buff1_to_bit(int pos); //将特定某一位置一void change_buff2_to_bit(int pos);//将特定某一位置一void change_buff1_to_bit_low(int pos);//将特定某一位置0void change_buff2_to_bit_low(int pos);//将特定某一位置0void action_fist1();//第1个人出拳 并处理减血情况void action_fist2();//第2个人出拳 并处理减血情况void action_leg1();//第1个人出腿 并处理减血情况void action_leg2();//第2个人出腿 并处理减血情况void scan_key();//这个是函数的主体部分 分别扫描并处理向后退 向前走 出拳 出腿的动作 当出拳和出腿的时候 开启定时器//这300ms内记录是否有对方出拳出腿的动作 并按照先扫描向后退 后扫描向前进的顺序 扫描一遍 并处理 如果没有这两个动作 直接返回调用出拳//否则处理后再出拳 出脚的道理一致void init_interrupt();//中断位的设置//开启定两个时器中断 16位模式void shed_blood1();//记录流血的数字void shed_blood2();void main(){int i;while(1){if(S1==0){delay(1);if(S1==0){for(i=0;i0){if(position1==2){i=15;change_buffer_to1(START1+JUMP_UP_AND_BACK2);while(i--)display_one_page();i=5;change_buffer_to1(START1+INIT);while(i--)display_one_page();}else if(position1==1){i=15;change_buffer_to1(START1+JUMP_UP_AND_BACK2);while(i--)display_one_page();i=5;change_buffer_to1(START1+BACK);while(i--)display_one_page();}position1--;}}void action_back2()//第2个人向后一步{//先显示向后跳起的动作 再显示后退的状态int i;if(position2>0){if(position2==2){i=15;change_buffer_to2(START1+JUMP_UP_AND_BACK1);while(i--)display_one_page();i=5;change_buffer_to2(START2+INIT);while(i--)display_one_page();}else if(position2==1){i=15;change_buffer_to2(START2+JUMP_UP_AND_BACK2);while(i--)display_one_page();i=5;change_buffer_to2(START2+BACK);while(i--)display_one_page();}position2--;}}void action_forth1()//第1个人向前一步{int i;if(position1<2){if(position1==1){i=10;change_buffer_to1(START1+FORTH);while(i--)display_one_page();}else if(position1==0){i=10;change_buffer_to1(START1+INIT);while(i--)display_one_page();}position1++;}}void action_forth2()//第2个人向前一步{//直接显示向前一步的状态int i;if(position2<2){if(position2==1){i=10;change_buffer_to2(START2+FORTH);while(i--)display_one_page();}else if(position2==0){i=10;change_buffer_to2(START1+INIT);while(i--)display_one_page();}position2++;}}void change_buff1_to_bit(int pos){int p1=pos/8;int p2=pos%8;unsigned char flag=0x01;flag=flag0)blood1--;}}else if(position2==2){if(position1>0){if(blood1>0)blood1--;}}}void shed_blood2(){if(position1==1){if(position2==2){if(blood2>0)blood2--;}}else if(position1==2){if(position2>0){if(blood2>0)blood2--;}}}void handle1() interrupt 1{EA=0;TH0=63536/256;//2ms一次定时器中断TL0=63536%256;time0_count++;if(time0_count==200){time0_count=0;TR0=0;if(count_back2==1){count_back2=0;action_back2();}else if(count_forth2==1){count_forth2=0;action_forth2();}}EA=1;}void handle2() interrupt 3{EA=0;TH1=63536/256;//2ms一次定时器中断TL1=63536%256;time1_count++;if(time1_count==200){time1_count=0;TR1=0;if(count_back1==1){count_back1=0;action_back1();}else if(count_forth1==1){count_forth1=0;action_forth1();}}EA=1;       }

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top