經自己多次審核,發現能猜到的幾率很小。所以增加每猜對一次,機會就加一次。這將取消機會旗。
以前的、原理圖、效果圖都在這:
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (15.68 KB, 下載次數: 64)
下載附件
2018-8-6 17:19 上傳
0.png (52.51 KB, 下載次數: 70)
下載附件
2018-8-6 17:19 上傳
單片機源程序如下:
- #include <reg52.h>
- #include <stdlib.h> //該文件中包含隨機數生成函數包含函數
- #include "lcd.c" //顯示
- //二進制的形象表示方法,從左到右分別位第7 6 5 4 3 2 1 0位
- #define bin(_a,_b,_c,_d,_e,_f,_g,_h) ((_a)<<7|(_b)<<6|(_c)<<5|(_d)<<4|(_e)<<3|(_f)<<2|(_g)<<1|(_h)) //二進制的表示
- //定義按鍵
- sbit k1=P2^0;
- sbit k2=P2^1;
- sbit k3=P2^2;
- sbit k4=P2^3;
- sbit sp=P2^4;
- const uchar normal_man[]={ //正常臉CGRAM
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,1,1,1,1,1),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,0,0,0,0)
- };
- const uchar yes_man[]={ //笑臉CGRAM
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,1,0,0,0,1),
- bin(0,0,0,0,1,1,1,0),
- bin(0,0,0,0,0,1,1,0),
- bin(0,0,0,0,0,0,0,0)
- };
- const uchar no_man[]={ //哭臉CGRAM
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,1,1,1,0),
- bin(0,0,0,1,0,0,0,1),
- bin(0,0,0,0,0,0,0,0),
- bin(0,0,0,0,0,0,0,0)
- };
- const uchar pause[]={ //暫停符號CGRAM
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,1,1,0,1,1),
- bin(0,0,0,0,0,0,0,0)
- };
- const uchar key[]={ //鑰匙CGRAM
- bin(0,0,0,0,0,1,1,0),
- bin(0,0,0,0,0,1,0,0),
- bin(0,0,0,0,0,1,1,0),
- bin(0,0,0,0,0,1,0,0),
- bin(0,0,0,0,1,1,1,0),
- bin(0,0,0,0,1,0,1,0),
- bin(0,0,0,0,1,1,1,0),
- bin(0,0,0,0,0,0,0,0)
- };
- void paused()
- {
- uchar a,b;
- LcdWriteCom(0x8a); //定義坐標(10,0)
- LcdWriteData(0x03); //寫暫停符號
- while(1){
- if(sp==0){ //發現S/P按鈕按下
- for(b=1;b>0;b--) //延時10微秒以實現消抖
- for(a=2;a>0;a--);
- if(sp==0){
- while(!sp); //等待S/P按鈕松開
- LcdWriteCom(0x8a); //定義坐標(10,0)
- LcdWriteData(0x20); //清除暫停符號
- return; //返回
- }
- }
- }
- }
- void game()
- {
- uchar a,b,c,key,rnd,delay,lives=4; //定義數據槽
- uint score=0; //定義得分槽
- LcdWriteCom(0x01); //清屏
- LcdPrintf(" 1 MAN:"); //放上必需字符
- LcdWriteCom(0xc0); //定義坐標(0,1)
- LcdPrintf("324 SCORE:000000"); //放上必需字符
- LcdWriteCom(0x88); //定義坐標(8,0)
- LcdWriteData(0x00); //放置正常臉CGRAM
- while(1){
- LcdWriteCom(0xca); //定義坐標(10,1)
- LcdPrintNum(score,4); //顯示得分
- if(c==0){
- rnd=rand()%4; //獲取一個小于4的隨機數
- c=1;
- }
- if(k1==0){ //發現1按鈕按下
- delay=1;
- key=0;
- }
- if(k2==0){ //發現2按鈕按下
- delay=1;
- key=1;
- }
- if(k3==0){ //發現3按鈕按下
- delay=1;
- key=2;
- }
- if(k4==0){ //發現4按鈕按下
- delay=1;
- key=3;
- }
- if(sp==0){ //發現S/P按鈕按下
- for(b=1;b>0;b--) //延時10微秒以實現消抖
- for(a=2;a>0;a--);
- if(sp==0){
- while(!sp); //等待S/P按鈕松開
- paused(); //暫停
- }
- }
- if(delay==1){
- if(rnd==0){
- LcdWriteCom(0x81); //定義坐標(1,0)
- LcdWriteData(0x04); //放置鑰匙CGRAM
- }
- if(rnd==1){
- LcdWriteCom(0xc1); //定義坐標(1,1)
- LcdWriteData(0x04); //放置鑰匙CGRAM
- }
- if(rnd==2){
- LcdWriteCom(0xc0); //定義坐標(0,1)
- LcdWriteData(0x04); //放置鑰匙CGRAM
- }
- if(rnd==3){
- LcdWriteCom(0xc2); //定義坐標(2,1)
- LcdWriteData(0x04); //放置鑰匙CGRAM
- }
- if(key==rnd){ //如果這個代碼中找到鑰匙
- LcdWriteCom(0x88); //定義坐標(8,0)
- LcdWriteData(0x01); //放置笑臉CGRAM
- Lcd1602_Delay1ms(1000);
- ++score;
- ++lives;
- if(lives==0)lives=255;
- }
- else{
- LcdWriteCom(0x88); //定義坐標(8,0)
- LcdWriteData(0x02); //放置哭臉CGRAM
- Lcd1602_Delay1ms(1000);
- --lives;
- }
- c=0;
- delay=0;
- //恢復原狀
- LcdWriteCom(0x81);
- LcdWriteData('1');
- LcdWriteCom(0xc1);
- LcdWriteData('2');
- LcdWriteCom(0xc0);
- LcdWriteData('3');
- LcdWriteCom(0xc2);
- LcdWriteData('4');
- LcdWriteCom(0x88);
- LcdWriteData(0x00);
-
- }
- //觀察機會
- LcdWriteCom(0x8c);
- LcdWriteData('L');
- LcdPrintNum(lives,3);
- //當沒有機會旗時且某個代碼中沒有找到鑰匙,游戲結束
- if(lives==0xff){
- LcdWriteCom(0x01); //清屏
- LcdWriteCom(0x80);
- LcdPrintf("GAME OVER"); //顯示字符
- LcdWriteCom(0xc0); //定義坐標(0,1)
- LcdPrintf("SCORE:"); //顯示字符
- //顯示得分
- LcdPrintNum(score,4);
- LcdPrintf("00");
-
- return;
- }
- }
- }
- void main()
- {
- uchar a,b;
- LcdInit(); //LCD初始化
- //將所有CGRAM寫入LCD
- LcdWriteCGRAM(0x00,normal_man);
- LcdWriteCGRAM(0x01,yes_man);
- LcdWriteCGRAM(0x02,no_man);
- LcdWriteCGRAM(0x03,pause);
- LcdWriteCGRAM(0x04,key);
- LcdPrintf("Code Gamer"); //顯示字符
- LcdWriteCom(0xc0); //定義坐標(0,1)
- LcdPrintf("Press S/P Button"); //顯示字符
- while(1){
- if(sp==0){ //發現S/P按鈕按下
- for(b=1;b>0;b--) //延時10微秒以實現消抖
- for(a=2;a>0;a--);
- if(sp==0){
- while(!sp); //等待S/P按鈕松開
- game(); //游戲開始
- }
- }
- }
- }
復制代碼
所有資料51hei提供下載:
code.zip
(49.62 KB, 下載次數: 8)
2018-8-6 17:06 上傳
點擊文件名下載附件
程序文件及仿真 下載積分: 黑幣 -5
|