《单片机俄罗斯方块程序(共14页).doc》由会员分享,可在线阅读,更多相关《单片机俄罗斯方块程序(共14页).doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上#includereg52.h#define WINDOW_XADDR_START0x0050 / Horizontal Start Address Set#define WINDOW_XADDR_END0x0051 / Horizontal End Address Set#define WINDOW_YADDR_START0x0052 / Vertical Start Address Set#define WINDOW_YADDR_END0x0053 / Vertical End Address Set#define GRAM_XADDR 0x0020 / GRAM
2、 Horizontal Address Set#define GRAM_YADDR 0x0021 / GRAM Vertical Address Set#define GRAMWR 0x0022 / memory write#define uint unsigned int#define uchar unsigned charsbit CS=P22;/片选sbit RES=P21;/复位sbit RS=P24;/数据/命令选择sbit RW=P25; const uint col9=0x0000,0xFFFF,0x001F,0xFFE0,0xF800,0xF81F,0x07E0,0x7FFF,
3、0x051F; /Black White BlueYellow Red Magenta Green Cyan Blue2const uint boxes7= /方块样式存储数组 0x0f00,0x0740,0x0e20,0x0e40,0x0c60,0x06c0,0x0660 ;uint fi23=0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003, 0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003, 0xffff,0x
4、ffff,0xffff;uchar a,b; uint t,p; uint score; uchar speed,leve,time; /游戏速度 uchar pcolor,color;void Init_Timer1(void) TMOD |= 0x10; /使用模式1,16位定时器,使用|符号可以在使用多个定时器时不受影响 TH1=0x3c; /给定初值,这里使用定时器最大值从0开始计数一直到65535溢出 TL1=0xb0; EA=1; /总中断打开 ET1=1; /定时器中断打开 TR1=1; /定时器开关打开void delay(unsigned int i) /延时函数while(
5、i-);uchar keyscan(void) /键盘扫描函数,使用行列反转扫描法 uchar k; P1=0xff; k=P1; if(k!=0xff) delay(100); if(k!=0xff) k=P1;if(k=0xfd) return 1; if(k=0xfb)return 2;if(k=0xf7) return 3;if(k=0xef) return 4; return 8; void delayms(uint count) int i,j; for(i=0;icount;i+) for(j=0;j8;n=y;Write_Cmd(0x00,x);Write_Data(m,n);
6、void Write_Data_U16(uint y)unsigned char m,n;m=y8;n=y;Write_Data(m,n);static void LCD_SetPos(uint x0,uint x1,uint y0,uint y1) Write_Cmd_Data(WINDOW_XADDR_START,x0); Write_Cmd_Data(WINDOW_XADDR_END,x1); Write_Cmd_Data(WINDOW_YADDR_START,y0); Write_Cmd_Data(WINDOW_YADDR_END,y1); Write_Cmd_Data(GRAM_XA
7、DDR,x0); Write_Cmd_Data(GRAM_YADDR,y0); Write_Cmd (0x00,0x22);/LCD_WriteCMD(GRAMWR);void ILI9325_Initial(void) CS=1;delayms(5);RES=0;delayms(5);RES=1;delayms(5); delayms(100); Write_Cmd_Data(0x0001,0x0100); Write_Cmd_Data(0x0002,0x0700); Write_Cmd_Data(0x0003,0x1030); Write_Cmd_Data(0x0004,0x0000);
8、Write_Cmd_Data(0x0008,0x0207); Write_Cmd_Data(0x0009,0x0000); Write_Cmd_Data(0x000A,0x0000); Write_Cmd_Data(0x000C,0x0000); Write_Cmd_Data(0x000D,0x0000); Write_Cmd_Data(0x000F,0x0000);/power on sequence VGHVGL Write_Cmd_Data(0x0010,0x0000); Write_Cmd_Data(0x0011,0x0007); Write_Cmd_Data(0x0012,0x000
9、0); Write_Cmd_Data(0x0013,0x0000); /vgh Write_Cmd_Data(0x0010,0x1290); Write_Cmd_Data(0x0011,0x0227); /delayms(100); /vregiout Write_Cmd_Data(0x0012,0x001d); /0x001b /delayms(100); /vom amplitude Write_Cmd_Data(0x0013,0x1500); /delayms(100); /vom H Write_Cmd_Data(0x0029,0x0018); Write_Cmd_Data(0x002
10、B,0x000D); /gamma Write_Cmd_Data(0x0030,0x0004); Write_Cmd_Data(0x0031,0x0307); Write_Cmd_Data(0x0032,0x0002);/ 0006 Write_Cmd_Data(0x0035,0x0206); Write_Cmd_Data(0x0036,0x0408); Write_Cmd_Data(0x0037,0x0507); Write_Cmd_Data(0x0038,0x0204);/0200 Write_Cmd_Data(0x0039,0x0707); Write_Cmd_Data(0x003C,0
11、x0405);/ 0504 Write_Cmd_Data(0x003D,0x0F02); /ram Write_Cmd_Data(0x0050,0x0000); Write_Cmd_Data(0x0051,0x00EF); Write_Cmd_Data(0x0052,0x0000); Write_Cmd_Data(0x0053,0x013F); Write_Cmd_Data(0x0060,0xA700); Write_Cmd_Data(0x0061,0x0001); Write_Cmd_Data(0x006A,0x0000); / Write_Cmd_Data(0x0080,0x0000);
12、Write_Cmd_Data(0x0081,0x0000); Write_Cmd_Data(0x0082,0x0000); Write_Cmd_Data(0x0083,0x0000); Write_Cmd_Data(0x0084,0x0000); Write_Cmd_Data(0x0085,0x0000); / Write_Cmd_Data(0x0090,0x0010); Write_Cmd_Data(0x0092,0x0600); Write_Cmd_Data(0x0093,0x0003); Write_Cmd_Data(0x0095,0x0110); Write_Cmd_Data(0x00
13、97,0x0000); Write_Cmd_Data(0x0098,0x0000); Write_Cmd_Data(0x0007,0x0133);uchar dir_y(uchar k) return (1-k%2)*(k/2*2-1);uchar dir_x(uchar k) return (k%2)*(k/2*2-1);void ClearScreen(void) uint i,j; LCD_SetPos(0,240,0,320);/320x240 for (i=0;i320;i+) for (j=0;j=310) Write_Data_U16(col8); else Write_Data
14、_U16(col0); void SET(char x,char y,uchar k) uint i,bColor; bColor=colk; LCD_SetPos(30+x*15,43+x*15,10+y*15,23+y*15); for (i=0;i225;i+) Write_Data_U16(bColor);void SET1(uint z,uchar k)char i;i=0; while(i16) if(z&0x8000) SET(i%4+a,i/4+b,k); z=1;i+; uchar NUM(uchar n)switch(n)case 0:return 0x3f;break;
15、case 1:return 0x06;break; case 2:return 0x5b;break; case 3:return 0x4f;break; case 4:return 0x66;break; case 5:return 0x6d;break; case 6:return 0x7d;break; case 7:return 0x07;break; case 8:return 0x7f;break; case 9:return 0x6f;break;return 0x00;void OUTNUM(char x,char y,uchar n)int a,b,i;a=30+x*15,b
16、=10+y*15;LCD_SetPos(a,a+14,b,b+31); for (i=0;i512;i+) Write_Data_U16(col0);if(n&0x80)LCD_SetPos(a+14,a+15,b+30,b+31); for(i=0;i4;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+4,a+11,b+16,b+17); for(i=0;i16;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+2,a+3,b+4,b+15); for(i=0;i24;i+) W
17、rite_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+2,a+3,b+18,b+29); for(i=0;i24;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+4,a+11,b+30,b+31); for(i=0;i16;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+12,a+13,b+18,b+29); for(i=0;i24;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+
18、12,a+13,b+4,b+15); for(i=0;i24;i+) Write_Data_U16(col8); n=1;if(n&0x80)LCD_SetPos(a+4,a+11,b+2,b+3); for(i=0;i16;i+) Write_Data_U16(col8); void PRSCORE(void) uint k;char i,n,q; k=score;i=1; while(k) n=NUM(k%10); OUTNUM(i,2,n); k/=10;i-; OUTNUM(-2,12,0x38); q=speed;i=0; while(q) n=NUM(q%10); OUTNUM(i
19、,12,n); q/=10;i-; uchar CANMOVE(uchar q)/判定方块是否能朝 q 方向移动 uchar i,j,z;uint k; if(!p) return 0; i=a+dir_x(q);j=b+dir_y(q); z=0;k=0; while(z4) k=k+(fij+z(4*z); z+; if(k&p) return 0; else return 1;void MOVE(uchar q)/朝 q 方向移动方块 SET1(p,0); a+=dir_x(q);b+=dir_y(q); SET1(p,color);void SYS(void)/将可活动的方块都变为不可
20、活动的方块 uchar z;uint k; z=0;k=p; while(za); k=4;z+; p=0;void TURN(void)/方块变形 uint k,p1;uchar z; switch(p) case 0x0f00: p1=0x4444;break;case 0x4444: p1=0x0f00;break; case 0x0740: p1=0x0622;break;case 0x0622: p1=0x02e0;break;case 0x02e0: p1=0x4460;break;case 0x4460: p1=0x0740;break; case 0x0e20: p1=0x22
21、60;break;case 0x2260: p1=0x0470;break;case 0x0470: p1=0x0644;break;case 0x0644: p1=0x0e20;break; case 0x0e40: p1=0x4c40;break;case 0x4c40: p1=0x4e00;break;case 0x4e00: p1=0x4640;break;case 0x4640: p1=0x0e40;break; case 0x0c60: p1=0x2640;break;case 0x2640: p1=0x0c60;break; case 0x06c0: p1=0x4620;brea
22、k;case 0x4620: p1=0x06c0;break; case 0x0660: p1=0x0660;break; z=0;k=0; while(z4) k=k+(fib+z(4*z); z+; if(!(k&p1) SET1(p,0);p=p1;SET1(p,color); uchar SCORED(void)/满行消去,返回加分 uchar i,j,k,s=0,bcolor; bcolor=time%8+1; for(k=19;k0;k-)if(fik=0xffff) for(i=2;i0;j-) if(fiji)&0x8000)(fij-1i)&0x8000) if(fij0;j
23、-) fij=fij-1;k+;s+; return s; void putbox(void)/随机取一个方块,输出到左边并等待下次放出 char i;uint k; t=boxestime%7; pcolor=time%8+1; i=0;k=t; while(i16) if(k&0x8000) SET(i%4-2,i/4+5,pcolor); else SET(i%4-2,i/4+5,0); k1) leve-;speed=leve+score/200;PRSCORE();if(k=3&levecm&cn-cmins) break;if(cnins) break; while(p);if(CANMOVE(2) MOVE(2);else SYS();/方块自动下移,不可下移时把可移动方块变为不可移动 k=SCORED(); /满行消去,返回加分 if(k) score+=(k+1)*(k+1);speed=leve+score/200; PRSCORE(); if(!p) k=getbox(); if(!k) break; putbox(); /无法放出方块己方游戏结束 void Timer1_isr(void) interrupt 3 using 1 TH1=0x3c; /重新赋值 TL1=0xb0; time+; if(time100) time=0;专心-专注-专业
限制150内