📄 minigui-games-programming.txt
字号:
}
}
void OnClose(HWND hWnd)
{
if(!hWnd) return;
FreePlayingWindow(hWnd);
//saving the high score,keys arrange...
DestroyMainWindow (hWnd);
PostQuitMessage (hWnd);
}
void DrawPlayingWindow(HDC hDC,POINT poStart,\
int iSideLength,PlayingWindow * pPlayingWindow)
{
RECT rect;
char strText[50];
int i,j;
MovingSquare *pMovingSquare;
BOOL bMeetBrick;
POINT poStart1;
assert(hDC);
assert(pPlayingWindow);
//syslog(LOG_INFO,"DPW():iSideLength:%d",iSideLength);
rect.left = poStart.x -4;
rect.top = poStart.y -4;
rect.right = poStart.x +iSideLength*RUSSIA_SQUARE_WIDTH+4;
rect.bottom = poStart.y + iSideLength*
(RUSSIA_SQUARE_HEIGHT+4)+RUSSIA_SQUARE_GRID+4;
//syslog(LOG_INFO,"DPW:rect:%d:%d:%d:%d",rect.left,rect.top,rect.right,rect.bottom);
//Rectangle(hDC,rect.left,rect.top,rect.right,rect.bottom);
Draw3DBorder(hDC,rect.left,rect.top,rect.right,rect.bottom);
rect.left += 2;
rect.top += 2;
rect.right -= 2;
rect.bottom -= 2;
//Rectangle(hDC,rect.left,rect.top,rect.right,rect.bottom);
Draw3DBorder(hDC,rect.left,rect.top,rect.right,rect.bottom);
snprintf(strText,50,"Score:%d",pPlayingWindow->iScore);
TextOut(hDC,poStart.x+2,poStart.y+2,strText);
if(!pPlayingWindow->bBegin){
TextOut(hDC,poStart.x+2,poStart.y+22,"Push start key...");
return;
}else if (pPlayingWindow->bPause){
TextOut(hDC,poStart.x+2,poStart.y+22,"Paused...");
return;
}else if(pPlayingWindow->bDead){
TextOut(hDC,poStart.x+2,poStart.y+22,"Die...");
}
poStart.y += RUSSIA_SQUARE_GRID;
//first draw the static bricks,then draw the moving square.
pMovingSquare = pPlayingWindow->pPreCreatedSquare;
if(pMovingSquare){
poStart1.x = poStart.x+(6+ pMovingSquare->poAll[0].x)*iSideLength;
poStart1.y = poStart.y+(0+ pMovingSquare->poAll[0].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(6+ pMovingSquare->poAll[1].x)*iSideLength;
poStart1.y = poStart.y+(0+ pMovingSquare->poAll[1].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(6+ pMovingSquare->poAll[2].x)*iSideLength;
poStart1.y = poStart.y+(0+ pMovingSquare->poAll[2].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(6+ pMovingSquare->poAll[3].x)*iSideLength;
poStart1.y = poStart.y+(0+ pMovingSquare->poAll[3].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
}
poStart.y += 4*iSideLength;
//syslog(LOG_INFO,"DPW:%d:%d",poStart.x,poStart.y);
for(i=RUSSIA_SQUARE_HEIGHT-1;i>=0;i--)
{
//syslog(LOG_INFO,"DPW:Drawing Static Matrix");
bMeetBrick = FALSE;
for(j=0;j<RUSSIA_SQUARE_WIDTH;j++)
if(pPlayingWindow->iStateMatrix[i][j])
{
poStart1.x = poStart.x+j*iSideLength;
poStart1.y = poStart.y+i*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,\
pPlayingWindow->iStateMatrix[i][j]);
bMeetBrick = TRUE;
}
}
pMovingSquare = pPlayingWindow->pMovingSquare;
if(pMovingSquare){
//syslog(LOG_INFO,"DPW:Drawing Moving Square!");
//syslog(LOG_INFO,"DPW:poStart.x:%d",poStart.x);
//syslog(LOG_INFO,"DPW:poStart.y:%d",poStart.y);
//syslog(LOG_INFO,"DPW:pMovingSquare->poOrigin.x:%d",pMovingSquare->poOrigin.x);
//syslog(LOG_INFO,"DPW:pMovingSquare->poOrigin.y:%d",pMovingSquare->poOrigin.y);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[0].x:%d",pMovingSquare->poAll[0].x);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[0].y:%d",pMovingSquare->poAll[0].y);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[1].x:%d",pMovingSquare->poAll[1].x);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[1].y:%d",pMovingSquare->poAll[1].y);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[2].x:%d",pMovingSquare->poAll[2].x);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[2].y:%d",pMovingSquare->poAll[2].y);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[3].x:%d",pMovingSquare->poAll[3].x);
//syslog(LOG_INFO,"DPW:pMovingSquare->poAll[3].y:%d",pMovingSquare->poAll[3].y);
poStart1.x = poStart.x+(pMovingSquare->poOrigin.x+
pMovingSquare->poAll[0].x)*iSideLength;
poStart1.y = poStart.y+(pMovingSquare->poOrigin.y+
pMovingSquare->poAll[0].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(pMovingSquare->poOrigin.x+
pMovingSquare->poAll[1].x)*iSideLength;
poStart1.y = poStart.y+(pMovingSquare->poOrigin.y+
pMovingSquare->poAll[1].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(pMovingSquare->poOrigin.x+
pMovingSquare->poAll[2].x)*iSideLength;
poStart1.y = poStart.y+(pMovingSquare->poOrigin.y+
pMovingSquare->poAll[2].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
poStart1.x = poStart.x+(pMovingSquare->poOrigin.x+
pMovingSquare->poAll[3].x)*iSideLength;
poStart1.y = poStart.y+(pMovingSquare->poOrigin.y+
pMovingSquare->poAll[3].y)*iSideLength;
DrawLittleBrick(hDC,poStart1,iSideLength,pMovingSquare->iType);
}
}
void DrawLittleBrick(HDC hDC,POINT poStart,int iSideLength,int iType)
{
gal_pixel iColor;
iColor = COLOR_green;
switch(iType) {
case SQUARE_LONG:
iColor = COLOR_green;
break;
case SQUARE_SQUARE:
iColor = COLOR_lightgray;
break;
case SQUARE_HILL:
iColor = COLOR_red;
break;
case SQUARE_LEFT_DUCK:
iColor = COLOR_yellow;
break;
case SQUARE_RIGHT_DUCK:
iColor = COLOR_blue;
break;
case SQUARE_LEFT_CHAIR:
iColor = COLOR_magenta;
break;
case SQUARE_RIGHT_CHAIR:
iColor = COLOR_cyan;
break;
}
Draw3DUpFrame(hDC,poStart.x,poStart.y,poStart.x+iSideLength,
poStart.y+iSideLength,iColor);
}
BOOL OnLeftKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
assert(hWnd);
if(!pPlayingWindow) return FALSE;
if(!pPlayingWindow->bBegin) return FALSE;
if(pPlayingWindow->bPause) return FALSE;
if(pPlayingWindow->bDead) return FALSE;
if(MoveSquare(pPlayingWindow,ACTION_LEFT)){
rect1.left=pPlayingWindow->pMovingSquare->poOrigin.x;
rect1.top =pPlayingWindow->pMovingSquare->poOrigin.y;
rect1.right = rect1.left + pPlayingWindow->pMovingSquare->poRightBottom.x+2;
rect1.bottom = rect1.top + pPlayingWindow->pMovingSquare->poRightBottom.y+1;
//rect1.left = 0;
//rect1.top = 0;
//rect1.right = 0;
//rect1.bottom = 0;
//syslog(LOG_INFO,"OnLeftKeyDown:rect1:L:%d,R:%d,T:%d,B:%d",rect1.left,
//rect1.right,rect1.top,rect1.bottom);
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
}
else
return FALSE;
return TRUE;
}
BOOL OnRightKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
assert(hWnd);
if(!pPlayingWindow) return FALSE;
if(!pPlayingWindow->bBegin) return 0;
if(pPlayingWindow->bPause) return 0;
if(pPlayingWindow->bDead) return 0;
if(MoveSquare(pPlayingWindow,ACTION_RIGHT)){
rect1.left=pPlayingWindow->pMovingSquare->poOrigin.x-1;
rect1.top =pPlayingWindow->pMovingSquare->poOrigin.y;
rect1.right = rect1.left + pPlayingWindow->pMovingSquare->poRightBottom.x+2;
rect1.bottom = rect1.top + pPlayingWindow->pMovingSquare->poRightBottom.y+1;
//rect1.left = 0;
//rect1.top = 0;
//rect1.right = 0;
//rect1.bottom = 0;
//syslog(LOG_INFO,"OnRightKeyDown:rect1:L:%d,R:%d,T:%d,B:%d",rect1.left,
//rect1.right,rect1.top,rect1.bottom);
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
}
else
return FALSE;
return TRUE;
}
BOOL OnRotateKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
assert(hWnd);
if(!pPlayingWindow) return FALSE;
if(!pPlayingWindow->bBegin) return 0;
if(pPlayingWindow->bPause) return 0;
if(pPlayingWindow->bDead) return 0;
if(MoveSquare(pPlayingWindow,ACTION_ROTATE)){
rect1.left=pPlayingWindow->pMovingSquare->poOrigin.x-1;
rect1.top =pPlayingWindow->pMovingSquare->poOrigin.y-1;
rect1.right = rect1.left + 5;
rect1.bottom = rect1.top + 5;
//rect1.left = 0;
//rect1.top = 0;
//rect1.right = 0;
//rect1.bottom = 0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
}
else
return FALSE;
return TRUE;
}
BOOL OnDownKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
int iScoreBefore;
int iScoreAfter;
int iLines;
assert(hWnd);
if(!pPlayingWindow) return FALSE;
if(!pPlayingWindow->bBegin) return 0;
if(pPlayingWindow->bPause) return 0;
if(pPlayingWindow->bDead) return 0;
MoveSquare(pPlayingWindow,ACTION_DOWN);
PutSquare(pPlayingWindow);
iScoreBefore = (pPlayingWindow->iScore)%DIFFICULTY_HOP;
iLines = RemoveFullLines(pPlayingWindow,iWhich);
if(iLines){
iScoreAfter = (pPlayingWindow->iScore)%DIFFICULTY_HOP;
if(iScoreBefore > iScoreAfter){
pPlayingWindow->iLevel = (pPlayingWindow->iLevel + 1);
if(pPlayingWindow->iLevel>9)
pPlayingWindow->iLevel = 9;
if(iWhich == 1)
#ifdef _TIMER_UNIT_10MS
SetTimerSpeed(hWnd,RUSSIA_SQUARE_TIMER1,1000/TimerFreq[pPlayingWindow->iLevel]);
#else
SetTimerSpeed(hWnd,RUSSIA_SQUARE_TIMER1,TimerFreq[pPlayingWindow->iLevel]);
#endif
else
#ifdef _TIMER_UNIT_10MS
SetTimerSpeed(hWnd,RUSSIA_SQUARE_TIMER2,1000/TimerFreq[pPlayingWindow->iLevel]);
#else
SetTimerSpeed(hWnd,RUSSIA_SQUARE_TIMER2,TimerFreq[pPlayingWindow->iLevel]);
#endif
}
if(iLines>=2)
InvalidateRect(hWnd,NULL,TRUE);
}
rect1.left=0;
rect1.right=0;
rect1.top=0;
rect1.bottom=0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
return TRUE;
}
BOOL OnStartKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
int i,j;
assert(hWnd);
if(!pPlayingWindow) return FALSE;
if(pPlayingWindow->bPause) return FALSE;
if(theApp.bTease)
return 0;
//reset the struct : pMoingSquare....
if(pPlayingWindow->pMovingSquare){
free(pPlayingWindow->pMovingSquare);
pPlayingWindow->pMovingSquare = NULL;
free(pPlayingWindow->pPreCreatedSquare);
pPlayingWindow->pPreCreatedSquare = NULL;
}
for(i=0;i<RUSSIA_SQUARE_HEIGHT;i++)
for(j=0;j<RUSSIA_SQUARE_WIDTH;j++)
pPlayingWindow->iStateMatrix[i][j] = 0;
pPlayingWindow->iScore = 0;
pPlayingWindow->bBegin = TRUE;
pPlayingWindow->bDead = FALSE;
return TRUE;
}
BOOL OnPauseKeyDown(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
assert(hWnd);
if(!pPlayingWindow) return FALSE;;
if(!pPlayingWindow->bBegin) return FALSE;
if(pPlayingWindow->bDead) return FALSE;
if(theApp.bTease)
return 0;
if(pPlayingWindow->bPause)
pPlayingWindow->bPause = FALSE;
else
pPlayingWindow->bPause = TRUE;
rect1.left=0;
rect1.right=0;
rect1.top=0;
rect1.bottom=0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,!pPlayingWindow->bPause);
return TRUE;
}
void OnTimer(HWND hWnd,PlayingWindow *pPlayingWindow,int iWhich)
{
RECT rect1;
int iScoreBefore;
int iScoreAfter;
int iLines;
MovingSquare * pMovingSquare ;
assert(hWnd);
assert(pPlayingWindow);
if(!pPlayingWindow->bBegin) return;
if(pPlayingWindow->bPause) return;
if(pPlayingWindow->bDead) return;
//syslog(LOG_INFO,"OnTimer:Here I am called!");
//if no moving square,create one.
if(!pPlayingWindow->pMovingSquare){
if(!pPlayingWindow->pPreCreatedSquare){
pPlayingWindow->pPreCreatedSquare =
(MovingSquare*)calloc(1,sizeof(MovingSquare));
pMovingSquare = pPlayingWindow->pPreCreatedSquare;
srandom(time(NULL)+2+iWhich);
pMovingSquare->iType = (rand() +2+iWhich)% 7 + 1;
pMovingSquare->iDir = (rand() +2+iWhich)% 4 + 1;
pMovingSquare->poOrigin.y = 0;
pMovingSquare->poOrigin.x = (rand()+2+iWhich) % (RUSSIA_SQUARE_WIDTH-3);
MoveSquare(pPlayingWindow,ACTION_INIT);
//syslog(LOG_INFO,"OTAAA:Created A Square Type:%d Dir:%d",pMovingSquare->iType,pMovingSquare->iDir);
//syslog(LOG_INFO,"OT:pMovingSquare->poOrigin.x:%d",pMovingSquare->poOrigin.x);
//syslog(LOG_INFO,"OT:pMovingSquare->poOrigin.y:%d",pMovingSquare->poOrigin.y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[0].x:%d",pMovingSquare->poAll[0].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[0].y:%d",pMovingSquare->poAll[0].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[1].x:%d",pMovingSquare->poAll[1].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[1].y:%d",pMovingSquare->poAll[1].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[2].x:%d",pMovingSquare->poAll[2].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[2].y:%d",pMovingSquare->poAll[2].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[3].x:%d",pMovingSquare->poAll[3].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[3].y:%d",pMovingSquare->poAll[3].y);
}
pPlayingWindow->pMovingSquare = pPlayingWindow->pPreCreatedSquare;
pPlayingWindow->pPreCreatedSquare =
(MovingSquare*)calloc(1,sizeof(MovingSquare));
pMovingSquare = pPlayingWindow->pPreCreatedSquare;
srandom(time(NULL)+iWhich);
pMovingSquare->iType = (rand()+iWhich) % 7 + 1;
pMovingSquare->iDir = (rand()+iWhich) % 4 + 1;
pMovingSquare->poOrigin.y = 0;
pMovingSquare->poOrigin.x = (rand()+iWhich) % (RUSSIA_SQUARE_WIDTH-3);
MoveSquare(pPlayingWindow,ACTION_INIT);
//syslog(LOG_INFO,"OTBBB:Created A Square Type:%d",pMovingSquare->iType);
//syslog(LOG_INFO,"OT:pMovingSquare->poOrigin.x:%d",pMovingSquare->poOrigin.x);
//syslog(LOG_INFO,"OT:pMovingSquare->poOrigin.y:%d",pMovingSquare->poOrigin.y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[0].x:%d",pMovingSquare->poAll[0].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[0].y:%d",pMovingSquare->poAll[0].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[1].x:%d",pMovingSquare->poAll[1].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[1].y:%d",pMovingSquare->poAll[1].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[2].x:%d",pMovingSquare->poAll[2].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[2].y:%d",pMovingSquare->poAll[2].y);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[3].x:%d",pMovingSquare->poAll[3].x);
//syslog(LOG_INFO,"OT:pMovingSquare->poAll[3].y:%d",pMovingSquare->poAll[3].y);
//syslog(LOG_INFO,"OnTimer:Created a new square!");
//syslog(LOG_INFO,"Type:%d,Dir:%d,poOrigin:%d,%d",pMovingSquare->iType,pMovingSquare->iDir,pMovingSquare->poOrigin.x,pMovingSquare->poOrigin.y);
if(!MoveSquare(pPlayingWindow,ACTION_PUTIN)){
//No where to put the created square,die.
pPlayingWindow->bDead = TRUE;
UpdateHighScore(hWnd,pPlayingWindow);
rect1.left=0;
rect1.right=0;
rect1.top=0;
rect1.bottom=0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
return;
}
if(WillNotTouch(pPlayingWindow)){
//A successful initialize.
//rect1.left=pMovingSquare->poOrigin.x;
//rect1.right=rect1.left+pMovingSquare->poRightBottom.x;
//rect1.top=pMovingSquare->poOrigin.y;
//rect1.bottom=rect1.top+pMovingSquare->poRightBottom.y;
rect1.left = 0;
rect1.right = 0;
rect1.top = 0;
rect1.bottom = 0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
return;
}
//Touch the bottom!! put it in the square.
PutSquare(pPlayingWindow);
//Can remove at least one line,live.....
if(RemoveFullLines(pPlayingWindow,iWhich)>0){
rect1.left=0;
rect1.right=0;
rect1.top=0;
rect1.bottom=0;
ConvertRectCord(hWnd,iWhich,&rect1);
InvalidateRect(hWnd,&rect1,TRUE);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -