int MoveBlock(int Direction) { switch(Direction) { case LEFT: if(DetectCollision(LEFT)) return 1; DisplayBlock(--BlockX, BlockY); break; case RIGHT: if(DetectCollision(RIGHT)) return 1; DisplayBlock(++BlockX, BlockY); break; case DOWN: if(DetectCollision(DOWN)) { GetNextBlock(); //Generate new block on screen return 1; } DisplayBlock(BlockX, ++BlockY); break; case REFRESH: DisplayBlock(BlockX, BlockY); break; } return 0; }
void PageDraw(HPAGE PageHandle,HWND Window,int DrawLeft, int DrawTop,int DrawRight,int DrawBottom) { int SaveColor; struct viewporttype TmpViewPort; #ifdef __TURBOC__ struct linesettingstype SaveLineStyle; #else unsigned short old_style; #endif ORDINATETYPE BoxDrawLeft,BoxDrawTop,BoxDrawRight,BoxDrawBottom; ORDINATETYPE BoxXY[2*MAXPOLYGONNUMBER]; HBOX HBox; Pages *MidPage; int Left,Top,Right,Bottom; int PageLeft,PageTop,PageRight,PageBottom; WindowGetRect(Window,&Left,&Top,&Right,&Bottom); if (!RectangleIsInRectangle(DrawLeft,DrawTop,DrawRight,DrawBottom, Left,Top,Right,Bottom)) return; if (DrawRight>=Right-Left) DrawRight=Right-Left-1; if (DrawBottom>=Bottom-Top) DrawBottom=Bottom-Top-1; if (DrawLeft<0) DrawLeft=0; if (DrawTop<0) DrawTop=0; BoxDrawLeft=WindowXToUserX(DrawLeft); BoxDrawTop=WindowYToUserY(DrawTop); BoxDrawRight=WindowXToUserX(DrawRight); BoxDrawBottom=WindowYToUserY(DrawBottom); MidPage=HandleLock(ItemGetHandle(PageHandle)); if (MidPage==NULL) return; PageLeft=UserXToWindowX(0); PageTop=UserYToWindowY(0); PageRight=UserXToWindowX(PageGetPageWidth(MidPage)); PageBottom=UserYToWindowY(PageGetPageHeight(MidPage)); if(GlobalTextBlockStart<GlobalTextBlockEnd) // must clear now block DisplayBlock(GlobalBoxHeadHandle,GlobalTextBlockStart,GlobalTextBlockEnd); SaveColor=getcolor(); MouseHidden(); TextCursorOff(); getviewsettings(&TmpViewPort); setviewport(Left,Top,Right,Bottom,1); /*---- clear page -----*/ setfillstyle(1,EGA_WHITE); bar(DrawLeft,DrawTop,DrawRight,DrawBottom); /*--- draw page border ---*/ setcolor(PAGEBORDERCOLOR); rectangle(PageLeft,PageTop,PageRight,PageBottom); if(!IsInGlobalBrowser()) DrawPageFootHead(PageHandle,TRUE,TRUE); if(!fEditor) { #ifdef __TURBOC__ getlinesettings(&SaveLineStyle); setlinestyle(4,0x5555,1); #else old_style=getlinestyle(); setlinestyle(0xffff); #endif /*--- draw page shadow ---*/ setcolor(EGA_DARKGRAY); line(((PageLeft>0)?PageLeft:0)+3,PageBottom+1,PageRight+3,PageBottom+1); line(((PageLeft>0)?PageLeft:0)+3,PageBottom+2,PageRight+3,PageBottom+2); line(((PageLeft>0)?PageLeft:0)+3,PageBottom+3,PageRight+3,PageBottom+3); line(PageRight+1,((PageTop>0)?PageTop:0)+3,PageRight+1,PageBottom+3); line(PageRight+2,((PageTop>0)?PageTop:0)+3,PageRight+2,PageBottom+3); line(PageRight+3,((PageTop>0)?PageTop:0)+3,PageRight+3,PageBottom+3); /*--- draw box align line ---*/ setcolor(EGA_LIGHTBLUE); /*------------ use 0x5555 -- #ifdef __TURBOC__ setlinestyle(4,0x5f5f,1); #else setlinestyle(0x5f5f); #endif ----------------*/ setlinestyle(0x8888); PageLeft=UserXToWindowX(PageGetMarginLeft(MidPage))-1; PageTop=UserYToWindowY(PageGetMarginTop(MidPage))-1; PageRight=UserXToWindowX(PageGetPageWidth(MidPage)- PageGetMarginRight(MidPage))+1; PageBottom=UserYToWindowY(PageGetPageHeight(MidPage)- PageGetMarginBottom(MidPage))+1; line(UserXToWindowX(0),PageTop, UserXToWindowX(PageGetPageWidth(MidPage)),PageTop); line(UserXToWindowX(0),PageBottom, UserXToWindowX(PageGetPageWidth(MidPage)),PageBottom); line(PageLeft,UserYToWindowY(0),PageLeft, UserYToWindowY(PageGetPageHeight(MidPage))); line(PageRight,UserYToWindowY(0),PageRight, UserYToWindowY(PageGetPageHeight(MidPage))); #ifdef __TURBOC__ setlinestyle(SaveLineStyle.linestyle,SaveLineStyle.upattern, SaveLineStyle.thickness); #else setlinestyle(old_style); #endif } HandleUnlock(ItemGetHandle(PageHandle)); setcolor(EGA_BLACK); HBox=PageGetBoxHead(PageHandle); while (HBox) { ORDINATETYPE BoxLeft,BoxTop,BoxRight,BoxBottom; TextBoxs *MidBox; int BoxDots; MidBox=HandleLock(ItemGetHandle(HBox)); if (MidBox==NULL) break; BoxGetPolygonDrawBorder((Boxs *)MidBox,&BoxDots,BoxXY); PolygonGetMinRectangle(BoxDots,BoxXY,&BoxLeft,&BoxTop,&BoxRight,&BoxBottom); if (RectangleIsInRectangle(BoxDrawLeft,BoxDrawTop,BoxDrawRight,BoxDrawBottom, BoxLeft,BoxTop,BoxRight,BoxBottom)||TextBoxGetBoxType(MidBox)==TEXTBOX) { HBOX LinkBox; BoxDraw(HBox, (HBox==GlobalBoxHeadHandle)?DRAWBORDERWITHRECATNGLE: DRAWVIRTUALBORDOR); if (TextBoxGetBoxType(MidBox)==TEXTBOX && (GlobalBoxTool==IDX_UNLINK||GlobalBoxTool==IDX_LINK) && (LinkBox=TextBoxGetPrevLinkBox(MidBox))!=0 ) { ORDINATETYPE Left2,Top2,Right2,Bottom2; BoxGetRect(HBox,&BoxLeft,&BoxTop,&BoxRight,&BoxBottom); BoxXY[0]=BoxLeft; BoxXY[1]=BoxTop; BoxPolygonRotate(1,BoxXY,(PictureBoxs *)MidBox); BoxPolygonToWindowXY(1,BoxXY); BoxLeft=BoxXY[0]; BoxTop=BoxXY[1]; if(ItemGetFather(LinkBox)==PageHandle) { BoxGetRect(LinkBox,&Left2,&Top2,&Right2,&Bottom2); BoxXY[0]=Right2; BoxXY[1]=Bottom2; BoxPolygonRotate(1,BoxXY,HandleLock(ItemGetHandle(LinkBox))); HandleUnlock(ItemGetHandle(LinkBox)); BoxPolygonToWindowXY(1,BoxXY); } else { BoxXY[0]=getmaxx(); BoxXY[1]=0; // screen (Right,Top) } line(BoxLeft,BoxTop,BoxXY[0],BoxXY[1]); } } HandleUnlock(ItemGetHandle(HBox)); HBox=ItemGetNext(HBox); } if (GlobalGroupGetSign()) GroupDrawAllBorder(DRAWXORBORDER); setviewport(TmpViewPort.left,TmpViewPort.top,TmpViewPort.right, TmpViewPort.bottom,TmpViewPort.clip); setcolor(SaveColor); MouseShow(); return; }
int main(){ int gd=DETECT, gm; int Return=0; char Key, ScanCode; int Counter=0; //Divide total delay & take multiple input initgraph(&gd, &gm,"c:\\tc\\bgi"); //initialize graphics mode randomize(); //Randomize block's shapes & color cleardevice(); //clear screen InitPalette(); //for setting color pallete InitMatrix(); //Initialize Matrix GetImages(); //Saving the images StartScreen(); //for start screen cleardevice(); //clear screen AssignShape(GetRandomShape(), GetRandomColor()); //for the falling block NextShape=GetRandomShape(); NextColor=GetRandomColor(); DisplayScreen(); //Show main screen DisplayNextShape(); //show next brick MoveBlock(LEFT); //keep the block on center & check game over while(kbhit()) getch(); //empty the keyboard input while (!Quit && !GameOver) { //Moving the blocks down if(++Counter >= Speed) //For controling the speed { Counter=0; MoveBlock(DOWN); SoundDrop(); } if(kbhit()) //For the arrow keys { Key = getch(); if(Key == 0) { ScanCode = getch(); if(ScanCode == KEY_UP) RotateBlock(); else if(ScanCode == KEY_LEFT) MoveBlock(LEFT); else if(ScanCode == KEY_RIGHT) MoveBlock(RIGHT); else if(ScanCode == KEY_DOWN) { Score++; //increase score PrintScore(); MoveBlock(DOWN); } if(!Return) SoundDrop(); Return = 0; } else if(Key == KEY_ENTER || Key == KEY_SPACE) //Rotating bricks RotateBlock(); else if(Key == 'P' || Key == 'p') //For pause { MessageBox(" Paused"); while(kbhit()) getch(); //clear the keyboard input for(int x=0; x<COLS; x++) for(int y=0; y<ROWS; y++) PreviousScreenLayout[x][y] -= 1; //Clear the present screen layout to refresh the whole screen UpdateScreen(); //refresh screen } else if(Key == KEY_ESC) //For quit { char ret = MessageBox("Are you sure, you want to Quit?", 563, 2); if(ret == 'y' || ret == 'Y' || ret == KEY_ENTER) { Quit = 1; break; } cleardevice(); //Clear the message box while(kbhit()) getch(); //Clear the keyboard input for(int x=0; x<COLS; x++) for(int y=0; y<ROWS; y++) PreviousScreenLayout[x][y] -= 1; // Clear the present screen layout to refresh the whole screen UpdateScreen(); //refresh screen DisplayScreen(); //show the main screen again DisplayNextShape(); //show next brick box } else if(Key == 's' || Key == 'S') //For sound on/off { SoundOn = !SoundOn; } else if(Key=='a' || Key=='A') //For author { MessageBox("Author: Aguntuk Group",450); cleardevice(); //Clear the message box while(kbhit()) getch(); //Clear the keyboard input for(int x=0;x<COLS;x++) for(int y=0;y<ROWS;y++) PreviousScreenLayout[x][y] -=1; //Clear the present screen layout to refresh the whole screen UpdateScreen(); //refresh screen DisplayScreen(); //show the main screen again DisplayNextShape(); //show next brick box } } delay(6); //For moving down the blocks slowly } if(GameOver) //For game over option { DisplayBlock(6,0); //For display the top most brick ShowGameOver(); //For display game over message box } restorecrtmode(); //For closing graphicg mode return 0; }