Guest User

C++

a guest
Dec 5th, 2011
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.10 KB | None | 0 0
  1. //FALLING BLOCK GAME!
  2.  
  3. //main.cpp
  4.  
  5. //tell compiler to not include many unneeded header files.
  6. #include <string>
  7.  
  8. #define WIN32_LEAN_AND_MEAN
  9.  
  10. //need this for windows stuff.
  11.  
  12. #include <windows.h>
  13.  
  14. //need this for srand and rand
  15.  
  16. #include <stdlib.h>
  17.  
  18. //now let's include our bitmapobject definitions
  19.  
  20. #include "bitmapobject.h"
  21.  
  22. //let's give our window a name
  23.  
  24. #define WINDOWCLASS "FallingBlockGame"
  25.  
  26. //let's give our window a title...er caption.
  27.  
  28. #define WINDOWTITLE "BloX - A Falling Block Game!"
  29.  
  30. //since we're using square blocks, let's only use a single size.
  31. using namespace std;
  32. string convertInt(int number)
  33. {
  34.     if (number == 0)
  35.         return "0";
  36.     string temp="";
  37.     string returnvalue="";
  38.     while (number>0)
  39.     {
  40.         temp+=number%10+48;
  41.         number/=10;
  42.     }
  43.     for (int i=0;i<temp.length();i++)
  44.         returnvalue+=temp[temp.length()-i-1];
  45.     return returnvalue;
  46. }
  47. const int TILESIZE=16;
  48.  
  49. //now for the map...
  50.  
  51. const int MAPWIDTH=13;
  52.  
  53. const int MAPHEIGHT=30;
  54.  
  55. const int GREY=8;
  56.  
  57. const int TILENODRAW=-1;
  58.  
  59. const int TILEBLACK=0;
  60.  
  61. const int TILEGREY=1;
  62.  
  63. const int TILEBLUE=2;
  64.  
  65. const int TILERED=3;
  66.  
  67. const int TILEGREEN=4;
  68.  
  69. const int TILEYELLOW=5;
  70.  
  71. const int TILEWHITE=6;
  72.  
  73. const int TILESTEEL=7;
  74.  
  75. const int TILEPURPLE=8;
  76.  
  77. bool GameInit(); // game initialization function
  78.  
  79. void GameLoop(); //where the game actually takes place
  80.  
  81. void GameDone(); //clean up!
  82.  
  83. void DrawTile(int x, int y, int tile); //coordinates & tile type
  84.  
  85. void DrawMap(); //draw the whole map.. render function, basically
  86.  
  87. void NewBlock(); //create a new block!
  88.  
  89. void RotateBlock(); //rotate a block.. if you can!
  90.  
  91. void Move(int x, int y); //coordinates to move.
  92.  
  93. int CollisionTest(int nx, int ny); //test collision of blocks
  94.  
  95. void RemoveRow(int row); //remove a row.. that would be the 'x'.
  96.  
  97. void NewGame(); //make a new game!
  98.  
  99. HINSTANCE hInstMain=NULL; //main app handle
  100.  
  101. HWND hWndMain=NULL; //main window handle
  102.  
  103. int Map[MAPWIDTH][MAPHEIGHT+1]; //the game map!
  104.  
  105. struct Piece {
  106.  
  107.     int size[4][4];
  108.  
  109.     int x;
  110.  
  111.     int y;
  112.  
  113. };
  114.  
  115.  
  116.  
  117. Piece sPrePiece; //preview piece.
  118.  
  119. Piece sPiece; //the 's' prefixes indicate this is a 'structure'
  120.  
  121. DWORD start_time;  //used in timing
  122.  
  123. bool GAMESTARTED=false; //used by NewBlock()
  124.  
  125. //map for the program
  126.  
  127. BitMapObject bmoMap;
  128.  
  129. //block images
  130.  
  131. BitMapObject bmoBlocks;
  132.  
  133. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  134.  
  135. {
  136.  
  137.     //which message did we get?
  138.  
  139.     switch(uMsg)
  140.  
  141.     {
  142.  
  143.     case WM_KEYDOWN:
  144.  
  145.         {
  146.  
  147.             //check for escape key
  148.  
  149.             if(wParam==VK_ESCAPE)
  150.  
  151.        
  152. {
  153.              MessageBox(NULL, "You have beaten the high score of " "\n\n\n\n Your score is: ", "High Score", MB_OK);
  154.              Beep(1000, 80);Beep(800, 80);Beep(700, 80);Beep(600, 80);Beep(500, 80);
  155.                 DestroyWindow(hWndMain);
  156.                
  157.  
  158.  
  159.                 return(0);//handled message
  160.  
  161.             }
  162.  
  163.             else if(wParam==VK_DOWN) //check for down arrow key
  164.  
  165.             {
  166.  
  167.                 Move(0,1);
  168.  
  169.                 return(0);//handled message
  170.  
  171.             }
  172.  
  173.             else if(wParam==VK_UP) //check for up arrow key
  174.  
  175.             {
  176.                  Beep(300,30);
  177.                 RotateBlock();
  178.  
  179.                 return(0);//handled message
  180.  
  181.             }
  182.  
  183.             else if(wParam==VK_LEFT) //check for left arrow key
  184.  
  185.             {
  186.  
  187.                 Move(-1,0);
  188.  
  189.                 return(0);//handled message
  190.  
  191.             }
  192.  
  193.             else if(wParam==VK_RIGHT) //check for right arrow key
  194.  
  195.             {
  196.  
  197.                 Move(1,0);
  198.  
  199.                 return(0);//handled message
  200.  
  201.             }
  202.  
  203.         }break;
  204.  
  205.     case WM_DESTROY://the window is being destroyed
  206.  
  207.         {
  208.  
  209.  
  210.  
  211.             //tell the application we are quitting
  212.  
  213.             PostQuitMessage(0);
  214.  
  215.  
  216.  
  217.             //handled message, so return 0
  218.  
  219.             return(0);
  220.  
  221.  
  222.  
  223.         }break;
  224.  
  225.     case WM_PAINT://the window needs repainting
  226.  
  227.         {
  228.  
  229.             //a variable needed for painting information
  230.  
  231.             PAINTSTRUCT ps;
  232.  
  233.  
  234.  
  235.             //start painting
  236.  
  237.             HDC hdc=BeginPaint(hwnd,&ps);
  238.  
  239.  
  240.  
  241.             //redraw the map
  242.  
  243.             BitBlt(hdc,0,0,TILESIZE*MAPWIDTH+TILESIZE*GREY,TILESIZE*MAPHEIGHT,bmoMap,0,0,SRCCOPY);
  244.  
  245.  
  246.  
  247.             //end painting
  248.  
  249.             EndPaint(hwnd,&ps);
  250.  
  251.  
  252.  
  253.             //handled message, so return 0
  254.  
  255.             return(0);
  256.  
  257.         }break;
  258.  
  259.     }
  260.  
  261.  
  262. if(wParam==VK_SHIFT) MessageBox(NULL, "BloX has been paused,\nplease press 'Ok' to continue. ", "A Falling Block Game!", MB_OK); ;
  263.  
  264.     //pass along any other message to default message handler
  265.  
  266.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  267.  
  268. }
  269. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  270.  
  271. {
  272.  
  273.     //assign instance to global variable
  274.  
  275.     hInstMain=hInstance;
  276.  
  277.  
  278.  
  279.     //create window class
  280.  
  281.     WNDCLASSEX wcx;
  282.  
  283.  
  284.  
  285.     //set the size of the structure
  286.  
  287.     wcx.cbSize=sizeof(WNDCLASSEX);
  288.  
  289.  
  290.  
  291.     //class style
  292.  
  293.     wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  294.  
  295.  
  296.  
  297.     //window procedure
  298.  
  299.     wcx.lpfnWndProc=TheWindowProc;
  300.  
  301.  
  302.  
  303.     //class extra
  304.  
  305.     wcx.cbClsExtra=0;
  306.  
  307.  
  308.  
  309.     //window extra
  310.  
  311.     wcx.cbWndExtra=0;
  312.  
  313.  
  314.  
  315.     //application handle
  316.  
  317.     wcx.hInstance=hInstMain;
  318.  
  319.  
  320.  
  321.     //icon
  322.  
  323.        wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  324.  
  325.  
  326.  
  327.     //cursor
  328.  
  329.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  330.  
  331.  
  332.  
  333.     //background color
  334.  
  335.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  336.  
  337.  
  338.  
  339.     //menu
  340.  
  341.     wcx.lpszMenuName=NULL;
  342.  
  343.  
  344.  
  345.     //class name
  346.  
  347.     wcx.lpszClassName=WINDOWCLASS;
  348.  
  349.  
  350.  
  351.     //small icon
  352.  
  353.     wcx.hIconSm=NULL;
  354.  
  355.  
  356. Beep(1000,100);Beep(800,100);Beep(600,100);Sleep(100);Beep(600,100);Sleep(10);Beep(600,100);Beep(1000,200);Beep(800,200);Beep(1200,600);Beep(800,100);Beep(600,100);Beep(400,100);Beep(200,100);Beep(0,100);Beep(200,100);Beep(400,100);Beep(600,100);Beep(800,100);Beep(1000,100);
  357.     //register the window class, return 0 if not successful
  358.  
  359.     if(!RegisterClassEx(&wcx)) return(0);
  360.  
  361.  
  362.  
  363.     //create main window
  364.  
  365.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  366.  
  367.  
  368.  
  369.     //error check
  370.  
  371.     if(!hWndMain) return(0);
  372.  
  373.  
  374.  
  375.     //if program initialization failed, then return with 0
  376.  
  377.     if(!GameInit()) return(0);
  378.  
  379.  
  380.  
  381.     //message structure
  382.  
  383.     MSG msg;
  384.  
  385.  
  386.  
  387.     //message pump
  388.  
  389.     for( ; ; )
  390.  
  391.     {
  392.  
  393.         //look for a message
  394.  
  395.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  396.  
  397.         {
  398.  
  399.             //there is a message
  400.  
  401.  
  402.  
  403.             //check that we arent quitting
  404.  
  405.             if(msg.message==WM_QUIT) break;
  406.  
  407.  
  408.  
  409.             //translate message
  410.  
  411.             TranslateMessage(&msg);
  412.  
  413.  
  414.  
  415.             //dispatch message
  416.  
  417.             DispatchMessage(&msg);
  418.  
  419.         }
  420.  
  421.  
  422.  
  423.         //run main game loop
  424.  
  425.         GameLoop();
  426.  
  427.  
  428.  
  429.     }
  430.  
  431.  
  432.  
  433.     //clean up program data
  434.  
  435.     GameDone();
  436.  
  437.  
  438.  
  439.     //return the wparam from the WM_QUIT message
  440.  
  441.     return(msg.wParam);
  442.  
  443. }
  444.  
  445. bool GameInit()
  446.  
  447. {
  448.  
  449.     //set the client area size
  450.  
  451.     RECT rcTemp;
  452.  
  453.     SetRect(&rcTemp,0,0,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);//160x480 client area
  454.  
  455.     AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
  456.  
  457.     SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height
  458.  
  459.  
  460.  
  461.     //create map image
  462.  
  463.     HDC hdc=GetDC(hWndMain);
  464.  
  465.     bmoMap.Create(hdc,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);
  466.  
  467.     FillRect(bmoMap,&rcTemp,(HBRUSH)GetStockObject(BLACK_BRUSH));
  468.  
  469.     ReleaseDC(hWndMain,hdc);
  470.  
  471.  
  472.  
  473.     bmoBlocks.Load(NULL,"blocks.bmp");
  474.  
  475.     NewGame();
  476.  
  477.  
  478.  
  479.     return(true);//return success
  480.  
  481. }
  482.  
  483.  
  484.  
  485. void GameDone()
  486.  
  487. {
  488.  
  489.     //clean up code goes here
  490.  
  491. }
  492.  
  493. void GameLoop()
  494.  
  495. {
  496.  
  497.     if( (GetTickCount() - start_time) > 1000)
  498.  
  499.     {
  500.  
  501.         Move(0,1);
  502.  
  503.         start_time=GetTickCount();
  504.  
  505.     }
  506.  
  507.  
  508.  
  509. }
  510.  
  511. void NewGame()
  512.  
  513. {
  514.  
  515.     start_time=GetTickCount();
  516.  
  517.     GAMESTARTED=false;
  518.  
  519.  
  520.  
  521.     //start out the map
  522.  
  523.     for(int x=0;x< MAPWIDTH;x++)
  524.  
  525.     {
  526.  
  527.         for(int y=0;y< MAPHEIGHT+1;y++)
  528.  
  529.         {
  530.  
  531.             if(y==MAPHEIGHT) //makes Y-collision easier.
  532.  
  533.                 Map[x][y]=TILEGREY;
  534.  
  535.             else
  536.  
  537.                 Map[x][y]=TILEBLACK;
  538.  
  539.         }
  540.  
  541.     }
  542.  
  543.     NewBlock();
  544.  
  545.  
  546.  
  547.     DrawMap();
  548.  
  549. }
  550.  
  551. void DrawTile(int x,int y,int tile)//put a tile
  552.  
  553. {
  554.  
  555.     //mask first
  556.  
  557.     BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,TILESIZE,SRCAND);
  558.  
  559.     //then image
  560.  
  561.     BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,0,SRCPAINT);
  562.  
  563. }
  564.  
  565. void DrawMap()//draw screen
  566.  
  567. {
  568.  
  569.     int xmy, ymx;
  570.  
  571.  
  572.  
  573.     //place the toolbar //MAPHEIGHT
  574.  
  575.     for(xmy=MAPWIDTH; xmy< MAPWIDTH+GREY; xmy++)
  576.  
  577.         for(ymx=0; ymx<MAPHEIGHT; ymx++)
  578.  
  579.             DrawTile(xmy, ymx, TILEGREY);
  580.  
  581.  
  582.  
  583.     //draw preview block
  584.  
  585.     for(xmy=0; xmy<4; xmy++)
  586.  
  587.         for(ymx=0; ymx<4; ymx++)
  588.  
  589.             if(sPrePiece.size[xmy][ymx] != TILENODRAW)
  590.  
  591.                 DrawTile(sPrePiece.x+xmy, sPrePiece.y+ymx, sPrePiece.size[xmy][ymx]);
  592.  
  593.  
  594.  
  595.     //draw the map
  596.  
  597.     //loop through the positions
  598.  
  599.     for(xmy=0;xmy< MAPWIDTH;xmy++)
  600.  
  601.         for(ymx=0;ymx< MAPHEIGHT;ymx++)
  602.  
  603.                 DrawTile(xmy,ymx,Map[xmy][ymx]);
  604.  
  605.  
  606.  
  607.     //draw moving block
  608.     for(xmy=0; xmy<4; xmy++)
  609.  
  610.         for(ymx=0; ymx<4; ymx++)
  611.  
  612.             if(sPiece.size[xmy][ymx] != TILENODRAW)
  613.  
  614.                 DrawTile(sPiece.x+xmy, sPiece.y+ymx, sPiece.size[xmy][ymx]);
  615.  
  616.  
  617.  
  618.     //invalidate the window rect
  619.  
  620.     InvalidateRect(hWndMain,NULL,FALSE);
  621.  
  622. }
  623.  
  624. void NewBlock()
  625.  
  626. {
  627.  
  628.     int newblock;
  629.  
  630.     int i,j;
  631.  
  632.     //  0   1   2   3   4    5   6
  633.  
  634.     //   X                             These
  635.  
  636.     //   X   XX   X  XX   XX  XX   XX  are
  637.  
  638.     //   X   XX  XXX  XX XX    X   X   block
  639.  
  640.     //   X                     X   X   types
  641.  
  642.  
  643.  
  644.     //begin game! make generate a block and then one in preview.
  645.  
  646.  
  647.  
  648.     srand(GetTickCount());
  649.  
  650.  
  651.  
  652.  
  653.  
  654.     //initialize the piece to all blank.
  655.  
  656.     for(i=0; i<4; i++)
  657.  
  658.         for(j=0; j<4; j++)
  659.  
  660.             sPiece.size[ i ][j]=TILENODRAW;
  661.  
  662.  
  663.  
  664.     sPiece.x=MAPWIDTH/2-2;
  665.  
  666.     sPiece.y=-1;
  667.  
  668.  
  669.  
  670.     //let's see if the game's started yet
  671.  
  672.     if(GAMESTARTED == false)
  673.  
  674.     {
  675.  
  676.         //guess not..
  677.  
  678.         //Generate a piece right off.
  679.  
  680.         //From now on, use previous preview block.
  681.  
  682.         GAMESTARTED=true;
  683.  
  684.  
  685.  
  686.         newblock=rand()%7;
  687.  
  688.  
  689.  
  690.         switch (newblock)
  691.  
  692.         {
  693.  
  694.         case 0: //Tower!
  695.  
  696.             {
  697.  
  698.                 sPiece.size[1][0]=TILERED;
  699.  
  700.                 sPiece.size[1][1]=TILERED;
  701.  
  702.                 sPiece.size[1][2]=TILERED;
  703.  
  704.                 sPiece.size[1][3]=TILERED;
  705.  
  706.                 sPiece.y=0;
  707.  
  708.             }break;
  709.  
  710.         case 1: //Box!
  711.  
  712.             {
  713.  
  714.                 sPiece.size[1][1]=TILEBLUE;
  715.  
  716.                 sPiece.size[1][2]=TILEBLUE;
  717.  
  718.                 sPiece.size[2][1]=TILEBLUE;
  719.  
  720.                 sPiece.size[2][2]=TILEBLUE;
  721.  
  722.             }break;
  723.  
  724.         case 2: //Pyramid!
  725.  
  726.             {
  727.  
  728.                 sPiece.size[1][1]=TILESTEEL;
  729.  
  730.                 sPiece.size[0][2]=TILESTEEL;
  731.  
  732.                 sPiece.size[1][2]=TILESTEEL;
  733.  
  734.                 sPiece.size[2][2]=TILESTEEL;
  735.  
  736.             }break;
  737.  
  738.         case 3://Left Leaner
  739.  
  740.             {
  741.  
  742.                 sPiece.size[0][1]=TILEYELLOW;
  743.  
  744.                 sPiece.size[1][1]=TILEYELLOW;
  745.  
  746.                 sPiece.size[1][2]=TILEYELLOW;
  747.  
  748.                 sPiece.size[2][2]=TILEYELLOW;
  749.  
  750.             }break;
  751.  
  752.         case 4://Right Leaner
  753.  
  754.             {
  755.  
  756.                 sPiece.size[2][1]=TILEGREEN;
  757.  
  758.                 sPiece.size[1][1]=TILEGREEN;
  759.  
  760.                 sPiece.size[1][2]=TILEGREEN;
  761.  
  762.                 sPiece.size[0][2]=TILEGREEN;
  763.  
  764.             }break;
  765.  
  766.         case 5://Left Knight
  767.  
  768.             {
  769.  
  770.                 sPiece.size[1][1]=TILEWHITE;
  771.  
  772.                 sPiece.size[2][1]=TILEWHITE;
  773.  
  774.                 sPiece.size[2][2]=TILEWHITE;
  775.  
  776.                 sPiece.size[2][3]=TILEWHITE;
  777.  
  778.             }break;
  779.  
  780.         case 6://Right Knight
  781.  
  782.             {
  783.  
  784.                 sPiece.size[2][1]=TILEPURPLE;
  785.  
  786.                 sPiece.size[1][1]=TILEPURPLE;
  787.  
  788.                 sPiece.size[1][2]=TILEPURPLE;
  789.  
  790.                 sPiece.size[1][3]=TILEPURPLE;
  791.  
  792.             }break;
  793.  
  794.         }
  795.  
  796.     }
  797.  
  798.     else
  799.  
  800.     {
  801.  
  802.         for(i=0; i<4; i++)
  803.  
  804.             for(j=0; j<4; j++)
  805.  
  806.                 sPiece.size[ i ][j]=sPrePiece.size[ i ][j];
  807.  
  808.  
  809.  
  810.     }
  811.  
  812.  
  813.  
  814.     newblock=rand()%7;
  815.  
  816.  
  817.  
  818.     for(i=0; i<4; i++)
  819.  
  820.         for(j=0; j<4; j++)
  821.  
  822.             sPrePiece.size[ i ][j]=TILENODRAW;
  823.  
  824.  
  825.  
  826.     sPrePiece.x=MAPWIDTH+GREY/4;
  827.  
  828.     sPrePiece.y=GREY/4;
  829.  
  830.  
  831.  
  832.     switch (newblock)
  833.  
  834.     {
  835.  
  836.         case 0: //Tower!
  837.  
  838.             {
  839.  
  840.                 sPrePiece.size[1][0]=TILERED;
  841.  
  842.                 sPrePiece.size[1][1]=TILERED;
  843.  
  844.                 sPrePiece.size[1][2]=TILERED;
  845.  
  846.                 sPrePiece.size[1][3]=TILERED;
  847.  
  848.             }break;
  849.  
  850.         case 1: //Box!
  851.  
  852.             {
  853.  
  854.                 sPrePiece.size[1][1]=TILEBLUE;
  855.  
  856.                 sPrePiece.size[1][2]=TILEBLUE;
  857.  
  858.                 sPrePiece.size[2][1]=TILEBLUE;
  859.  
  860.                 sPrePiece.size[2][2]=TILEBLUE;
  861.  
  862.             }break;
  863.  
  864.         case 2: //Pyramid!
  865.  
  866.             {
  867.  
  868.                 sPrePiece.size[1][1]=TILESTEEL;
  869.  
  870.                 sPrePiece.size[0][2]=TILESTEEL;
  871.  
  872.                 sPrePiece.size[1][2]=TILESTEEL;
  873.  
  874.                 sPrePiece.size[2][2]=TILESTEEL;
  875.  
  876.             }break;
  877.  
  878.         case 3://Left Leaner
  879.  
  880.             {
  881.  
  882.                 sPrePiece.size[0][1]=TILEYELLOW;
  883.  
  884.                 sPrePiece.size[1][1]=TILEYELLOW;
  885.  
  886.                 sPrePiece.size[1][2]=TILEYELLOW;
  887.  
  888.                 sPrePiece.size[2][2]=TILEYELLOW;
  889.  
  890.             }break;
  891.  
  892.         case 4://Right Leaner
  893.  
  894.             {
  895.  
  896.                 sPrePiece.size[2][1]=TILEGREEN;
  897.  
  898.                 sPrePiece.size[1][1]=TILEGREEN;
  899.  
  900.                 sPrePiece.size[1][2]=TILEGREEN;
  901.  
  902.                 sPrePiece.size[0][2]=TILEGREEN;
  903.  
  904.             }break;
  905.  
  906.         case 5://Left Knight
  907.  
  908.             {
  909.  
  910.                 sPrePiece.size[1][1]=TILEWHITE;
  911.  
  912.                 sPrePiece.size[2][1]=TILEWHITE;
  913.  
  914.                 sPrePiece.size[2][2]=TILEWHITE;
  915.  
  916.                 sPrePiece.size[2][3]=TILEWHITE;
  917.  
  918.             }break;
  919.  
  920.         case 6://Right Knight
  921.  
  922.             {
  923.  
  924.                 sPrePiece.size[2][1]=TILEPURPLE;
  925.  
  926.                 sPrePiece.size[1][1]=TILEPURPLE;
  927.  
  928.                 sPrePiece.size[1][2]=TILEPURPLE;
  929.  
  930.                 sPrePiece.size[1][3]=TILEPURPLE;
  931.  
  932.             }break;
  933.  
  934.     }
  935.  
  936.  
  937.  
  938.  
  939.     DrawMap();
  940.  
  941. }
  942.  
  943. void RotateBlock()
  944.  
  945. {
  946.  
  947.     int i, j, temp[4][4];
  948.  
  949.  
  950.  
  951.     //copy &rotate the piece to the temporary array
  952.  
  953.     for(i=0; i<4; i++)
  954.  
  955.         for(j=0; j<4; j++)
  956.  
  957.             temp[3-j][ i ]=sPiece.size[ i ][j];
  958.  
  959.  
  960.  
  961.     //check collision of the temporary array with map borders
  962.  
  963.     for(i=0; i<4; i++)
  964.  
  965.         for(j=0; j<4; j++)
  966.  
  967.             if(temp[ i ][j] != TILENODRAW)
  968.  
  969.                 if(sPiece.x + i < 0 || sPiece.x + i > MAPWIDTH - 1 ||
  970.  
  971.                     sPiece.y + j < 0 || sPiece.y + j > MAPHEIGHT - 1)
  972.  
  973.                     return;
  974.  
  975.  
  976.  
  977.     //check collision of the temporary array with the blocks on the map
  978.  
  979.     for(int x=0; x< MAPWIDTH; x++)
  980.  
  981.         for(int y=0; y< MAPHEIGHT; y++)
  982.  
  983.             if(x >= sPiece.x && x < sPiece.x + 4)
  984.  
  985.                 if(y >= sPiece.y && y < sPiece.y +4)
  986.  
  987.                     if(Map[x][y] != TILEBLACK)
  988.  
  989.                         if(temp[x - sPiece.x][y - sPiece.y] != TILENODRAW)
  990.  
  991.                             return;
  992.  
  993.  
  994.  
  995.     //end collision check
  996.  
  997.  
  998.  
  999.     //successful!  copy the rotated temporary array to the original piece
  1000.  
  1001.     for(i=0; i<4; i++)
  1002.  
  1003.         for(j=0; j<4; j++)
  1004.  
  1005.             sPiece.size[ i ][j]=temp[ i ][j];
  1006.  
  1007.  
  1008.  
  1009.     DrawMap();
  1010.  
  1011.  
  1012.  
  1013.     return;
  1014.  
  1015. }
  1016.  
  1017. void Move(int x, int y)
  1018.  
  1019. {
  1020.  
  1021.     if(CollisionTest(x, y))
  1022.  
  1023.     {
  1024.      
  1025.         if(y == 1)
  1026.  
  1027.         {
  1028. Beep(500, 100);
  1029.             if(sPiece.y<1)
  1030.  
  1031.             {
  1032.  
  1033.                 //you lose!  new game.
  1034.  
  1035.                 NewGame();
  1036.  
  1037.             }
  1038.  
  1039.             else
  1040.  
  1041.             {
  1042.  
  1043.                 bool killblock=false;
  1044.  
  1045.                 int i,j;
  1046.  
  1047.                 //new block time! add this one to the list!
  1048.  
  1049.                 for(i=0; i<4; i++)
  1050.  
  1051.                     for(j=0; j<4; j++)
  1052.  
  1053.                         if(sPiece.size[ i ][j] != TILENODRAW)
  1054.  
  1055.                             Map[sPiece.x+i][sPiece.y+j] = sPiece.size[ i ][j];
  1056.  
  1057.  
  1058.  
  1059.                 //check for cleared row!
  1060.  
  1061.                 for(j=0; j< MAPHEIGHT; j++)
  1062.  
  1063.                 {
  1064.  
  1065.                     bool filled=true;
  1066.  
  1067.                     for(i=0; i< MAPWIDTH; i++)
  1068.  
  1069.                         if(Map[ i ][j] == TILEBLACK)
  1070.  
  1071.                             filled=false;
  1072.  
  1073.  
  1074.  
  1075.                     if(filled)
  1076.  
  1077.                     {
  1078.  
  1079.                         RemoveRow(j);
  1080.  
  1081.                         killblock=true;
  1082.  
  1083.                     }
  1084.  
  1085.                 }
  1086.  
  1087.  
  1088.  
  1089.                 if(killblock)
  1090.  
  1091.                 {
  1092.  
  1093.                     for(i=0; i<4; i++)
  1094.  
  1095.                         for(j=0; j<4; j++)
  1096.  
  1097.                             sPiece.size[ i ][j]=TILENODRAW;
  1098.  
  1099.                 }
  1100.  
  1101.                 NewBlock();
  1102.  
  1103.             }
  1104.  
  1105.         }
  1106.  
  1107.  
  1108.  
  1109.     }
  1110.  
  1111.     else
  1112.  
  1113.     {
  1114.  
  1115.         sPiece.x+=x;
  1116.  
  1117.         sPiece.y+=y;
  1118.  
  1119.     }
  1120.  
  1121.  
  1122.  
  1123.     DrawMap();
  1124.  
  1125. }
  1126.  
  1127. int CollisionTest(int nx, int ny)
  1128.  
  1129. {
  1130.     int newx=sPiece.x+nx;
  1131.  
  1132.     int newy=sPiece.y+ny;
  1133.  
  1134.  
  1135.  
  1136.     int i,j,x,y;
  1137.  
  1138.  
  1139.  
  1140.     for(i=0; i< 4; i++)
  1141.  
  1142.         for(j=0; j< 4; j++)
  1143.  
  1144.             if(sPiece.size[ i ][j] != TILENODRAW)
  1145.  
  1146.                 if(newx + i < 0 || newx + i > MAPWIDTH - 1 ||
  1147.  
  1148.                     newy + j < 0 || newy + j > MAPHEIGHT - 1)
  1149.  
  1150.                     return 1;
  1151.  
  1152.  
  1153.  
  1154.     for(x=0; x< MAPWIDTH; x++)
  1155.  
  1156.         for(y=0; y< MAPHEIGHT; y++)
  1157.  
  1158.             if(x >= newx && x < newx + 4)
  1159.  
  1160.                 if(y >= newy && y < newy +4)
  1161.  
  1162.                     if(Map[x][y] != TILEBLACK)
  1163.  
  1164.                         if(sPiece.size[x - newx][y - newy] != TILENODRAW)
  1165.  
  1166.                             return 1;
  1167.  
  1168.     return 0;
  1169.  
  1170. }
  1171.  
  1172. int Score=1;
  1173.  
  1174. BOOL SetWindowText(HWND hWnd,LPCTSTR lpString);
  1175.  
  1176. void RemoveRow(int row)
  1177.  
  1178. {
  1179.  
  1180. int x,y;
  1181.  
  1182. int counter=0;
  1183.  
  1184.  
  1185.  
  1186. for(x=0; x< MAPWIDTH; x++)
  1187.  
  1188. for(y=row; y>0; y--)
  1189.  
  1190. Map[x][y]=Map[x][y-1];
  1191. #include <windows.h>
  1192.          Score++;
  1193.         char nScore[]="";
  1194.      itoa(Score,nScore,10);
  1195. using std::string;
  1196.     SetWindowText(hWndMain,(std::string(WINDOWTITLE) + " - " + "Score: " + nScore).c_str());
  1197.    
  1198.  
  1199. /*#include <iostream>
  1200. #include <fstream>
  1201. using namespace std;
  1202. void hiscore()
  1203. {
  1204.       ofstream outputFile;
  1205.       outputFile.open ("hiscore.txt");
  1206.       outputFile << nScore;
  1207.       outputFile.close();
  1208.  }*/
  1209.  
  1210.  Sleep(100); Beep(500, 100); Beep(1000, 300);
  1211. }
  1212.  
  1213.  
  1214.  
Advertisement
Add Comment
Please, Sign In to add comment