推箱子游戏设计资料
PG160128A液晶屏初始化源代码(51单片机):
- #define uchar unsigned char
- #define LCD_CHAR 0x14
- sbit cd = P3^0; /*通道选择*/
- sbit rd = P3^1; /*读操作信号*/
- sbit wr = P3^2; /*写操作信号*/
- sbit error0 = P1^5; /*出错提示1*/
- sbit error1 = P1^6; /*出错提示2*/
- sbit error2 = P1^7; /*出错提示3*/
- /*读状态*/
- uchar read_state(){
- uchar temp;
- P2 = 0xff;
- cd = 1;
- rd = 0;
- temp = P2;
- rd = 1;
- return(temp);
- }
- /*STA0指令读写状态,STA1数据读写状态 判断函数*/
- void lcd_enable(){
- uchar i;
- for(i = 10; i > 0; i--)
- if((read_state() & 0x03) == 0x03)
- break;
- if(i==0)error0=1;/*若i==0,说明错误*/
- //else error0=0;
- }
- /*STA2数据自动读状态 判断函数*/
- void atrd_enable(){
- uchar i;
- for(i = 10; i > 0; i--)
- if((read_state() & 0x04) == 0x04)
- break;
- if(i==0)error1=1;/*若i==0,说明错误*/
- //else error1=0;
- }
- /*STA3数据自动写状态 判断函数*/
- void atwr_enable(){
- uchar i;
- for(i = 10; i > 0; i--)
- if((read_state() & 0x08) == 0x08)
- break;
- if(i==0)error2=1;/*若i==0,说明错误*/
- //else error2=0;
- }
- /*写无参数函数*/
- void write_cmd0(uchar cmd){
- lcd_enable();
- cd=1;
- P2=cmd;
- wr=0;
- wr=1;
- }
- /*写单参数函数*/
- void write_cmd1(uchar data1, uchar cmd){
- lcd_enable();
- cd=0;
- P2=data1;
- wr=0;
- wr=1;
- lcd_enable();
- cd=1;
- P2=cmd;
- wr=0;
- wr=1;
- }
- /*写双参数函数*/
- void write_cmd2(uchar data1, uchar data2, uchar cmd){
- lcd_enable();
- cd=0;
- P2=data1;
- wr=0;
- wr=1;
- lcd_enable();
- cd=0;
- P2=data2;
- wr=0;
- wr=1;
- lcd_enable();
- cd=1;
- P2=cmd;
- wr=0;
- wr=1;
- }
- /*写数据函数*/
- void write_data(uchar data0){
- lcd_enable();
- cd=1;
- P2=data0;
- wr=0;
- wr=1;
- }
- /*读数据函数*/
- uchar read_data(){
- char temp;
- lcd_enable();
- cd = 0;
- P2 = 0xff;
- rd = 0;
- temp = P2;
- rd = 1;
- return(temp); /*若返回0,可能错误*/
- }
- /*自动写开始*/
- void auto_write(){
- write_cmd0(AUT_WR);
- }
- /*自动读开始*/
- void auto_read(){
- write_cmd0(AUT_RD);
- }
- /*自动写结束*/
- void atwr_stop(){
- write_cmd0(AUT_WO);
- }
- /*自动读结束*/
- void atrd_stop(){
- write_cmd0(AUT_RO);
- }
- /*数据一次写函数*/
- void write_one(uchar data1, char way){
- atwr_enable();
- auto_write();
- write_cmd1(data1,way);
- atwr_stop();
- }
- /*数据一次读函数*/
- uchar read_one(char way){
- uchar temp;
- atrd_enable();
- auto_read();
- write_cmd0(way);
- temp = read_data();
- atrd_stop();
- return(temp);
- }
- /*设置当前显示位置函数x,y从0开始表示单位为字符*/
- void set_xy(uchar x, uchar y){
- int temp;
- temp = y * LCD_CHAR + x;
- write_cmd2(temp&0xff,temp/0xff,ADR_POS);
- }
- void set_adr(uchar D1, uchar D2){
- write_cmd2(D1,D2,ADR_POS);
- }
- /*设置光标指针 x,y从0开始*/
- void set_cur(char x, char y){
- write_cmd2(x,y,CUR_POS);
- }
- /*CGRAM偏置地址设置函数*/
- void set_cgram(){
- write_cmd2(0x01,0x00,CGR_POS);//0000,1100,0000,0000 0C00
- }
- /*液晶初始化函数(文本区首地址D1,文本区首地址D2, 文本区宽度, 图形区首地址D1, 图形区首地址D2, 图形区宽度, 光标形状, 显示方式, 显示开关)*/
- void lcd_init(uchar txtstpd1, uchar txtstpd2, uchar txtwid, uchar grhstpd1, uchar grhstpd2, uchar grhwid, uchar cur, uchar mod, uchar sw){
- write_cmd2(txtstpd1,txtstpd2,TXT_STP); /*文本区首地址*/
- write_cmd2(txtwid,0x00,TXT_WID); /*文本区宽度*/
- write_cmd2(grhstpd1,grhstpd2,GRH_STP); /*图形区首地址*/
- write_cmd2(grhwid,0x00,GRH_WID); /*图形区宽度*/
- write_cmd0(CUR_SHP | cur); /*光标形状*/
- write_cmd0(mod); /*显示方式*/
- write_cmd0(DIS_SW | sw); /*显示开关*/
- }
复制代码
部分源码预览:
- /*获取当前行,列*/
- /*设置当前行,列*/
- uchar g=0;
- void delay(int c){
- int i, j;
- for(i = 0; i < c; i++)
- for(j = 0; j < 1000; j++);
- }
- /*清屏 320 = (160/8) * (128/8) = 20 * 16 = 320*/
- void cls(void){
- int i;
- set_xy(0,0);
- for(i = 0; i < 320; i++)
- write_one(0x94,INC_WR);
- }
- uchar curx,cury; /*纪录当前人物所在位置*/
- uchar level_temp[8][8]={
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- };
- void wirte_bg(void){
- int i;
- set_adr(0x50,0x01);
- for(i = 0; i < 2560; i++)
- write_one(bg[i],INC_WR);
- }
- void wirte_cgrom(void){
- int i;
- // set_adr(0x50,0x01);
- // for(i = 0; i < 2560; i++)
- // write_one(0xff,INC_WR);
- set_adr(0x00,0x0c);
- /*自定义字符写入CGROM*/
- for(i = 0; i < 848; i++)
- write_one(Lattice[i],INC_WR);
- }
- void start(void){
- uchar i;
- set_xy(0,0);
- for(i=0;i<20;i++)
- write_one(0x95,INC_WR);
- set_xy(0,15);
- for(i=0;i<20;i++)
- write_one(0x95,INC_WR);
- for(i=0;i<15;i++){
- set_xy(0,i);
- write_one(0x95,INC_WR);
- set_xy(19,i);
- write_one(0x95,INC_WR);
- }
- set_xy(18,1);
- write_one(0x96,INC_WR);
- set_xy(18,14);
- write_one(0x97,INC_WR);
- set_xy(1,1);
- write_one(0x98,INC_WR);
- set_xy(1,14);
- write_one(0x99,INC_WR);
- set_xy(7,6);
- write_one(0xaa,INC_WR);
- write_one(0xab,INC_WR);
- write_one(0xae,INC_WR);
- write_one(0xaf,INC_WR);
- write_one(0xb2,INC_WR);
- write_one(0xb3,INC_WR);
- set_xy(7,7);
- write_one(0xac,INC_WR);
- write_one(0xad,INC_WR);
- write_one(0xb0,INC_WR);
- write_one(0xb1,INC_WR);
- write_one(0xb4,INC_WR);
- write_one(0xb5,INC_WR);
- set_xy(6,8);
- write_one(0x9a,INC_WR);
- write_one(0x9b,INC_WR);
- write_one(0x9e,INC_WR);
- write_one(0x9f,INC_WR);
- write_one(0xa2,INC_WR);
- write_one(0xa3,INC_WR);
- write_one(0xa6,INC_WR);
- write_one(0xa7,INC_WR);
- set_xy(6,9);
- write_one(0x9c,INC_WR);
- write_one(0x9d,INC_WR);
- write_one(0xa0,INC_WR);
- write_one(0xa1,INC_WR);
- write_one(0xa4,INC_WR);
- write_one(0xa5,INC_WR);
- write_one(0xa8,INC_WR);
- write_one(0xa9,INC_WR);
- while(i){ /*此while语句判断确定键超级技巧*/
- switch(P1&0x1f){
- case 0x0f:
- i=0;
- break;
- }
- }
- }
- void guan(void){
- /*推*/
- set_xy(16,0);
- write_one(0xaa,INC_WR);
- write_one(0xab,INC_WR);
- set_xy(16,1);
- write_one(0xac,INC_WR);
- write_one(0xad,INC_WR);
- /*箱*/
- set_xy(16,2);
- write_one(0xae,INC_WR);
- write_one(0xaf,INC_WR);
- set_xy(16,3);
- write_one(0xb0,INC_WR);
- write_one(0xb1,INC_WR);
- /*子*/
- set_xy(16,4);
- write_one(0xb2,INC_WR);
- write_one(0xb3,INC_WR);
- set_xy(16,5);
- write_one(0xb4,INC_WR);
- write_one(0xb5,INC_WR);
- /*第*/
- set_xy(16,8);
- write_one(0xd2,INC_WR);
- write_one(0xd3,INC_WR);
- set_xy(16,9);
- write_one(0xd4,INC_WR);
- write_one(0xd5,INC_WR);
- /*几*/
- set_xy(16,10);
- write_one(0xd6,INC_WR);
- write_one(0xd6+2*(g+1),INC_WR);
- set_xy(16,11);
- write_one(0xd7,INC_WR);
- write_one(0xd7+2*(g+1),INC_WR);
- /*关*/
- set_xy(16,12);
- write_one(0xce,INC_WR);
- write_one(0xcf,INC_WR);
- set_xy(16,13);
- write_one(0xd0,INC_WR);
- write_one(0xd1,INC_WR);
- /*阿*/
- set_xy(18,0);
- write_one(0x9a,INC_WR);
- write_one(0x9b,INC_WR);
- set_xy(18,1);
- write_one(0x9c,INC_WR);
- write_one(0x9d,INC_WR);
- /*С*/
- set_xy(18,2);
- write_one(0x9e,INC_WR);
- write_one(0x9f,INC_WR);
- set_xy(18,3);
- write_one(0xa0,INC_WR);
- write_one(0xa1,INC_WR);
- /*制*/
- set_xy(18,4);
- write_one(0xa2,INC_WR);
- write_one(0xa3,INC_WR);
- set_xy(18,5);
- write_one(0xa4,INC_WR);
- write_one(0xa5,INC_WR);
- /*作*/
- set_xy(18,6);
- write_one(0xa6,INC_WR);
- write_one(0xa7,INC_WR);
- set_xy(18,7);
- write_one(0xa8,INC_WR);
- write_one(0xa9,INC_WR);
- }
- void printc(uchar i, uchar j, uchar c){
- set_xy(i*2,j*2);
- switch(c){
- case 0:
- write_one(0x94,INC_WR);
- write_one(0x94,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x94,INC_WR);
- write_one(0x94,INC_WR);
- break;
- case 1: /*人物1*/
- write_one(0x80,INC_WR);
- write_one(0x81,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x82,INC_WR);
- write_one(0x83,INC_WR);
- break;
- case 2: /*砖头2*/
- write_one(0x84,INC_WR);
- write_one(0x85,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x86,INC_WR);
- write_one(0x87,INC_WR);
- break;
- case 3: /*箱子3*/
- write_one(0x88,INC_WR);
- write_one(0x89,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x8a,INC_WR);
- write_one(0x8b,INC_WR);
- break;
- case 4: /*目的4*/
- write_one(0x8c,INC_WR);
- write_one(0x8d,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x8e,INC_WR);
- write_one(0x8f,INC_WR);
- break;
- case 5: /*成功5*/
- write_one(0x90,INC_WR);
- write_one(0x91,INC_WR);
- set_xy(i*2,j*2+1);
- write_one(0x92,INC_WR);
- write_one(0x93,INC_WR);
- break;
- }
-
- }
- void pushbox(){
- uchar i,j;
- /*根据level.h中的值进行输出单个字符点阵为16*16,显示8*8个字符*/
- for(i = 0; i < 8; i++)
- for(j = 0; j < 8; j++){
- level_temp[i][j]=level[g][j][i];
- switch(level_temp[i][j]){
- case 0:
- printc(i,j,0);
- break;
- case 1: /*人物1*/
- curx=i;
- cury=j;
- printc(i,j,1);
- break;
- case 2: /*砖头2*/
- printc(i,j,2);
- break;
- case 3: /*箱子3*/
- printc(i,j,3);
- break;
- case 4: /*目的4*/
- printc(i,j,4);
- break;
- case 5: /*成功5*/
-
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
下载:
推箱子.zip
(75.42 KB, 下载次数: 60)
|