學習了隨機數的產生
就打算寫一個與隨機數有關的游戲機
因此。。。。。。
整體電路圖.png (88.61 KB, 下載次數: 73)
下載附件
整體電路圖
2018-6-15 20:50 上傳
效果圖1.png (21.75 KB, 下載次數: 78)
下載附件
效果圖1
2018-6-15 20:50 上傳
效果圖2.png (23.12 KB, 下載次數: 72)
下載附件
效果圖2
2018-6-15 20:50 上傳
效果圖3.jpg (186.7 KB, 下載次數: 76)
下載附件
效果圖3
2018-6-15 20:50 上傳
效果圖4.jpg (265.31 KB, 下載次數: 61)
下載附件
效果圖4
2018-6-15 20:50 上傳
效果圖5.jpg (257.2 KB, 下載次數: 80)
下載附件
效果圖5
2018-6-15 20:50 上傳
main.c如下:
- #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 lives[]={ //機會旗CGRAM
- bin(0,0,0,1,0,0,0,0),
- bin(0,0,0,1,1,0,0,0),
- bin(0,0,0,1,1,1,0,0),
- bin(0,0,0,1,1,1,1,0),
- bin(0,0,0,1,1,1,1,1),
- bin(0,0,0,1,0,0,0,0),
- bin(0,0,0,1,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(0x04); //寫暫停符號
- 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
- LcdWriteCom(0x8c); //定義坐標(12,0)
- LcdWriteData(0x03); //放置機會旗CGRAM
- LcdWriteData(0x03); //放置機會旗CGRAM
- LcdWriteData(0x03); //放置機會旗CGRAM
- LcdWriteData(0x03); //放置機會旗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(0x05); //放置鑰匙CGRAM
- }
- if(rnd==1){
- LcdWriteCom(0xc1); //定義坐標(1,1)
- LcdWriteData(0x05); //放置鑰匙CGRAM
- }
- if(rnd==2){
- LcdWriteCom(0xc0); //定義坐標(0,1)
- LcdWriteData(0x05); //放置鑰匙CGRAM
- }
- if(rnd==3){
- LcdWriteCom(0xc2); //定義坐標(2,1)
- LcdWriteData(0x05); //放置鑰匙CGRAM
- }
- if(key==rnd){ //如果這個代碼中找到鑰匙
- LcdWriteCom(0x88); //定義坐標(8,0)
- LcdWriteData(0x01); //放置笑臉CGRAM
- Lcd1602_Delay1ms(1000);
- ++score;
- }
- 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);
-
- }
- //減少機會,機會旗消耗
- if(lives==3){
- LcdWriteCom(0x8f);
- LcdWriteData(0x20);
- }
- if(lives==2){
- LcdWriteCom(0x8e);
- LcdWriteData(0x20);
- }
- if(lives==1){
- LcdWriteCom(0x8d);
- LcdWriteData(0x20);
- }
- if(lives==0){
- LcdWriteCom(0x8c);
- LcdWriteData(0x20);
- }
-
- //當沒有機會旗時且某個代碼中沒有找到鑰匙,游戲結束
- 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,lives);
- LcdWriteCGRAM(0x04,pause);
- LcdWriteCGRAM(0x05,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(); //游戲開始
- }
- }
- }
- }
復制代碼
lcd.c如下:
lcd.h如下: - #ifndef __LCD_H_
- #define __LCD_H_
- /**********************************
- 當使用的是4位數據傳輸的時候定義,
- 使用8位取消這個定義
- **********************************/
- //#define LCD1602_4PINS
- /**********************************
- 包含頭文件
- **********************************/
- #include<reg52.h>
- //---重定義關鍵詞---//
- #ifndef uchar
- typedef unsigned char uchar;
- #endif
- #ifndef uint
- typedef unsigned int uint;
- #endif
- /**********************************
- PIN口定義
- **********************************/
- #define LCD1602_DATAPINS P0
- sbit LCD1602_E=P2^7;
- sbit LCD1602_RW=P2^5;
- sbit LCD1602_RS=P2^6;
- /**********************************
- 函數聲明
- **********************************/
- /*在51單片機12MHZ時鐘下的延時函數*/
- void Lcd1602_Delay1ms(uint c); //誤差 0us
- /*LCD1602寫入8位命令子函數*/
- void LcdWriteCom(uchar com);
- /*LCD1602寫入8位數據子函數*/
- void LcdWriteData(uchar dat) ;
- /*LCD1602初始化子程序*/
- void LcdInit();
- /*LCD1602向屏幕輸出字符*/
- void LcdPrintf(uchar *s);
- /*建立一個自定義字符*/
- void LcdWriteCGRAM(uchar index,uchar *p);
- /*顯示數字*/
- void LcdPrintNum(uint num,uchar wei);
- #endif
復制代碼
|