51单片机的俄罗斯方块.docx
《51单片机的俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《51单片机的俄罗斯方块.docx(37页珍藏版)》请在冰豆网上搜索。
51单片机的俄罗斯方块
C语言程序
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineDOWNTIME30
#defineMAXHANG20
#defineMAXLIE16
#defineMAXPIX3
#definePUSHON50
#defineLCDP2
#defineENP3_0
#defineRWP3_1
#defineRSP3_2
#defineCS1P3_3
#defineCS2P3_4
#defineKEYLEFTP3_5
#defineKEYDOWNP3_6
#defineKEYRIGHP3_7
#defineKEYROTATIONP1_0
uchargkey=0xff,keystate=0,t0ms1=0,t0ms=0,downtimegap=0;
ucharmiao=0,fen=0;
uchardownok;
bitkeyflag,timeupdate,fashionupdate;
ucharidatacubeMap[MAXHANG][2];
typedefstruct{
ucharcode*box;
ucharcube:
4;
ucharstate:
4;
charrow;
charcolumn;
}block;
blockthis;
uintscore=0;
ucharspeed=1;
ucharcodebittable[8]={1,2,4,8,0x10,0x20,0x40,0x80};
ucharcodecube[]=
{
/*■
■■■
*/
0,4,0xe,0,0,2,6,2,0,7,2,0,4,6,4,0,
/*■
■■■
*/
0,8,0xe,0,0,4,4,0xc,0,0,0xe,2,0,6,4,4,
/*■■■
■
*/
0,0xe,8,0,0,4,4,6,0,1,7,0,6,2,2,0,
/*■■
■■
*/
0,0xc,6,0,0,2,6,4,0,6,3,0,2,6,4,0,
/*■■
■■
*/
0,6,0xc,0,0,4,6,2,0,3,6,0,4,6,2,0,
/*■■■■
*/
0,0xf,0,0,4,4,4,4,0,0,0xf,0,2,2,2,2,
/*■■
■■
*/
0,6,6,0,0,6,6,0,0,6,6,0,0,6,6,0
};
ucharcodeasii[]=
{
0x3E,0x51,0x49,0x45,0x3E,//-0-
0x00,0x42,0x7F,0x40,0x00,//-1-
0x62,0x51,0x49,0x49,0x46,//-2-
0x21,0x41,0x49,0x4D,0x33,//-3-
0x18,0x14,0x12,0x7F,0x10,//-4-
0x27,0x45,0x45,0x45,0x39,//-5-
0x3C,0x4A,0x49,0x49,0x31,//-6-
0x01,0x71,0x09,0x05,0x03,//-7-
0x36,0x49,0x49,0x49,0x36,//-8-
0x46,0x49,0x49,0x29,0x1E,//-9-
0x00,0x36,0x36,0x00,0x00,//-:
-10
//next
0x7F,0x04,0x08,0x10,0x7F,//-N-11
0x7F,0x49,0x49,0x49,0x41,//-E-12
0x63,0x14,0x08,0x14,0x63,//-X-13
0x01,0x01,0x7F,0x01,0x01,//-T-14
//speed
0x26,0x49,0x49,0x49,0x32,//-S-15
0x7F,0x09,0x09,0x09,0x06,//-P-16
0x7F,0x49,0x49,0x49,0x41,//-E-17
0x7F,0x41,0x41,0x41,0x3E,//-D-18
//score
0x3E,0x41,0x41,0x41,0x22,//-C-19
0x3E,0x41,0x41,0x41,0x3E,//-O-20
0x7F,0x09,0x19,0x29,0x46,//-R-21
0x00,0x00,0x00,0x00,0x00,//--22
//GAMEOVER
0x3E,0x41,0x51,0x51,0x72,//-G-23
0x7C,0x12,0x11,0x12,0x7C,//-A-24
0x7F,0x02,0x0C,0x02,0x7F,//-M-25
0x1F,0x20,0x40,0x20,0x1F,//-V-26
//TIME
//0x00,0x41,0x7F,0x41,0x00//-I-27
};
////////////////////////////////////////////////////////////////////////////////
voidlcdCmd(ucharcmd)
{
bitea;
ea=EA;
EA=0;
EN=0;
RW=0;
RS=0;
LCD=cmd;
EN=1;
EN=1;
EN=0;
EA=ea;
}
//-------------------------------------------------------------------------------
voidlcdWriteByte(ucharch)
{
EN=0;
RS=1;
RW=0;
LCD=ch;
EN=1;
EN=1;
EN=0;
}
//--------------------------------------------------------------------------------
voidlcdSetPage(ucharpage)
{
page&=0x7;
page+=0xb8;
lcdCmd(page);
}
//--------------------------------------------------------------------------------
voidlcdSetColumn(ucharcolumn)
{
column&=0x3f;
column+=0x40;
lcdCmd(column);
}
//--------------------------------------------------------------------------------
//characterfron=5*8
voidlcdPlayChar(ucharindex,ucharpage,ucharcolume)
{
uchari,temp;
uintp;
p=5*index;
for(i=colume;i{
if(i<64)
{
CS1=1;
CS2=0;
temp=i;
}
else
{
CS1=0;
CS2=1;
temp=i-64;
}
lcdSetPage(page);
lcdSetColumn(temp);
lcdWriteByte(asii[p++]);
}
}
//---------------------------------------------------------------------------------
//rectangle(3,0,50,60)
voidrectangle(void)
{
uchari,page;
CS1=1;
CS2=0;
lcdSetPage(0);
lcdSetColumn
(2);
EN=0;
RS=1;
RW=0;
LCD=0xff;
EN=1;
EN=1;
EN=0;
for(i=3;i<51;i++)
{
EN=0;
RS=1;
RW=0;
LCD=0x1;
EN=1;
EN=1;
EN=0;
}
EN=0;
RS=1;
RW=0;
LCD=0xff;
EN=1;
EN=1;
EN=0;
//---------------------------
for(page=1;page<7;page++)
{
lcdSetPage(page);
lcdSetColumn
(2);
EN=0;
RS=1;
RW=0;
LCD=0xff;
EN=1;
EN=1;
EN=0;
for(i=3;i<51;i++)
{
EN=0;
RS=1;
RW=0;
LCD=0x0;
EN=1;
EN=1;
EN=0;
}
EN=0;
RS=1;
RW=0;
LCD=0xff;
EN=1;
EN=1;
EN=0;
}
//---------------------------
lcdSetPage(7);
lcdSetColumn
(2);
EN=0;
RS=1;
RW=0;
LCD=0x1f;
EN=1;
EN=1;
EN=0;
for(i=3;i<51;i++)
{
EN=0;
RS=1;
RW=0;
LCD=0x10;
EN=1;
EN=1;
EN=0;
}
EN=0;
RS=1;
RW=0;
LCD=0x1f;
EN=1;
EN=1;
EN=0;
}
//--------------------------------------------------------------------
//x:
列;y行,页3*3
voidlcdPutPix(ucharx,uchary,ucharflag)
{
uchari,dat,bitmask,nextbit;
bitbflag,pflag,ea;
x=x*MAXPIX;
y=y*MAXPIX;
bflag=0;
pflag=0;
i=y%8;
if(i==0)
bitmask=0x7;
elseif(i==1)
bitmask=0xe;
elseif(i==2)
bitmask=0x1c;
elseif(i==3)
bitmask=0x38;
elseif(i==4)
bitmask=0x70;
elseif(i==5)
bitmask=0xe0;
elseif(i==6)
{
bflag=1;
bitmask=0xc0;
nextbit=1;
}
elseif(i==7)
{
bflag=1;
bitmask=0x80;
nextbit=3;
}
if(x<62)
{
CS1=1;
CS2=0;
}
elseif(x>63)
{
x-=64;
CS1=0;
CS2=1;
}
else
pflag=1;
lcdSetPage(y/8);
for(i=x;i{
if(pflag)
{
if(i==62||i==63)
{
CS1=1;
CS2=0;
lcdSetPage(y/8);
}
elseif(pflag&&i==64)
{
CS1=0;
CS2=1;
lcdSetPage(y/8);
}
}
lcdSetColumn(i);
ea=EA;
EA=0;
EN=0;
LCD=0xff;
RS=1;
RW=1;
EN=1;
EN=0;
EN=1;
dat=LCD;
EN=0;
if(flag==1)
dat|=bitmask;
else
dat&=~bitmask;
lcdSetColumn(i);
EN=0;
RW=0;
RS=1;
LCD=dat;
EN=1;
EN=1;
EN=0;
EA=ea;
}
if(bflag)
{
lcdSetPage(y/8+1);
for(i=x;i{
if(pflag)
{
if(i==62||i==63)
{
CS1=1;
CS2=0;
lcdSetPage(y/8+1);
}
elseif(pflag&&i==64)
{
CS1=0;
CS2=1;
lcdSetPage(y/8+1);
}
}
lcdSetColumn(i);
ea=EA;
EA=0;
EN=0;
LCD=0xff;
RS=1;
RW=1;
EN=1;
EN=0;
EN=1;
dat=LCD;
EN=0;
if(flag==1)
dat|=nextbit;
else
dat&=~nextbit;
lcdSetColumn(i);
EN=0;
RW=0;
RS=1;
LCD=dat;
EN=1;
EN=1;
EN=0;
EA=ea;
}
}
}
//------------------------------------------------------------------
voidlcdClear(void)
{
uchari,page;
CS1=1;
CS2=0;
for(page=0;page<8;page++)
{
lcdSetPage(page);
lcdSetColumn(0);
for(i=0;i<64;i++)
lcdWriteByte(0);
}
CS1=0;
CS2=1;
for(page=0;page<8;page++)
{
lcdSetPage(page);
lcdSetColumn(0);
for(i=0;i<64;i++)
lcdWriteByte(0);
}
}
//-----------------------------------------------------------------
#defineSTAR53
#defineWIDE6
voidlcdIni(void)
{
lcdCmd(0x3f);
lcdCmd(0xc0);
lcdClear();
rectangle();
//NEXT
lcdPlayChar(11,0,STAR);
lcdPlayChar(12,0,STAR+1*WIDE);
lcdPlayChar(13,0,STAR+2*WIDE);
lcdPlayChar(14,0,STAR+3*WIDE);
//SPEED
lcdPlayChar(15,3,STAR);
lcdPlayChar(16,3,STAR+1*WIDE);
lcdPlayChar(17,3,STAR+2*WIDE);
lcdPlayChar(17,3,STAR+3*WIDE);
lcdPlayChar(18,3,STAR+4*WIDE);
//01
lcdPlayChar(0,4,STAR+2*WIDE);
lcdPlayChar(1,4,STAR+3*WIDE);
//SCORE
lcdPlayChar(15,5,STAR);
lcdPlayChar(19,5,STAR+1*WIDE);
lcdPlayChar(20,5,STAR+2*WIDE);
lcdPlayChar(21,5,STAR+3*WIDE);
lcdPlayChar(12,5,STAR+4*WIDE);
lcdPlayChar(0,6,STAR+1*WIDE);
lcdPlayChar(0,6,STAR+2*WIDE);
lcdPlayChar(0,6,STAR+3*WIDE);
lcdPlayChar(0,6,STAR+4*WIDE);
//TIME
lcdPlayChar(0,7,STAR);
lcdPlayChar(0,7,STAR+1*WIDE);
lcdPlayChar(10,7,STAR+2*WIDE);
lcdPlayChar(0,7,STAR+3*WIDE);
lcdPlayChar(0,7,STAR+4*WIDE);
}
//-----------------------------------------------------------------
voidshowScoreSpeed(void)
{
ucharnum[5];
chari;
uinttemp;
temp=score;
for(i=0;i<5;i++)
{
num[i]=temp%10;
temp=temp/10;
}
for(i=4;i>0;i--)
{
if(num[i]==0)
num[i]=22;
else
break;
}
for(i=4;i>-1;i--)
lcdPlayChar(num[i],6,STAR+(4-i)*WIDE);
lcdPlayChar(speed/10,4,STAR+2*WIDE);
lcdPlayChar(speed%10,4,STAR+3*WIDE);
}
//-------------------------------------------------------------------
voidtimeServer(void)
{
if(timeupdate)
{
timeupdate=0;
lcdPlayChar(fen/10,7,STAR);
lcdPlayChar(fen%10,7,STAR+1*WIDE);
lcdPlayChar(10,7,STAR+2*WIDE);
lcdPlayChar(miao/10,7,STAR+3*WIDE);
lcdPlayChar(miao%10,7,STAR+4*WIDE);
}
if(fashionupdate)
{
fashionupdate=0;
lcdPlayChar(22,7,STAR+2*WIDE);
}
}
//===================================================================
voidt0isr(void)interrupt1
{
ucharkey;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
downtimegap++;
t0ms=++t0ms%100;
if(t0ms==0)
{
timeupdate=1;
miao=++miao%60;
if(miao==0)
fen=++fen%60;
}
if(t0ms==50)
fashionupdate=1;
//----------------------------
key=0xff;
KEYLEFT=1;
KEYRIGH=1;
KEYROTATION=1;
KEYDOWN=1;
if(!
KEYLEFT)
key=0;
if(!
KEYRIGH)
key=1;
if(!
KEYROTATION)
key=2;
if(!
KEYDOWN)
key=3;
switch(keystate)
{
case0:
if(key!
=gkey)
{
gkey=key;
keystate=1;
}
break;
case1:
if(key==gkey)
{
t0ms1=0;
keystate=2;
if(key!
=0xff)
keyflag=1;
}
else
keystate=0;
break;
case2:
if(key==gkey)
{
if(t0ms1t0ms1++;
}
else
{
keystate=0;
keyflag=0;
gkey=0xff;
}
break;
}
}
//===================================================================
voidshowNextCube(ucharcode*p,ucharx,uchary)
{
uchari,j,temp;
for(i=0;i<4;i++)
{
temp=1;
for(j=0;j<4;j++)
{
if(p[i]&temp)
lcdPutPix(x+j,y+i,1);
else
lcdPutPix(x+j,y+i,0);
temp<<=1;
}
}
}
//------------------------------------------------------------------
voidcreateCube(void)
{
staticucharnext;
this.cube=next;
next=TL0%7;
this.row=0;
this.column=6;
this.state=0;
this.box=cube+16*this.cube;
showNextCube(cube+16*next,19,3);
}
//------------------------------------------------------------------
voidshowCubeMap(void)
{
unsignedcharhang,lie,temp;
for(hang=MAXHANG-1;hang>0;hang--)
{
if(cubeMap[hang][0]==0&&cubeMap[hang][1]==0)
break;
for(lie=0;