- //该游戏采用函数指针制作确保的数据的安全性
- #include "sys.h"
- #include "key.h"
- sbit key0 = P1^0; //上移按键
- sbit key1 = P1^1; //下移按键
- sbit key2 = P1^2; //顺时针旋转按键
- sbit key3 = P1^3; //逆时针·旋转按键
- sbit led1 = P1^4; //测试的小灯
- /* 按键检测函数 */
- void KeyScan()
- {
- if(key0 == 0)
- {
- Control.delay_ms(5); //消抖延时
- if(key0 == 0)
- {
- switch(num3)
- {
- case 1:break;
- case 2:break;
- case 3:num3 = Control.Find_position(0xbf); break;
- case 4:break;
- case 5:break;
- case 6:num3 = Control.Find_position(0xfe); break;
- case 7:break;
- case 8:break;
- case 9:break;
- case 10:num3 = Control.Find_position(0x9c);break;
- case 12:num3 = Control.Find_position(0xdc);break;
- }
- while(!key0); //等待按键释放
- }
- }
- /*以上是第一个按键按下的函数即是上移的函数*/
- else if(key1 == 0 )
- {
- Control.delay_ms(5); //消抖延时
- if(key1 == 0)
- {
- switch(num3)
- {
- case 0:num3 = Control.Find_position(0xbf);break;
- case 1:break;
- case 2:break;
- case 4:break;
- case 5:break;
- case 6:num3 = Control.Find_position(0xf7);break;
- case 7:break;
- case 8:num3 = Control.Find_position(0xa3);break;
- case 9:num3 = Control.Find_position(0xe3);break;
- }
- while(!key1); //等待按键释放
- }
- }
- /*以上是第二个按键按下的函数即是下移的函数*/
- else if(key2 == 0)
- {
- Control.delay_ms(5);//消抖延时
- if(key2 == 0)
- {
- switch(num3)
- {
- case 0: num3=Control.Find_position(0xfd);break;
- case 1: num3=Control.Find_position(0xfb);break;
- case 2: num3=Control.Find_position(0xf7);break;
- case 3: num3=Control.Find_position(0xef);break;
- case 4: num3=Control.Find_position(0xdf);break;
- case 5: num3=Control.Find_position(0xbf);break;
- case 6: num3=Control.Find_position(0xfe);break;
- case 7: num3=Control.Find_position(0xcf);break;
- case 11:num3=Control.Find_position(0xf9);break;
- }
- while(!key2); //等待按键释放
- }
- }
- /*以上是第三个按键按下的函数即是顺时针的函数*/
- else if(key3 == 0 )
- {
- Control.delay_ms(5); //消抖延时
- if(key3 == 0 )
- {
- switch(num3)
- {
- case 0: num3=Control.Find_position(0xdf);break;
- case 5: num3=Control.Find_position(0xef);break;
- case 4: num3=Control.Find_position(0xf7);break;
- case 3: num3=Control.Find_position(0xfb);break;
- case 2: num3=Control.Find_position(0xfd);break;
- case 1: num3=Control.Find_position(0xbf);break;
- case 6: num3=Control.Find_position(0xfe);break;
- case 7: num3=Control.Find_position(0xcf);break;
- case 11:num3=Control.Find_position(0xf9);break;
- }
- while(!key3); //等待按键释放
- }
- }
- /*以上是第四个按键按下的函数即是逆时针的函数*/
- }
- #include "Game.h"
- #include "main.h"
- #include "sys.h"
- Enable flag = False;
- uint num0,num1,num2;
- void T0_timer() interrupt 1
- {
- TH0 =(65536-45872)/256;
- TL0 =(65536-45872)%256;
- num0++;
-
- if(num0==20)
- {
- num1++;
- num0=0;
- }
- if(num1==10)
- {
- num1=0;
- num2++;
- }
- if(num2==10)
- {
- num2=0;
- }
-
- Control.Nixie_Display(num1);
- Control.Nixie_Display(num2);
- Control.CS_Enable(True);
-
- }
- void Sys_init()
- {
-
- TMOD = 0X01;
- TH0 = (65536-45872)/256;
- TL0 = (65536-45872)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- num2=0;
- num0 = 0;
- num1 = 0;
- }
- void Game_Init()
- {
- //Sys_init();
- Control.Init_LCD();
- Control.LCD_PutString(1,1,"俄罗斯方块");
- Control.LCD_PutString(0,2,"当前得分");
- Control.LCD_PutString(0,3,"电气信息工程学院");
- Control.LCD_PutString(0,4,"2018年10月31");
- while(1)
- {
- //Left_shift();
-
- Control.Write_Cmd(0x90+5);
- Control.Write_Data('0'+score/10);
- Control.Write_Data('0'+score%10);
- //Control.Left_shift() ;
- Control.Regulation_Game(2);
-
-
- // Disable(0,8);
- }
- #include "sys.h"
- #include "main.h"
- #include "game.h"
- #include "st7290.h"
- #include "key.h"
- int score=0;
- uint num3=0;
- uchar code Nixie[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0x80,0X90,0xff}; //显示的数
- uchar code vide_Game[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0xf9,0x9c,0xdc,0xa3,0xcf,0xe3,0xff}; //方块
- void beep_enabel(Enable enable,uint index)
- {
- uint x=0;
- for(;x<index;x++)
- {
- Control.delay_ms(100);
- beep = enable == True? 1 : 0;
- }
- }
- uint Find_position(char parameter) //查找位置函数
- {
- uint index=0,i;
- uint length = sizeof(vide_Game);
- for(i=0;i<length;i++)
- {
- if(parameter==vide_Game[i])
- {
- index=i;
- break;
- }
- }
- return index;
- }
- void delay_ms(uint flag) //延时函数
- {
- uint i,j;
- for(i = 0; i < flag; i++)
- for(j = 110; j > 0; j--);
- }
- void CS_Enable(Enable enable) //CS使能或失能函数
- {
- if(enable == True)
- {
- P2 = 0XFF;
- }
- else
- if(enable == False)
- {
- P2 = 0XDF;
- }
- }
- void Nixie_Display(uint index) //数码管显示函数
- {
- char temp,i = 0;
- clk = 0;
- Control.CS_Enable(False);
- temp = Nixie[index];
- for(;i<8;i++)
- {
- din = temp&0x80;
- clk = 1;
- clk = 1;
- clk = 0;
- clk = 0;
- temp <<=1;
- }
- }
- void Game_Display(uint index) //游戏产生的种子
- {
- char temp,i = 0;
- clk = 0;
- Control.CS_Enable(False);
- temp = vide_Game[index];
- for(;i<8;i++)
- {
- din = temp&0x80;
- clk = 1;
- clk = 1;
- clk = 0;
- clk = 0;
- temp <<=1;
- }
- }
- void Result_Display(char index) //用来显示最后的记分的
- {
- char temp,i = 0;
- clk = 0;
- Control.CS_Enable(False);
- temp = index;
- for(;i<8;i++)
- {
- din = temp&0x80;
- clk = 1;
- clk = 1;
- clk = 0;
- clk = 0;
- temp <<=1;
- }
- }
- void Disable(uchar index1,uchar index2 ) //这个函数是用来设置数码管哪几个不亮的
- {
- for(;index1 < index2;index1++)
- {
- Nixie_Display(10);
- }
- }
- void Regulation_Game(uint index) //这个函数是用来执行游戏的规则的
- {
- int i;
- static uint seed=1,value=sizeof(vide_Game)-1 ,bug=5;
- static char temp=0xff;
- srand(seed);
- num3=1+rand()%bug; //随机种子
- for(i=7;i>=0;i--)
- {
- Control.KeyScan();
- Game_Display(num3); //这个是移动的数
- Disable(0,i);
- Control.Result_Display(temp); //这个是用来显示在第一位数码管上的值
- CS_Enable(True);
- delay_ms(1000);
- CS_Enable(False);
- delay_ms(1000);
- Disable(0,8);
- seed++; //更新随机种子
- }
- /****************************以下为该游戏的协议****************************************/
- temp = temp&vide_Game[num3]; value=num3; //进行与操作显示最后一位数
- if(temp==Nixie[8]) { score++; temp=0xff; Control.beep_enabel(True,3); Control.beep_enabel(False,1);} //出结果后进行消去数码管上的数
- if(bug==12) { bug=5; } //限制随机种子的范围
- else{ bug++; }
-
- }
- void Left_shift() //这个是左移函数
- {
- int i;
- for(i = 7;i >= 0;i--)
- {
- Control.Nixie_Display(i);
- Control.Disable(0,i);
- Control.CS_Enable(True);
- Control.delay_ms(1000);
- Control.CS_Enable(False);
- Control.delay_ms(1000);
- Control.Disable(0,8);
- }
- }
- void Right_shift() //这个是又移函数
- {
- int i;
- for(i=0;i<8;i++)
- {
- Control.Nixie_Display(i);
- Control.CS_Enable(True);
- Control.delay_ms(1000);
- Control.CS_Enable(False);
- Control.delay_ms(1000);
- }
- }
- CONTROL Control=
- {
- CS_Enable,
- Game_Init,
- delay_ms,
- Disable,
- Regulation_Game,
- Nixie_Display,
- Left_shift,
- Right_shift,
- Init_LCD,
- LCD_PutString,
- Write_Data,
- Write_Cmd,
- Game_Display,
- KeyScan,
- Find_position,
- Result_Display,
- beep_enabel,
- };
复制代码 |