TETRIS
为实现俄罗斯方块,我不禁会问自己以下的问题,这便是软件的思路:
first step: 如何画出游戏图案运动的范围
second step:如何画出一个10乘10的方块以及消除方法
third step: 如何画出或消除对应的19种图案以及消除方法
(有小伙伴会问是哪19种)
fourth step:如何让画出的图案动起来,并消除运动过程的残影
fifth step:如何用按键控制图案的运动,并消除运动残影与重画出新图案
sixth step: 如何用一个按键控制图案在同一位置的图形变换
seventh step:如何用数组存储当前面板上的图案位置信息。
(在这里分为明码与暗码:明码为LCD显示出的图案码,暗码为记录位置信息的数组)
eighth step: 如何判断图案的相撞。
ninth step: 如何消除组合完整一行。
tenth step: 如何将消除后的一行上面的残留块儿,向下运动一行。
eleventh step:如何预测并显示下一个将出现的图案信息。
twelfth step:如何显示分数,并实时更新数据。游戏界面如何设计。
First Step:
/***************************************
//画游戏界面
***************************************/
void Tetris_Boundary(void)
{
LCD_Fill(0,0,240,10,BROWN);
LCD_Fill(0,0,10,310,BROWN);
LCD_Fill(240-10,0,240,310,BROWN);
LCD_Fill(0,310-10,240,310,BROWN);
}
Second Step:
/**************************************
//画10*10的像素方块
*************************************/
void DrawBlock(u16 x,u16 y)
{
u8 i,n;
for(i=1;i<=8;i++)
{
for(n=1;n<=8;n++)
{
GUI_DrawPoint((x+i),(y-n),GRAY);
}
}
for(i=0;i<=9;i++)
{
GUI_DrawPoint((x+i),y,BLACK );
GUI_DrawPoint((x+i),(y-9),BLACK );
GUI_DrawPoint(x,(y-i),BLACK );
GUI_DrawPoint((x+9),(y-i),BLACK );
}
}
/*****************************************
//删除一个10*10的像素块
*****************************************/
void DeleteBlock(u16 x,u16 y)
{
u8 i,n;
for(i=0;i<=9;i++)
{
for(n=0;n<=9;n++)
{
GUI_DrawPoint((x+i),(y-n),WHITE);
}
}
}
Third Step:
/*************************************************
//画出方块组合图形
*************************************************/
void DrawShape(u16 x,u16 y,u8 what)
{
switch (what)
{
case 1:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);}break;
case 2:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+20,y);DrawBlock(x+30,y);}break;
case 3:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x,y-30);}break;
case 4:{
DrawBlock(x,y);DrawBlock(x+10,y-10);DrawBlock(x+10,y);DrawBlock(x+20,y);}break;
case 5:{
DrawBlock(x,y);DrawBlock(x+10,y-10);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
case 6:{
DrawBlock(x,y);DrawBlock(x-10,y-10);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
case 7:{
DrawBlock(x-10,y-10);DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);}break;
case 8:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x+10,y);}break;
case 9:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);DrawBlock(x+20,y-10);}break;
case 10:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x-10,y-20);}break;
case 11:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+20,y);DrawBlock(x+20,y-10);}break;
case 12:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+10,y-10);DrawBlock(x+10,y-20);}break;
case 13:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x,y-20);}break;
case 14:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x,y-20);DrawBlock(x+10,y-20);}break;
case 15:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y);DrawBlock(x+20,y);}break;
case 16:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x+10,y-10);DrawBlock(x+10,y-20);}break;
case 17:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x,y-10);DrawBlock(x-10,y-10);}break;
case 18:{
DrawBlock(x,y);DrawBlock(x,y-10);DrawBlock(x-10,y-10);DrawBlock(x-10,y-20);}break;
case 19:{
DrawBlock(x,y);DrawBlock(x+10,y);DrawBlock(x+10,y-10);DrawBlock(x+20,y-10);}break;
}
}
/*************************************************
//消除方块组合图形
*************************************************/
void DeleteShape(u16 x,u16 y,u8 what)
{
switch (what)
{
case 1: {
DeleteBlock(x,y);DeleteBlock(x+10,y) ;DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);}break;
case 2: {
DeleteBlock(x,y);DeleteBlock(x+10,y) ;DeleteBlock(x+20,y);DeleteBlock(x+30,y);}break;
case 3: {
DeleteBlock(x,y);DeleteBlock(x,y-10) ;DeleteBlock(x,y-20);DeleteBlock(x,y-30);}break;
case 4: {
DeleteBlock(x,y);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y);DeleteBlock(x+20,y);}break;
case 5: {
DeleteBlock(x,y);DeleteBlock(x+10,y-10);DeleteBlock(x,y-10);DeleteBlock(x,y-20);}break;
case 6: {
DeleteBlock(x,y);DeleteBlock(x-10,y-10);DeleteBlock(x,y-10);DeleteBlock(x,y-20);}break;
case 7: {
DeleteBlock(x-10,y-10);DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);}break;
case 8: {
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20) ;DeleteBlock(x+10,y);}break;
case 9: {
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);DeleteBlock(x+20,y-10);}break;
case 10:{
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20) ;DeleteBlock(x-10,y-20);}break;
case 11:{
DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+20,y) ;DeleteBlock(x+20,y-10);}break;
case 12:{
DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y-20);}break;
case 13:{
DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x,y-10) ;DeleteBlock(x,y-20);}break;
case 14:{
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x,y-20) ;DeleteBlock(x+10,y-20);}break;
case 15:{
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y) ;DeleteBlock(x+20,y);}break;
case 16:{
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x+10,y-10);DeleteBlock(x+10,y-20);}break;
case 17:{
DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x,y-10) ;DeleteBlock(x-10,y-10);}break;
case 18:{
DeleteBlock(x,y);DeleteBlock(x,y-10);DeleteBlock(x-10,y-10);DeleteBlock(x-10,y-20);}break;
case 19:{
DeleteBlock(x,y);DeleteBlock(x+10,y);DeleteBlock(x+10,y-10);DeleteBlock(x+20,y-10);}break;
}
}
fourth step:
/*************************************
按键控制图形运动
*************************************/
void Tetris_Game_Control(void)
{
TETRIS_KEY_RECEIVER = Menu_Control_Read2();
switch(TETRIS_KEY_RECEIVER)
{
case 2:
{
delay_ms(200);
TransFormer(TETRIS_X_POSITION,TETRIS_Y_POSITION);
Usart1_Send(BeCode[0],768);
}break;
case 10:
{
delay_ms(200);
DeleteShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
DeleteShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
TETRIS_Y_POSITION += 10;
DrawShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
DrawShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
Usart1_Send(BeCode[0],768);
}break;
case 5:
{
delay_ms(200);
DeleteShape(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
DeleteShapeState(TETRIS_X_POSITION,TETRIS_Y_POSITION,RandomNumber);
switch(RandomNumber)
{
case 1:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
case 2:if(BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)TETRIS_X_POSITION -= 10;break;
case 3:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-20)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-30)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
case 4:if(BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)TETRIS_X_POSITION -= 10;break;
case 5:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-20)/10][(TETRIS_X_POSITION-10)/10]!=1))TETRIS_X_POSITION -= 10;break;
case 6:if((BeCode[TETRIS_Y_POSITION/10][(TETRIS_X_POSITION-10)/10]!=1)||(BeCode[(TETRIS_Y_POSITION-10)/10][(TETRIS_X_POSITION-20)/10]!=1))TETRIS_X_POSITION