//定义游戏相关常数
#define uchar unsigned char
#define uint unsigned int
#define MAXHANG 20
#define MAXLIE 16
#define MAXPIX 3
#define PUSHON 50
//定义相关控制引脚
#define LCD P2
#define EN P3_0
#define RW P3_1
#define RS P3_2
#define CS1 P3_3
#define CS2 P3_4
#define KEYLEFT P1_0
#define KEYDOWN P1_7
#define KEYRIGH P1_6
#define KEYROTATION P1_1
uchar gkey=0xff,keystate=0,t0ms1=0,t0ms=0,downtimegap=0;
uchar miao=0,fen=0;
uchar downok;
bit keyflag,timeupdate,fashionupdate;
uchar idata cubeMap[MAXHANG][2];
typedef struct{
uchar code * box;
uchar cube : 4;
uchar state : 4;
char row;
char column;
} block;
block this;
uint score=0;
uchar speed=1;
uchar code bittable[8]={1,2,4,8,0x10,0x20,0x40,0x80};
//定义相应方块对应的数据
uchar code cube[]=
{
/* ■
■■■
*/
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
};
//ASCII码对应的显示常数
uchar code asii[]=
{
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
//GAME OVER
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
};
//向AMPIRE 128*64写入命令
void lcdCmd(uchar cmd)
{
bit ea; //首先设置使能
ea=EA;
EA=0;
EN=0;
RW=0;
RS=0; //对其他控制引脚操作
LCD=cmd; //写入命令
EN=1;
EN=1;
EN=0;
EA=ea;
}
//向AMPIRE 128*64写入IB的数据
void lcdWriteByte(uchar ch)
{
EN=0;
RS=1;
RW=0; //设置相应的时序
LCD=ch; //写入对应的数据
EN=1;
EN=1;
EN=0;
}
//设置AMPIRE 128*64的内部页
void lcdSetPage(uchar page)
{
page &=0x7; //设置对应的页
page +=0xb8;
lcdCmd(page); //写入页码设置命令
}
//设置LCD的显示列
void lcdSetColumn(uchar column)
{
column &=0x3f;
column +=0x40;
lcdCmd(column); //写入列设置命令
}
//在LCD上显示一个5*8的字符
void lcdPlayChar(uchar index,uchar page,uchar colume)
{
uchar i,temp;
uint p;
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)显示长方形
void rectangle(void)
{
uchar i,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 ,在LCD上显示一个点
void lcdPutPix(uchar x, uchar y,uchar flag)
{
uchar i,dat,bitmask,nextbit;
bit bflag,pflag,ea;
x=x*MAXPIX;
y=y*MAXPIX; //计算相应的坐标
bflag=0;
pflag=0;
i=y%8; //根据左边跑
if(i==0)
bitmask=0x7;
else if(i==1)
bitmask=0xe;
else if(i==2)
bitmask=0x1c;
else if(i==3)
bitmask=0x38;
else if(i==4)
bitmask=0x70;
else if(i==5)
bitmask=0xe0;
else if(i==6)
{
bflag=1;
bitmask=0xc0;
nextbit=1;
}
else if(i==7)
{
bflag=1;
bitmask=0x80;
nextbit=3;
}
if(x<62)
{
CS1=1;
CS2=0;
}
else if(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);
}
else if(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);
}
else if(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;
}
}
}
//LCD清除函数
void lcdClear(void)
{
uchar i,page;
CS1=1;
CS2=0;
for(page=0;page<8;page++) //对所有的页操作
{
lcdSetPage(page);
lcdSetColumn(0);
for(i=0;i<64;i++)
lcdWriteByte(0); //全部填充为0
}
CS1=0;
CS2=1;
for(page=0;page<8;page++) //继续下一个页码
{
lcdSetPage(page);
lcdSetColumn(0);
for(i=0;i<64;i++)
lcdWriteByte(0);
}
}
//液晶的初始化函数
#define STAR 53
#define WIDE 6
void lcdIni(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);
//显示速度
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);
//显示第一个行
lcdPlayChar(0,4,STAR+2*WIDE);
lcdPlayChar(1,4,STAR+3*WIDE);
//显示当前得分
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);
//显示当前时间
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);
}
//显示当前的得分和速度
void showScoreSpeed(void)
{
uchar num[5];
char i;
uint temp;
temp=score; //存放得分
for(i=0;i<5;i++)
{
num=temp%10; //把得分拆分为独立的字符
temp=temp/10;
}
for(i=4;i>0;i--)
{
if(num==0)
num=22;
else
break;
}
for(i=4;i>-1;i--)
lcdPlayChar(num,6,STAR+(4-i)*WIDE);
lcdPlayChar(speed/10,4,STAR+2*WIDE);
lcdPlayChar(speed%10,4,STAR+3*WIDE); //在液晶屏幕上显示当前得分
}
//当前的游戏时间记录
void timeServer(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);
}
}
//T0的中断服务子函数
void t0isr(void) interrupt 1
{
uchar key;
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)
{
case 0: if(key!=gkey)
{
gkey=key;
keystate=1;
}
break;
case 1: if(key==gkey)
{
t0ms1=0;
keystate=2;
if(key!=0xff)
keyflag=1;
}
else
keystate=0;
break;
case 2: if(key==gkey)
{
if(t0ms1
t0ms1++;
}
else
{
keystate=0;
keyflag=0;
gkey=0xff;
}
break;
}
}
//提示下一个方块的类型
void showNextCube(uchar code * p,uchar x,uchar y)
{
uchar i,j,temp;
for(i=0;i<4;i++)
{
temp=1;
for(j=0;j<4;j++)
{
if(p & temp)
lcdPutPix(x+j,y+i,1); //将下一个方块的形状显示在指定位置
else
lcdPutPix(x+j,y+i,0);
temp<<=1;
}
}
}
//产生一个方块
void createCube(void)
{
static uchar next;
this.cube=next;
next=TL0%7; //使用TL0作为随机数发生器
this.row=0;
this.column=6;
this.state=0;
this.box=cube+16*this.cube; //使用随机数产生一个方块
showNextCube(cube+16*next,19,3);
}
//显示当前的方块图形
void showCubeMap(void)
{
unsigned char hang,lie,temp;
for(hang=MAXHANG-1;hang>0;hang--)
{
if(cubeMap[hang][0]==0 && cubeMap[hang][1]==0)
break;
for(lie=0;lie<(MAXLIE/8);lie++)
{
temp=8*lie;
if(cubeMap[hang][lie]&0x01)
lcdPutPix(temp+1,hang,1);
if(cubeMap[hang][lie]&0x02)
lcdPutPix(temp+2,hang,1);
if(cubeMap[hang][lie]&0x04)
lcdPutPix(temp+3,hang,1);
if(cubeMap[hang][lie]&0x08)
lcdPutPix(temp+4,hang,1);
if(cubeMap[hang][lie]&0x10)
lcdPutPix(temp+5,hang,1);
if(cubeMap[hang][lie]&0x20)
lcdPutPix(temp+6,hang,1);
if(cubeMap[hang][lie]&0x40)
lcdPutPix(temp+7,hang,1);
if(cubeMap[hang][lie]&0x80)
lcdPutPix(temp+8,hang,1);
}
}
}
//将当前方块加入当前的方块图形
void writeCubeToMap(void)
{
uchar row,column,temp;
uchar hang,lie;
for(row=0;row<4;row++)
{
temp=1;
for(column=0;column<4;column++)
{
if(this.box[row] & temp)
{
hang=this.row+row;
lie=this.column+column;
cubeMap[hang][lie/8] |=bittable[lie%8];
lcdPutPix(lie+1,hang,1);
}
temp<<=1;
}
}
}
//从当前图形中清除这个方块
void clearCubeFromMap(void)
{
uchar row,column,temp;
uchar hang,lie;
for(row=0;row<4;row++)
{
temp=1;
for(column=0;column<4;column++)
{
if(this.box[row] & temp)
{
hang=this.row+row;
lie=this.column+column;
cubeMap[hang][lie/8] &=~bittable[lie%8];
lcdPutPix(lie+1,hang,0);
}
temp<<=1;
}
}
}
//检查当前方块图形是否达到了边界,如果是则返回1,否则返回0
uchar checkBorder(void)
{
if(this.box[3]!=0 && this.row>(MAXHANG-4))
return 1;
else if(this.box[2]!=0 && this.row>(MAXHANG-3))
return 1;
else if(this.box[1]!=0 && this.row>(MAXHANG-2))
return 1;
else if(this.box[0]!=0 && this.row>(MAXHANG-1))
return 1;
//---------------------
if((this.box[0] & 0x01) || (this.box[1] & 0x01) || (this.box[2] & 0x01) ||(this.box[3] & 0x01) )
{
if(this.column<0)
return 1;
}
else if((this.box[0] & 0x02) || (this.box[1] & 0x02) || (this.box[2] & 0x02) ||(this.box[3] & 0x02) )
{
if(this.column<-1)
return 1;
}
else if((this.box[0] & 0x04) || (this.box[1] & 0x04) || (this.box[2] & 0x04) ||(this.box[3] & 0x04) )
{
if(this.column<-2)
return 1;
}
else if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
{
if(this.column<-3)
return 1;
}
//---------------------
if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
{
if(this.column>(MAXLIE-4))
return 1;
}
else if((this.box[0] & 0x04) || (this.box[1] & 0x04) || (this.box[2] & 0x04) ||(this.box[3] & 0x04) )
{
if(this.column>(MAXLIE-3))
return 1;
}
else if((this.box[0] & 0x02) || (this.box[1] & 0x02) || (this.box[2] & 0x02) ||(this.box[3] & 0x02) )
{
if(this.column>(MAXLIE-2))
return 1;
}
else if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
{
if(this.column>(MAXLIE-1))
return 1;
}
//--------------------
return 0;
}
//检查是否要消除
uchar checkClask(void)
{
uchar row,column,temp;
uchar hang,lie;
for(row=0;row<4;row++)
{
temp=1;
for(column=0;column<4;column++)
{
if(this.box[row] & temp)
{
hang=this.row+row;
lie=this.column+column;
if(cubeMap[hang][lie/8] & bittable[lie%8])
return 1;
}
temp<<=1;
}
}
return 0;
}
//检查当前的图形边缘
void checkMap(void)
{
uchar i,j,delete;
bit full;
full=0;
delete=0;
for(i=MAXHANG-1;i>0;i--)
{
if(cubeMap[0]==0 && cubeMap[1]==0)
break;
if(cubeMap[0]==0xff && cubeMap[1]==0xff)
{
delete++;
full=1;
for(j=i;j>0;j--)
{
cubeMap[j][0]=cubeMap[j-1][0];
cubeMap[j][1]=cubeMap[j-1][1];
}
i++;
cubeMap[0][0]=0;
cubeMap[0][1]=0;
}
}
if(full)
{
if(delete==1)
score++;
else if(delete==2)
score+=4;
else if(delete==3)
score+=9;
else if(delete==4)
score+=16;
rectangle();
showCubeMap();
if(score<50)
speed=1;
else if(score<100)
speed=2;
else if(score<500)
speed=3;
else if(score<1000)
speed=4;
else if(score<5000)
speed=5;
else if(score<10000)
speed=6;
else if(score<20000)
speed=7;
else if(score<30000)
speed=8;
else if(score<40000)
speed=9;
else if(score<50000)
speed=10;
else if(score<60000)
speed=11;
else
speed=12;
showScoreSpeed();
}
}
//控制方块向左移动
void moveLeft(void)
{
clearCubeFromMap(); //清除方块
this.column--;
if(checkBorder() || checkClask())
this.column++; //方块的横向位置偏移
writeCubeToMap();
}
//控制方块向右移动
void moveRigh(void)
{ //清除方块
clearCubeFromMap(); //计算方块的横向位置
this.column++;
if(checkBorder() || checkClask())
this.column--;
writeCubeToMap(); //重新绘制方块
}
//向下快速移动方块的函数
void moveDown(void)
{ //首先是清除方块
clearCubeFromMap();
this.row++;
if(checkBorder() || checkClask())
{
this.row--;
downok=1; //方块位置
}
else
downok=0;
writeCubeToMap(); //重新绘制方块
if(downok)
checkMap(); //判断是否还需要下落
}
//方块翻转函数
void cubeRotation(void)
{
uchar temp;
temp=this.state; //先记录当前的相应状态
clearCubeFromMap();
this.state=++this.state%4;
this.box=cube+16*this.cube+4*this.state; //计算方块的相应位置
if(checkBorder() || checkClask())
{
this.state=temp;
this.box=cube+16*this.cube+4*this.state;
}
writeCubeToMap(); //重新绘制方块
}
//主函数
void main(void)
{
TMOD=0x1;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
lcdIni();
//以上为相应的初始化
for(t0ms=0;t0ms
{
cubeMap[t0ms][0]=0;
cubeMap[t0ms][1]=0;
}
while(1)
{
createCube();
if(checkClask())
{
rectangle();
#define SHOWSTAR 12
#define GAP 8
lcdPlayChar(23,2,SHOWSTAR); //GAME
lcdPlayChar(24,2,SHOWSTAR+GAP);
lcdPlayChar(25,2,SHOWSTAR+2*GAP);
lcdPlayChar(12,2,SHOWSTAR+3*GAP);
lcdPlayChar(20,4,SHOWSTAR); //OVER
lcdPlayChar(26,4,SHOWSTAR+GAP);
lcdPlayChar(12,4,SHOWSTAR+2*GAP);
lcdPlayChar(21,4,SHOWSTAR+3*GAP);
t0ms=0;
while(t0ms<95);//延时2秒
t0ms=0;
while(t0ms<95);
((void (code *) (void)) 0x0000) ( );
}
while(1)
{
timeServer(); //更新游戏时间
if(keyflag) //在主循环中判断是否有按键被按下
{
keyflag=0;
t0ms1=0;
if(gkey==0)
moveLeft();
if(gkey==1)
moveRigh();
if(gkey==2)
cubeRotation();
if(gkey==3)
moveDown();
}
if(gkey==0 && t0ms1==PUSHON)
{
t0ms1-=10;
moveLeft();
}
if(gkey==1 && t0ms1==PUSHON)
{
t0ms1-=10;
moveRigh();
}
if(gkey==3 && t0ms1==PUSHON)
{
t0ms1-=10;
moveDown();
}
if(downtimegap>(DOWNTIME-speed))
{
moveDown();
downtimegap=0;
}
if(downok)
{
downok=0;
break;
}
}
}
}
已退回9积分
回帖(3)
2016-5-6 12:56:56
这个我也不知道 你问问大神
这个我也不知道 你问问大神
举报
2016-5-7 17:25:59
呵呵,俄罗斯方块哈!我好像见过你。
QQ1753493268
呵呵,俄罗斯方块哈!我好像见过你。
QQ1753493268
举报
2021-3-11 20:02:03
请问楼主知道了嘛。我也正好用到了。
请问楼主知道了嘛。我也正好用到了。
举报
更多回帖