学习了随机数的产生
就打算写一个与随机数有关的游戏机
因此。。。。。。
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
复制代码
|