#include"RussiaBall.h"
void FlushQ ()
{
UGL_MSG msg;
UGL_STATUS status;
do
{
status = uglInputMsgGet (inputServiceId, &msg, UGL_NO_WAIT);
} while (status != UGL_STATUS_Q_EMPTY);
}
/**
功能:退出程序时清理内存空间
*/
void CleanUp()
{
UGL_FREE (SPEED);
UGL_FREE (ScreenArray);
UGL_FREE (colorTable);
UGL_FREE (Block);
uglFontDestroy (fontSystem);
uglGcDestroy (gc);
uglDeinitialize();
}
/**
功能:图形变换
Param:
int blockID 待变换基本图样类型ID 取值范围:0--27
Retval: 变换后的基本图样类型ID 取值范围:0--27
*/
int ChangeShape(int blockID)
{
int newBlockID;
if(blockID%4==0)
newBlockID=blockID+3;
else
newBlockID=blockID-1;
return newBlockID;
}
/**
功能:将Block所在单位块加到ScreenArray数组中
Param:
int row 单元格横坐标 取值范围:0--27
int column 单元格纵坐标 取值范围:0--19
int blockID 基本图样类型 取值范围:0--27
Retval: NULL
*/
void UpdateArray(int row,int column,int blockID)
{
int i,Position[4][2]={0};
for(i=0;i<4;i++)
{
Position[i][0]=row+Block[blockID][i][0];
Position[i][1]=column+Block[blockID][i][1];
ScreenArray[Position[i][0]][Position[i][1]]=blockID/4+1;
}
}
/*
初始化游戏,形成方块逐行向上递增,然后逐行向下递减的效果
*/
void InitGameShow()
{
int i,j,k;
/*k=rand()%7+1;*//*建议刷屏颜色采用单色,如反对则打开此行及下面第五行的屏蔽*/
for(i=0;i<28;i++)
{
for(j=0;j<20;j++)
ScreenArray[i][j]=LIGHTGREEN;/*k;*/
RefreshScreen();
uglOSTaskDelay(10);
}
for(i=Row+3;i>=0;i--)
{
for(j=0;j<20;j++)
ScreenArray[i][j]=0;
RefreshScreen();
uglOSTaskDelay(10);
}
}
/*=============复位游戏设置============*/
void ResetData()
{
russia.Nextscore = 500; /*设置升级初始值=============*/
russia.Speed = 0;
russia.Level = 0;
russia.Score = 0;
russia.dTime = SPEED[0];
}
void GameSet()/*===========游戏设置==========*/
{
UGL_MSG msg;
UGL_STATUS status;
char message_set[20]="Game Setting !";
russia.ResetFlag=1;
ShowMessage(message_set,displayWidth/2-50,displayHeight/2-10);
while(1)
{
uglOSTaskDelay(3);
status = uglInputMsgGet (inputServiceId, &msg, UGL_NO_WAIT);
if(status == UGL_STATUS_Q_EMPTY||msg.type != MSG_KEYBOARD)
continue;
if (msg.data.keyboard.modifiers & UGL_KBD_KEYDOWN)
switch(msg.data.keyboard.key)
{
case UGL_UNI_LEFT_ARROW:
russia.Speed --;
if (russia.Speed < 0)
russia.Speed = 10;
RefreshCtrlPad();
break;
case UGL_UNI_RIGHT_ARROW:
russia.Speed ++;
if (russia.Speed > 10)
russia.Speed = 0;
RefreshCtrlPad();
break;
case UGL_UNI_DOWN_ARROW:
russia.Level --;
if (russia.Level< 0)
russia.Level = 15;
RefreshCtrlPad();
break;
case UGL_UNI_UP_ARROW:
russia.Level ++;
if (russia.Level > 15)
russia.Level = 0;
RefreshCtrlPad();
break;
case UGL_UNI_CARRIAGE_RETURN:
ClearScreen(displayWidth/2-50,displayWidth/2+50,displayHeight/2-10,displayHeight/2+30);
RefreshCtrlPad();
return ;
default:
break;
}
}
}
/**
功能:检测并执行消行
*/
void RemoveRow()
{
int i,j;
int row,column;
int removeflag=1; /*行消除标志,默认为可以消除*/
int key=0;
int temprow[4]={-1};
for(i=0;i<Row;i++)
{
removeflag=1; /*初始化行标志为可以消除*/
for(j=0;j<Column;j++)
{
if(ScreenArray[i][j]==0)/*此行有一个方块没有被填充*/
{
removeflag=0;
break;
}
}
if(removeflag) /*所有方块被填充,需要消除此行*/
{
temprow[key]=i; /*记录当前要消的行*/
key++;
}
}
russia.Lines=key;/*===================*/
if(key<=0||key>4)
{
ClearScreen(GameRight+4,GameTop+366,GameRight+179,GameTop+366+195);
return;
}
RowColorChange(temprow,key);
for(i=key-1;i>=0 ; i--) /*每次消除行标最大的行*/
{
for(row=temprow[i];row<Row-1;row++) /*需要消除的行以上的行均下移一行*/
for(column=0;column<Column;column++)
ScreenArray[row][column]=ScreenArray[row+1][column];
for(column=0;column<Column;column++)
ScreenArray[Row-1][column]=0;
}
RefreshScreen();
ShowEncourage(russia.Lines,russia.Level);
ScoreGet();/*===================*/
}
/*初始化屏幕*/
void InitScreen()
{
static UGL_CHAR * msg_change = " W/w : Change Shape";
static UGL_CHAR * msg_down = " S/s : Move Down";
static UGL_CHAR * msg_left = " A/a : Move Left";
static UGL_CHAR * msg_right = " D/d : Move Right";
static UGL_CHAR * msg_fastdown = " L/l : fast down";
static UGL_CHAR * msg_set = " R/r : Game Reset";
static UGL_CHAR * msg_quit = " Esc : Exit Game";
static UGL_CHAR * msg_enter = " Enter : set level/speed";
static UGL_CHAR * msg_space = " Space : Strat/Pause";
static UGL_CHAR * msg_increaselevel = " ^ : increase level";
static UGL_CHAR * msg_decreaselevel = " v : decrease level";
static UGL_CHAR * msg_speeddown = " < : speed down";
static UGL_CHAR * msg_speedup = " > : speed up";
static UGL_CHAR * msg_next = "[NEXT]";
int textWidth, textHeight;
int top,bottom; /*游戏区的上下边界*/
int left_left,left_right; /*左侧方框的左右边界*/
int mid_left,mid_right; /*中间方框的左右边界*/
int right_left,right_right;/*右侧方框的左右边界*/
char info[40]; /*==为存放要显示的文本定义的数组===*/
uglBackgroundColorSet(gc, colorTable [0].uglColor);
if (inputServiceId != UGL_NULL)
{
/*画游戏界面边框*/
uglBatchStart(gc);
/*左侧边框*/
left_left = GameLeft-180;
left_right = GameLeft-4;
top = GameTop;
bottom = GameBottom;
if(left_left<0)
left_left=0;
uglForegroundColorSet(gc, colorTable[RED].uglColor);
uglLineWidthSet(gc, 2);
uglRectangle(gc, left_left, top , left_right, bottom);
/*游戏区边框*/
mid_left = GameLeft-2;
mid_right = GameRight;
uglForegroundColorSet(gc, colorTable[WHITE].uglColor);
uglLineWidthSet(gc, 2);
uglRectangle(gc, mid_left, top , mid_right, bottom);
/*右侧边框*/
right_left = GameRight+2;
right_right = GameRight + 180;
if(right_right>displayWidth-1)
{
right_right=displayWidth-1;
}
uglForegroundColorSet(gc, colorTable[RED].uglColor);
uglLineWidthSet(gc, 2);
uglRectangle(gc, right_left, top , right_right, bottom);
uglBatchEnd(gc);
/* A S D W 键的屏幕提示 */
/* A S D W 图示说明 */
uglForegroundColorSet(gc, colorTable[BLACK].uglColor);
uglBackgroundColorSet(gc, colorTable[WHITE].uglColor);
uglRectangle(gc,left_left+75, top+50 , left_left+105, top+80);
uglBackgroundColorSet(gc, colorTable[WHITE].uglColor);
uglRectangle(gc,left_left+45, top+80 , left_left+75, top+110);
uglBackgroundColorSet(gc, colorTable[WHITE].uglColor);
uglRectangle(gc,left_left+75, top+80 , left_left+105, top+110);
uglBackgroundColorSet(gc, colorTable[WHITE].uglColor);
uglRectangle(gc,left_left+105, top+80 , left_left+135, top+110);
/*ASDW 文字说明*/
uglBackgroundColorSet(gc, colorTable[0].uglColor); /*sets a graphics context’s background color*/
uglForegroundColorSet(gc, colorTable[WHITE].uglColor);/*sets a graphics context’s foreground color*/
uglFontSet(gc, fontSystem);
uglTextSizeGet(fontSystem, &textWidth, &textHeight, -1, msg_change);
uglTextDraw(gc,left_left+6, top+125, -1, msg_change);
uglTextSizeGet(fontSystem, &textWidth, &textHeight, -1, msg_down);
uglTextDraw(gc, left_left+6, top+textHeight+125, -1, msg_down);
uglTextSizeGet(fontSystem, &textWidth, &textHeight, -1, msg_left);
uglTextDraw(gc, left_left+6, top+textHeight*2+125, -1, msg_left);
uglTextSizeGet(fontSystem, &textWidth, &textHeight, -1, msg_right);
uglTextDraw(gc, left_left+6, top+textHeight*3+125, -1,