|
楼主 |
发表于 2008-5-8 21:15:32
|
显示全部楼层
程序贴出来
这个是boxgame.c:
#include"../5110LCD/5110LCD.h"
#include"../TABLES/bmp.h"
#include"../TABLES/english_6x8.h"
#include"../KEYSCAN/keyscan.h"
#include"../DELAY/delayh.h"
#include<iom16v.h>
#include<MACROS.h>
#include"boxgame.h"
/****************************全局变量**********************************/
unsigned char mapArray[MapLength][2] = //总地图
{
0xff,0xff,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,
0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0xff,0xff
};
unsigned char allScore = 0;//总分即所消的行数
unsigned char MapXBox = 7;//此刻图形的X坐标
unsigned char MapYBox = 0;//此刻图形的Y坐标
unsigned char MapNumBox = 0;//此刻加载上来的图形名
unsigned char MapNumBoxNext = 0;//下一个的图形名
unsigned char RotatePosition = 0;//旋转的位置
unsigned char times = 0;//计数
unsigned char TimeLevel = 5;//定时器溢出次数,可以作为难度级别
/****************************code量**********************************/
const unsigned char table2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const unsigned char boxesArray[7][4][4]=//七种图形,四种方向,[图形][方向][点阵]
{
/*
□□□□ □□□□ □□□□ □□□□
□■□□ □□□□ □■■□ □□□□
□■□□ □□■□ □□■□ ■■■□
□■■□ ■■■□ □□■□ ■□□?
*/
0x00,0x0e,0x08,0x00,
0x08,0x08,0x0c,0x00,
0x00,0x02,0x0e,0x00,
0x0c,0x04,0x04,0x00,
/*
□□□□ □□□□ □□□□ □□□□
□■■□ ■■■□ □□■□ ■□□□
□■□□ □□■□ □□■□ ■■■□
□■□□ □□□□ □■■□ □□□□
*/
0x00,0x0e,0x02,0x00,
0x06,0x04,0x04,0x00,
0x00,0x08,0x0e,0x00,
0x02,0x02,0x06,0x00,
/*
□□□□ □□□□ □□□□ □□□□
□□□□ □■□□ □□□□ ■□□□
□■□□ ■■□□ ■■■□ ■■□□
■■■□ □■□□ □■□□ ■□□□
*/
0x08,0x0c,0x08,0x00,
0x04,0x0e,0x00,0x00,
0x04,0x0c,0x04,0x00,
0x0e,0x04,0x00,0x00,
/*
□□□□ □□□□ □□□□ □□□□
□□□□ ■□□□ □□□□ ■□□□
□■■□ ■■□□ □■■□ ■■□□
■■□□ □■□□ ■■□□ □■□□
*/
0x08,0x0c,0x04,0x00,
0x06,0x0c,0x00,0x00,
0x08,0x0c,0x04,0x00,
0x06,0x0c,0x00,0x00,
/*
□□□□ □□□□ □□□□ □□□□
□□□□ □■□□ □□□□ □■□□
■■□□ ■■□□ ■■□□ ■■□□
□■■□ ■□□□ □■■□ ■□□□
*/
0x04,0x0c,0x08,0x00,
0x0c,0x06,0x00,0x00,
0x04,0x0c,0x08,0x00,
0x0c,0x06,0x00,0x00,
/*
□■□□ □□□□ □□■□ □□□□
□■□□ □□□□ □□■□ ■■■■
□■□□ ■■■■ □□■□ □□□□
□■□□ □□□□ □□■□ □□□□
*/
0x00,0x0f,0x00,0x00,
0x04,0x04,0x04,0x04,
0x00,0x00,0x0f,0x00,
0x02,0x02,0x02,0x02,
/*
□□□□ □□□□ □□□□ □□□□
□□□□ □□□□ □□□□ □□□□
□■■□ □■■□ □■■□ □■■□
□■■□ □■■□ □■■□ □■■□
*/
0x00,0x0c,0x0c,0x00,
0x00,0x0c,0x0c,0x00,
0x00,0x0c,0x0c,0x00,
0x00,0x0c,0x0c,0x00,
};
/**************************控制函数************************************/
void T1_Init(void)//初始化定时器
{
TCCR1A &= ~(1 << WGM10);
TCCR1A &= ~(1 << WGM11);
TCCR1B &= ~(1 << WGM12);
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << CS10);
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS12);
TIMSK |= (1 << TOIE1);
}
void drawStaticElements(void)//显示静态元素,即筐架
{
DrawRectangleLine(57,0,83,47,1);//右筐
DrawGraphics(font6x8[46],6,8,58,2);//N
DrawGraphics(font6x8[37],6,8,64,2);//E
DrawGraphics(font6x8[56],6,8,70,2);//X
DrawGraphics(font6x8[52],6,8,76,2);//T
DrawLine(58,26,82,26);//下线
DrawLine(58,27,82,27);//下线
DrawGraphics(font6x8[44],6,8,58,29);//l
DrawGraphics(font6x8[41],6,8,64,29);//i
DrawGraphics(font6x8[46],6,8,70,29);//n
DrawGraphics(font6x8[37],6,8,76,29);//e
DrawLine(60,45,80,45);//下线
}
unsigned char GetBaiwei(unsigned char temp)//从整数中取百位数
{
temp = temp/100;
return temp;
}
unsigned char GetShiwei(unsigned char temp)//从整数中取十位数
{
if(temp > 99)
{
temp = temp - GetBaiwei(temp)*100;
}
temp = temp/10;
return temp;
}
unsigned char GetGewei(unsigned char temp)//从整数中取个位数
{
temp = temp%10;
return temp;
}
void disPlayScore(void)//把分数显示出来
{
ClearArea(61,36,19,9);
if(allScore <= 0xff)
{
DrawGraphics(font6x8[16+GetBaiwei(allScore)],6,8,61,37);
DrawGraphics(font6x8[16+GetShiwei(allScore)],6,8,67,37);
DrawGraphics(font6x8[16+GetGewei(allScore)],6,8,73,37);
}
}
void disPlayMap(void)//把地图显示出来
{
unsigned char i,j,temp1,temp2,temp3;//temp1判断是上字节还是下字节,temp2把0-15转换成两个0-7,temp3左移的中间变量
for(i=0;i<MapLength;i++)
{
for(j=0;j<MapWeight;j++)//16-1
{
if(j>7)//8-15
{
temp1 = 1;
temp2 = j - 8;//0-7
}
else//0-7
{
temp1 = 0;
temp2 = j;//0-7
}
temp3 = mapArray[temp1] << (7 - temp2);
temp3 = temp3 >> 7;
if(temp3 == 1)//如果有1就画方块
{
DrawGraphics(box1,3,3,i*3,j*3);
}
else//如果没1就擦掉
{
ClearArea(i*3,j*3,3,3);
}
}
}
}
void DrawOrEraseBoxOnMap(unsigned char x,unsigned char y,unsigned char DrawOrErase)
//从0开始,DrawOrErase当是0的时候擦除,是1的时候写入
{
unsigned char temp,temp1;
if((y + 1) % 8 == 0)
{
temp = (y + 1) / 8;
}
else
{
temp = ((y + 1) / 8) + 1;
}
temp1 = y % 8;
if(DrawOrErase == 1)
{
mapArray[x][temp - 1] |= table2[temp1];
}
else
{
mapArray[x][temp - 1] &= ~table2[temp1];
}
}
void attachOrEraseBox(unsigned char DrawOrErase)
{
unsigned char i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(CheckBitOnBox(i,j,MapNumBox))
{
DrawOrEraseBoxOnMap(i+MapXBox,MapYBox+j,DrawOrErase);
}
}
}
}
unsigned char CheckBitOnMap(unsigned char x,unsigned char y)
{
unsigned char temp,temp1;
if((y + 1) % 8 == 0)
{
temp = (y + 1) / 8;
}
else
{
temp = ((y + 1) / 8) + 1;
}
temp1 = y % 8;
return (mapArray[x][temp - 1] & table2[temp1]);
}
unsigned char CheckBitOnBox(unsigned char x,unsigned char y,unsigned char seclete)
{
return (boxesArray[seclete][RotatePosition][x] & table2[y]);
}
unsigned char GetBottom(void)//底部触碰检查
{
unsigned char i,x,y,temp1,temp2;
for(y=0;y<4;y++)
{
i = 3 - y;
for(x=0;x<4;x++)
{
temp1 = CheckBitOnMap(MapXBox+x,MapYBox+i);
temp2 = CheckBitOnBox(x,i,MapNumBox);
if(temp1&&temp2)
{
return 1;//有冲突
}
}
}
return 0;//没冲突
}
unsigned char GetRightOrLeft(unsigned char where)//左右触碰检查,where=0:左,where=1:右
{
unsigned char i,x,y,temp1,temp2;
for(x=0;x<4;x++)
{
if(where)
{
i = 3 - x;
}
else
{
i = x;
}
for(y=0;y<4;y++)
{
temp1 = CheckBitOnMap(MapXBox+i,MapYBox+y);
temp2 = CheckBitOnBox(i,y,MapNumBox);
if(temp1&&temp2)
{
return 1;//有冲突
}
}
}
return 0;
}
void attachNewBox(void)
{
attachOrEraseBox(1);
MapNumBox = MapNumBoxNext;
MapNumBoxNext ++;
if(MapNumBoxNext == 7)
{
MapNumBoxNext = 0;
}
playBoxOnView();//显示预览
MapXBox = 7;
MapYBox = 0;
RotatePosition = 0;
}
void playBoxOnView(void)
{
unsigned char i,j;
ClearArea(65,12,12,12);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(CheckBitOnBox(i,j,MapNumBoxNext))
{
DrawGraphics(box1,3,3,(i*3+65),(j*3+12));
}
}
}
}
void moveDownOneLine(unsigned char temp)//输入的是把从哪行开始,在map上所有该行以上的行下落一行
{
unsigned char x,y,temp2,temp3,i;
if(temp < 8)
{
x = temp;
y = 0;
}
if(temp >7)
{
x = temp - 8;
y = 1;
}
for(i=1;i<(MapLength-1);i++)//左右边界不移
{
if(y == 1)
{
mapArray[0] = mapArray[0] << 1;
}
temp2 = mapArray[y] >> (x + 1);//先把指定行下面的保存下来
temp2 = temp2 << (x + 1);
temp3 = mapArray[y] << (8 - x);//把指定行上面的下移一个,不过要先把原来下面的数据清掉
temp3 = temp3 >> (7 - x);
mapArray[y] = temp2 | temp3;//把两个数叠加起来
}
}
void checkCompleteLine(void)
{
unsigned char x1,y1;
unsigned char num = 0;
for(y1=0;y1<(MapWeight-1);y1++)
{
num = 0;
for(x1=1;x1<(MapLength-1);x1++)
{
if(CheckBitOnMap(x1,y1) == 0)
{
num = 1;
}
}
if(num == 0)
{
moveDownOneLine(y1);
allScore++;
}
}
}
void test(void)
{
unsigned char i,j;
i = getKeyCode();
if(i == UpPress)//右
{
MapXBox++;
if(GetRightOrLeft(1) == 1)
{
MapXBox--;
}
}
if(i == DownPress)//左
{
MapXBox--;
if(GetRightOrLeft(0) == 1)
{
MapXBox++;
}
}
if(i == EnterPress)//转
{
RotatePosition++;
if(RotatePosition == 4)
{
RotatePosition = 0;
}
if((GetBottom() == 1)||(GetRightOrLeft(0) == 1)||(GetRightOrLeft(1) == 1))
{
if(RotatePosition == 0)
{
RotatePosition = 3;
}
else
{
RotatePosition--;
}
}
}
GID;
disPlayScore();
checkCompleteLine();
attachOrEraseBox(1);
disPlayMap();
ReflashDisPlay();
attachOrEraseBox(0);
GIE;
}
#pragma interrupt_handler Int_TIMER1_OVF: iv_TIMER1_OVF //???/???1 ????
void Int_TIMER1_OVF(void)
{
unsigned char temp;
times++;
if(times == TimeLevel)
{
MapYBox++;
if(GetBottom() == 1)//碰撞
{
MapYBox--;
attachNewBox();
}
attachOrEraseBox(1);
disPlayMap();
ReflashDisPlay();
attachOrEraseBox(0);
times = 0;
}
} |
|