SHARE
TWEET

TETRIS ODDAM WIII

Mary_99 May 7th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "primlib.h"
  2. #include "pieces.inl"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <time.h>
  7.  
  8. #define COLOUR_BACKGROUND BLACK
  9. #define COLOUR_FRAME WHITE
  10. #define COLOUR_PIECE_STATIC BLUE
  11. #define COLOUR_PIECE_MOVING MAGENTA
  12. #define COLOUR_PIECE_COVERED RED
  13. #define COLOUR_PIECE_FRAME BLACK
  14. #define COLOUR_TEXT MAGENTA
  15.  
  16. #define COLUMNS 10
  17. #define ROWS 20
  18. #define BLOCK_SIZE 20
  19. #define NUMBER_OF_PIECES 7
  20. #define NUMBER_OF_ROTATIONS 4
  21. #define PIECE_ARRAY_SIZE 4
  22. #define FALLING_RATE 300
  23. #define LOOP_ITERATION_TIME 50
  24.  
  25. #define FRAME_THICKNESS 3
  26. #define NUMBER_OF_END_TEXTS 3
  27. #define INSTRUCTION_SPACING 10
  28.  
  29. #define FONT_SIZE 8
  30. #define EMPTY_SPACE 0
  31. #define MOVEMENT_LEFT -1
  32. #define MOVEMENT_RIGHT 1
  33.  
  34. #define TEXT_INITIAL_CONTENT "PRESS ANY KEY TO START"
  35. #define TEXT_NEXT_PIECE_CONTENT "NEXT PIECE:"
  36. #define TEXT_GAMEOVER_CONTENT "GAME OVER"
  37. #define TEXT_INSTRUCTION1_CONTENT "ESC TO CLOSE"
  38. #define TEXT_INSTRUCTION2_CONTENT "SPACE TO TRY AGAIN"
  39.  
  40. #define BOARD_LEFTX (screenWidth() - sizeOfWindowNextPiece) / 2 - (calculatedBlockSize * COLUMNS) / 2
  41. #define BOARD_BOTTOMY screenHeight() / 2 + (calculatedBlockSize * ROWS)/  2
  42. #define BOARD_RIGHTX (screenWidth() - sizeOfWindowNextPiece) /2 + (calculatedBlockSize * COLUMNS) / 2
  43. #define BOARD_TOPY screenHeight() / 2 - (calculatedBlockSize * ROWS) / 2
  44. #define BOARD_FRAME_LEFTX BOARD_LEFTX - FRAME_THICKNESS
  45. #define BOARD_FRAME_BOTTOMY BOARD_BOTTOMY + FRAME_THICKNESS
  46. #define BOARD_FRAME_RIGHTX BOARD_RIGHTX + FRAME_THICKNESS
  47. #define BOARD_FRAME_TOPY BOARD_TOPY - FRAME_THICKNESS
  48.  
  49. #define GAME_OVER_BOX_LEFT_X BOARD_FRAME_LEFTX
  50. #define GAME_OVER_BOX_BOTTOM_Y INSTRUCTION2_TEXT_Y + INSTRUCTION_SPACING + FONT_SIZE
  51. #define GAME_OVER_BOX_RIGHT_X BOARD_FRAME_RIGHTX
  52. #define GAME_OVER_BOX_TOP_Y screenHeight()/2-NUMBER_OF_END_TEXTS * INSTRUCTION_SPACING
  53. #define GAME_OVER_FRAME_LEFT_X BOARD_FRAME_LEFTX
  54. #define GAME_OVER_FRAME_BOTTOM_Y GAME_OVER_BOX_BOTTOM_Y + FRAME_THICKNESS
  55. #define GAME_OVER_FRAME_RIGHT_X BOARD_FRAME_RIGHTX
  56. #define GAME_OVER_FRAME_TOP_Y GAME_OVER_BOX_TOP_Y - FRAME_THICKNESS
  57.  
  58. #define NEXT_PIECE_BOX_LEFT_X NEXT_PIECE_FRAME_LEFT_X + FRAME_THICKNESS
  59. #define NEXT_PIECE_BOX_BOTTOM_Y NEXT_PIECE_FRAME_BOTTOM_Y - FRAME_THICKNESS
  60. #define NEXT_PIECE_BOX_RIGHT_X NEXT_PIECE_FRAME_RIGHT_X - FRAME_THICKNESS
  61. #define NEXT_PIECE_BOX_TOP_Y NEXT_PIECE_FRAME_TOP_Y + FRAME_THICKNESS
  62. #define NEXT_PIECE_FRAME_LEFT_X screenWidth() - sizeOfWindowNextPiece
  63. #define NEXT_PIECE_FRAME_BOTTOM_Y NEXT_PIECE_FRAME_TOP_Y + sizeOfWindowNextPiece - BORDER_TO_BOARD_DISTANCE / 2
  64. #define NEXT_PIECE_FRAME_RIGHT_X NEXT_PIECE_FRAME_LEFT_X + sizeOfWindowNextPiece - BORDER_TO_BOARD_DISTANCE / 2
  65. #define NEXT_PIECE_FRAME_TOP_Y BOARD_FRAME_TOPY+2*INSTRUCTION_SPACING
  66.  
  67. #define INITIAL_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INITIAL_CONTENT) / 2  * FONT_SIZE
  68. #define INITIAL_TEXT_Y screenHeight() / 3
  69. #define NEXT_PIECE_TEXT_X NEXT_PIECE_FRAME_LEFT_X
  70. #define NEXT_PIECE_TEXT_Y BOARD_FRAME_TOPY
  71. #define GAME_OVER_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_GAMEOVER_CONTENT) / 2 * FONT_SIZE
  72. #define GAME_OVER_TEXT_Y GAME_OVER_BOX_TOP_Y + INSTRUCTION_SPACING
  73. #define INSTRUCTION1_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INSTRUCTION1_CONTENT) / 2 * FONT_SIZE
  74. #define INSTRUCTION1_TEXT_Y GAME_OVER_TEXT_Y + 2 * INSTRUCTION_SPACING
  75. #define INSTRUCTION2_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INSTRUCTION2_CONTENT) / 2 * FONT_SIZE
  76. #define INSTRUCTION2_TEXT_Y INSTRUCTION1_TEXT_Y + INSTRUCTION_SPACING
  77.  
  78. #define BLOCK_X BOARD_LEFTX + calculatedBlockSize * boardXPosition
  79. #define BLOCK_Y BOARD_BOTTOMY - calculatedBlockSize * boardYPosition
  80. #define NEXT_PIECE_BLOCK_X NEXT_PIECE_BOX_LEFT_X + BLOCK_SIZE * boardXPosition + 10
  81. #define NEXT_PIECE_BLOCK_Y NEXT_PIECE_BOX_TOP_Y + BLOCK_SIZE * (boardYPosition + 1) + 10
  82.  
  83. #define SPAWN_BOARD_POSITION COLUMNS / 2 - PIECE_ARRAY_SIZE / 4
  84. #define NEXT_PIECE_NOT_DISPLAYED NUMBER_OF_PIECES + 1
  85. #define BLOCK_COVERED 2
  86. #define BORDER_TO_BOARD_DISTANCE 4 * FRAME_THICKNESS
  87.  
  88.  
  89. void initialize(void);
  90. void randomSpawn(void);
  91. void drawing(void);
  92. void falling(void);
  93. void moving(void);
  94. void rotating(void);
  95. void drop(void);
  96. void cleaningLine(void);
  97. void gameOver(void);
  98. void checkingBoundry(void);
  99.  
  100. int movingGameState[COLUMNS][ROWS];
  101. int staticGameState[COLUMNS][ROWS];
  102.  
  103. int pressedButton, checkSpawn, timeCounter, currentMovingPiece, nextMovingPiece;
  104. int currentMovingPieceRotation, nextMovingPieceRotation, loseCondition, calculatedBlockSize, sizeOfWindowNextPiece;
  105.  
  106.  
  107. int main(int argc, char* argv[])
  108. {
  109.     if(initGraph())
  110.     {
  111.         exit(3);
  112.     }
  113.  
  114.     do
  115.     {
  116.         initialize();
  117.         pressedButton = pollkey();
  118.         if(pressedButton == SDLK_ESCAPE)
  119.         {
  120.             exit(3);
  121.         }
  122.         else if((pressedButton != -1) && (pressedButton != SDLK_ESCAPE))
  123.         {
  124.             nextMovingPiece = rand()%NUMBER_OF_PIECES;
  125.             nextMovingPieceRotation = rand()%NUMBER_OF_ROTATIONS;
  126.             do
  127.             {
  128.                 if(checkSpawn)
  129.                 {
  130.                     randomSpawn();
  131.                 }
  132.                 if(loseCondition)
  133.                 {
  134.                 gameOver();
  135.                 break;
  136.                 }
  137.  
  138.                 drawing();
  139.                 updateScreen();
  140.                 pressedButton = pollkey();
  141.  
  142.                 switch(pressedButton)
  143.                 {
  144.                     case SDLK_LEFT:
  145.                     case SDLK_RIGHT:
  146.                         moving();
  147.                         break;
  148.                     case SDLK_SPACE:
  149.                         rotating();
  150.                         break;
  151.                     case SDLK_DOWN:
  152.                         drop();
  153.                         break;
  154.                     case SDLK_ESCAPE:
  155.                         exit(3);
  156.                 }
  157.  
  158.                 if(timeCounter >= FALLING_RATE)
  159.                 {
  160.                     falling();
  161.                 }
  162.                 cleaningLine();
  163.  
  164.                 SDL_Delay(LOOP_ITERATION_TIME);
  165.  
  166.                 timeCounter += LOOP_ITERATION_TIME;
  167.             }while(1);
  168.         }
  169.     }while(1);
  170.  
  171.   return 0;
  172. }
  173.  
  174.  
  175. void initialize()
  176. {
  177.     int initialHorizontalPosition;  
  178.     int initialVerticalPosition;
  179.  
  180.     srand(time(NULL));
  181.     checkSpawn = 1;
  182.     timeCounter = 0;
  183.     currentMovingPiece = 0;
  184.     currentMovingPieceRotation = 0;
  185.     loseCondition = 0;
  186.     nextMovingPiece = NEXT_PIECE_NOT_DISPLAYED;
  187.  
  188.     for(initialHorizontalPosition = 0; initialHorizontalPosition < COLUMNS; initialHorizontalPosition++)
  189.     {
  190.         for(initialVerticalPosition = 0; initialVerticalPosition < ROWS; initialVerticalPosition++)
  191.         {
  192.             movingGameState[initialHorizontalPosition][initialVerticalPosition] = EMPTY_SPACE;
  193.             staticGameState[initialHorizontalPosition][initialVerticalPosition] = EMPTY_SPACE;
  194.         }
  195.     }
  196.  
  197.     sizeOfWindowNextPiece = BORDER_TO_BOARD_DISTANCE + (PIECE_ARRAY_SIZE + 1) * BLOCK_SIZE;
  198.  
  199.     if(COLUMNS > ROWS)
  200.     {
  201.         calculatedBlockSize = (screenWidth() - sizeOfWindowNextPiece-  BORDER_TO_BOARD_DISTANCE) / (COLUMNS);
  202.     }else if(ROWS >= COLUMNS)
  203.     {
  204.     calculatedBlockSize = (screenHeight() - BORDER_TO_BOARD_DISTANCE) / (ROWS);
  205.     }
  206.  
  207.     drawing();
  208.     textout(INITIAL_TEXT_X, INITIAL_TEXT_Y ,TEXT_INITIAL_CONTENT, COLOUR_TEXT);
  209.     updateScreen();
  210. }
  211.  
  212.  
  213. void randomSpawn()
  214. {
  215.     int arrayX;
  216.     int arrayPositionY;
  217.     int arrayPieceY;
  218.  
  219.     currentMovingPiece = nextMovingPiece;
  220.     nextMovingPiece = rand()%NUMBER_OF_PIECES;
  221.     currentMovingPieceRotation = nextMovingPieceRotation;
  222.     nextMovingPieceRotation = rand()%NUMBER_OF_ROTATIONS;
  223.  
  224.     for(arrayX = 0; arrayX < PIECE_ARRAY_SIZE; arrayX++)
  225.     {
  226.         for(arrayPositionY = 0, arrayPieceY = PIECE_ARRAY_SIZE - 1; arrayPositionY < PIECE_ARRAY_SIZE; arrayPositionY++, arrayPieceY--)
  227.         {
  228.             if((staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] != 0) && (pieces[currentMovingPiece][currentMovingPieceRotation][arrayX][arrayPieceY] != 0))
  229.             {
  230.                 staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] = 0;
  231.                 movingGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] = BLOCK_COVERED;
  232.                 loseCondition = 1;
  233.             }
  234.             else if(staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE+arrayPositionY] == 0)
  235.                     {
  236.                     movingGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE+arrayPositionY] = pieces[currentMovingPiece][currentMovingPieceRotation]   [arrayX][arrayPieceY];
  237.                     }
  238.         }
  239.     }
  240.  
  241.     checkSpawn = 0;
  242. }
  243.  
  244.  
  245. void drawing()
  246. {
  247.     int boardXPosition;
  248.     int boardYPosition;
  249.  
  250.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, COLOUR_BACKGROUND);
  251.  
  252.     filledRect(BOARD_FRAME_LEFTX, BOARD_FRAME_BOTTOMY, BOARD_FRAME_RIGHTX, BOARD_FRAME_TOPY, COLOUR_FRAME);
  253.     filledRect(BOARD_LEFTX, BOARD_BOTTOMY, BOARD_RIGHTX, BOARD_TOPY, COLOUR_BACKGROUND);
  254.  
  255.     for(boardXPosition = 0; boardXPosition < COLUMNS; boardXPosition++)
  256.     {
  257.         for(boardYPosition = 0; boardYPosition < ROWS; boardYPosition++)
  258.         {
  259.             if(movingGameState[boardXPosition][boardYPosition] > 0)
  260.             {
  261.                 if(movingGameState[boardXPosition][boardYPosition] == BLOCK_COVERED)
  262.                 {
  263.                     filledRect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_COVERED);
  264.                     rect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_FRAME);
  265.                 }
  266.                 else
  267.                 {
  268.                     filledRect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_MOVING);
  269.                     rect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_FRAME);
  270.                 }
  271.             }
  272.             else if(staticGameState[boardXPosition][boardYPosition] > 0)
  273.             {
  274.                 filledRect(BLOCK_X,BLOCK_Y,BLOCK_X+calculatedBlockSize,BLOCK_Y-calculatedBlockSize,COLOUR_PIECE_STATIC);
  275.                 rect(BLOCK_X,BLOCK_Y,BLOCK_X+calculatedBlockSize,BLOCK_Y-calculatedBlockSize,COLOUR_PIECE_FRAME);
  276.             }
  277.         }
  278.     }
  279.  
  280.     filledRect(NEXT_PIECE_FRAME_LEFT_X, NEXT_PIECE_FRAME_BOTTOM_Y, NEXT_PIECE_FRAME_RIGHT_X, NEXT_PIECE_FRAME_TOP_Y, COLOUR_FRAME);
  281.     filledRect(NEXT_PIECE_BOX_LEFT_X, NEXT_PIECE_BOX_BOTTOM_Y, NEXT_PIECE_BOX_RIGHT_X, NEXT_PIECE_BOX_TOP_Y, COLOUR_BACKGROUND);
  282.     textout(NEXT_PIECE_TEXT_X, NEXT_PIECE_TEXT_Y, TEXT_NEXT_PIECE_CONTENT, COLOUR_TEXT);
  283.  
  284.  
  285.     if(nextMovingPiece != NEXT_PIECE_NOT_DISPLAYED)
  286.     {
  287.         for(boardXPosition = 0; boardXPosition < PIECE_ARRAY_SIZE; boardXPosition++)
  288.         {
  289.             for(boardYPosition = 0; boardYPosition < PIECE_ARRAY_SIZE; boardYPosition++)
  290.             {
  291.                 if(pieces[nextMovingPiece][nextMovingPieceRotation][boardXPosition][boardYPosition] > 0)
  292.                 {
  293.                     filledRect(NEXT_PIECE_BLOCK_X, NEXT_PIECE_BLOCK_Y, NEXT_PIECE_BLOCK_X + BLOCK_SIZE,
  294.                                NEXT_PIECE_BLOCK_Y -BLOCK_SIZE, COLOUR_PIECE_MOVING);
  295.                     rect(NEXT_PIECE_BLOCK_X, NEXT_PIECE_BLOCK_Y, NEXT_PIECE_BLOCK_X + BLOCK_SIZE,
  296.                          NEXT_PIECE_BLOCK_Y - BLOCK_SIZE, COLOUR_PIECE_FRAME);
  297.                 }
  298.             }
  299.         }
  300.     }
  301. }
  302.  
  303.  
  304. void falling(void)
  305. {
  306.     int fallingX, fallingY;
  307.     int convertToStatic = 0;
  308.  
  309.     for(fallingY = 0; fallingY < ROWS; fallingY++)
  310.     {
  311.         for(fallingX = 0; fallingX < COLUMNS; fallingX++)
  312.         {
  313.             if(((fallingY == 0) && (movingGameState[fallingX][fallingY] > 0 )) || ((fallingY > 0) && (movingGameState[fallingX][fallingY] > 0) && (staticGameState[fallingX][fallingY - 1]> 0)))
  314.             {
  315.                 convertToStatic = 1;
  316.                 break;
  317.             }
  318.         }
  319.         if(convertToStatic)
  320.         {
  321.             break;
  322.         }
  323.     }
  324.  
  325.     for(fallingY = 0; fallingY < ROWS; fallingY++)
  326.     {
  327.         for(fallingX = 0;fallingX < COLUMNS; fallingX++)
  328.         {
  329.             if((convertToStatic == 1) && (movingGameState[fallingX][fallingY] > 0))
  330.             {
  331.                 staticGameState[fallingX][fallingY] = movingGameState[fallingX][fallingY];
  332.                 movingGameState[fallingX][fallingY] = 0;
  333.                 checkSpawn = 1;
  334.             }
  335.             else if((convertToStatic == 0) && (movingGameState[fallingX][fallingY] > 0))
  336.             {
  337.                 movingGameState[fallingX][fallingY - 1] = movingGameState[fallingX][fallingY];
  338.                 movingGameState[fallingX][fallingY] = 0;
  339.                 }
  340.         }
  341.     }
  342.  
  343.     timeCounter = 0;
  344. }
  345.  
  346.  
  347. void moving(void)
  348. {
  349.     int directionOfMovement, movementXPosition, movementYPosition, moveable = 1;
  350.  
  351.     if(pressedButton==SDLK_LEFT)
  352.     {  
  353.         directionOfMovement = MOVEMENT_LEFT;
  354.     }
  355.     else if(pressedButton == SDLK_RIGHT)
  356.     {
  357.         directionOfMovement = MOVEMENT_RIGHT;
  358.     }
  359.  
  360.     for(movementYPosition = 0; movementYPosition < ROWS; movementYPosition++)
  361.     {
  362.         if(directionOfMovement > 0)
  363.         {
  364.             for(movementXPosition = COLUMNS - 1; movementXPosition >= 0; movementXPosition--)
  365.             {
  366.                 if((movingGameState[movementXPosition][movementYPosition] > 0) &&((movementXPosition == COLUMNS - 1)||(staticGameState[movementXPosition+directionOfMovement][movementYPosition] > 0)))
  367.                 {
  368.                     moveable = 0;
  369.                     break;
  370.                 }
  371.             }
  372.         }
  373.         else if(directionOfMovement < 0)
  374.         {
  375.             for(movementXPosition = 0; movementXPosition < COLUMNS; movementXPosition++)
  376.             {
  377.                 if((movingGameState[movementXPosition][movementYPosition] > 0) && ((movementXPosition == 0)||(staticGameState[movementXPosition+directionOfMovement][movementYPosition] > 0)))
  378.                 {
  379.                     moveable = 0;
  380.                     break;
  381.                 }
  382.             }
  383.         }
  384.         if(moveable == 0)
  385.         {
  386.             break;
  387.         }
  388.     }
  389.  
  390.     if(moveable)
  391.     {
  392.         for(movementYPosition = 0;movementYPosition < ROWS; movementYPosition++)
  393.         {
  394.             if(directionOfMovement > 0)
  395.             {
  396.                 for(movementXPosition = COLUMNS - 1;movementXPosition >= 0; movementXPosition--)
  397.                 {
  398.                     if(movingGameState[movementXPosition][movementYPosition] > EMPTY_SPACE)
  399.                     {
  400.                         movingGameState[movementXPosition + directionOfMovement][movementYPosition] = movingGameState[movementXPosition][movementYPosition];
  401.                         movingGameState[movementXPosition][movementYPosition] = EMPTY_SPACE;
  402.                     }
  403.                 }
  404.             }
  405.             else if(directionOfMovement < 0)
  406.             {
  407.                 for(movementXPosition = 0; movementXPosition < COLUMNS; movementXPosition++)
  408.                 {
  409.                     if(movingGameState[movementXPosition][movementYPosition] > EMPTY_SPACE)
  410.                     {
  411.                         movingGameState[movementXPosition+directionOfMovement][movementYPosition] = movingGameState[movementXPosition][movementYPosition];
  412.                         movingGameState[movementXPosition][movementYPosition] = EMPTY_SPACE;
  413.                     }
  414.                 }
  415.             }
  416.         }
  417.     }
  418. }
  419.  
  420.  
  421. void rotating(void)
  422. {
  423.     int nextPieceX,nextPieceY,xBeforeRotation,yBeforeRotation,newRotation,foundCenter,rotationIsValid, nextPieceStartX,nextPieceStartY,pieceWidth,pieceHeight,piece_inversed_height;
  424.  
  425.     if(currentMovingPieceRotation == NUMBER_OF_ROTATIONS - 1)
  426.     {
  427.         newRotation = 0;
  428.     }  
  429.     else if((currentMovingPieceRotation >= 0) && (currentMovingPieceRotation < NUMBER_OF_ROTATIONS - 1))
  430.     {
  431.         newRotation = currentMovingPieceRotation + 1;
  432.     }
  433.  
  434.     foundCenter = 0;
  435.    
  436.     for(pieceWidth = 0; pieceWidth < COLUMNS; pieceWidth++)
  437.     {
  438.         for(pieceHeight = 0;pieceHeight < ROWS; pieceHeight++)
  439.         {
  440.             if(movingGameState[pieceWidth][pieceHeight] > 1)  
  441.             {
  442.                 foundCenter = 1;
  443.                 xBeforeRotation = pieceWidth;
  444.                 yBeforeRotation = pieceHeight;
  445.                 break;
  446.             }
  447.         }
  448.         if(foundCenter)
  449.         {
  450.             break;
  451.         }
  452.     }
  453.  
  454.     foundCenter = 0;
  455.    
  456.     for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  457.     {
  458.         for(pieceHeight = 0; pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++)
  459.         {
  460.             if(pieces[currentMovingPiece][newRotation][pieceWidth][pieceHeight] > 1)
  461.             {
  462.                 foundCenter = 1;
  463.                 nextPieceX = pieceWidth;
  464.                 nextPieceY = PIECE_ARRAY_SIZE - 1- pieceHeight;
  465.                 break;
  466.             }
  467.         }
  468.         if(foundCenter)
  469.         {
  470.             break;
  471.         }
  472.     }
  473.  
  474.     nextPieceStartX  = xBeforeRotation - nextPieceX;
  475.     nextPieceStartY = yBeforeRotation - nextPieceY;
  476.  
  477.     if(nextPieceStartX >= 0 && nextPieceStartY >= 0)
  478.     {
  479.         rotationIsValid = 1;
  480.         for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  481.         {
  482.             for(pieceHeight = 0,piece_inversed_height = PIECE_ARRAY_SIZE - 1;pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++, piece_inversed_height--)
  483.             {
  484.                 if((pieces[currentMovingPiece][newRotation][pieceWidth][piece_inversed_height] > 0)&&
  485.                 (((nextPieceStartX + pieceWidth) >= COLUMNS)||((nextPieceStartY + pieceHeight) >= ROWS)||
  486.                 ((nextPieceStartX + pieceWidth) < 0)||((nextPieceStartY + pieceHeight) < 0)||
  487.                 (staticGameState[nextPieceStartX + pieceWidth][nextPieceStartY + pieceHeight] > 0)))
  488.                 {
  489.                 rotationIsValid = 0;
  490.                 break;
  491.                 }
  492.             }
  493.             if(rotationIsValid == 0)
  494.             {
  495.                 break;
  496.             }
  497.         }
  498.     }else rotationIsValid = 0;
  499.  
  500.     if(rotationIsValid)
  501.     {
  502.         for(pieceWidth = 0; pieceWidth < COLUMNS; pieceWidth++)
  503.         {
  504.             for(pieceHeight = 0;pieceHeight < ROWS; pieceHeight++)
  505.             {
  506.                 movingGameState[pieceWidth][pieceHeight] = EMPTY_SPACE;
  507.             }
  508.         }
  509.         for(pieceHeight = 0, piece_inversed_height = PIECE_ARRAY_SIZE - 1; pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++, piece_inversed_height--)
  510.         {
  511.             for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  512.             {
  513.                 if((nextPieceStartX + pieceWidth >= 0) && (nextPieceStartX + pieceWidth < COLUMNS) &&
  514.                     (nextPieceStartY+pieceHeight >= 0) && (nextPieceStartY + pieceHeight < ROWS))
  515.                 {
  516.                     movingGameState[nextPieceStartX + pieceWidth][nextPieceStartY + pieceHeight] = pieces[currentMovingPiece][newRotation][pieceWidth][piece_inversed_height];
  517.                 }
  518.             }
  519.         }
  520.         currentMovingPieceRotation = newRotation;
  521.     }
  522. }
  523.  
  524.  
  525. void drop(void)
  526. {
  527.     while(checkSpawn == 0) falling();
  528.     timeCounter = 0;
  529. }
  530.  
  531.  
  532. void cleaningLine(void)
  533. {
  534.     int rowIndex,columnIndex,emptyColumnIndex,moveRowDown,takenSpacesCount;
  535.    
  536.     for(rowIndex = 0;rowIndex<ROWS;rowIndex++)
  537.     {
  538.         takenSpacesCount = 0;
  539.         for(columnIndex = 0; columnIndex < COLUMNS; columnIndex++)
  540.         {
  541.             if(staticGameState[columnIndex][rowIndex] > EMPTY_SPACE)
  542.             {
  543.                 takenSpacesCount++;
  544.             }
  545.             else if(staticGameState[columnIndex][rowIndex] == EMPTY_SPACE)
  546.             {    
  547.                 break;
  548.             }
  549.             if(takenSpacesCount == COLUMNS)
  550.                     for(emptyColumnIndex = 0; emptyColumnIndex < COLUMNS; emptyColumnIndex++)
  551.                     {
  552.                         staticGameState[emptyColumnIndex][rowIndex] = EMPTY_SPACE;
  553.                         for(moveRowDown = rowIndex; moveRowDown<ROWS - 1; moveRowDown++)
  554.                         {
  555.                             staticGameState[emptyColumnIndex][moveRowDown] = staticGameState[emptyColumnIndex][moveRowDown + 1];
  556.                         }
  557.                     }
  558.         }
  559.     }
  560. }
  561.  
  562.  
  563. void gameOver(void)
  564. {
  565.     nextMovingPiece = 0;
  566.     drawing();
  567.     filledRect(GAME_OVER_FRAME_LEFT_X, GAME_OVER_FRAME_BOTTOM_Y, GAME_OVER_FRAME_RIGHT_X, GAME_OVER_FRAME_TOP_Y, COLOUR_FRAME);
  568.     filledRect(GAME_OVER_BOX_LEFT_X, GAME_OVER_BOX_BOTTOM_Y, GAME_OVER_BOX_RIGHT_X, GAME_OVER_BOX_TOP_Y, COLOUR_BACKGROUND);
  569.     textout(GAME_OVER_TEXT_X,GAME_OVER_TEXT_Y,TEXT_GAMEOVER_CONTENT,COLOUR_TEXT);
  570.     textout(INSTRUCTION1_TEXT_X, INSTRUCTION1_TEXT_Y, TEXT_INSTRUCTION1_CONTENT, COLOUR_TEXT);
  571.     textout(INSTRUCTION2_TEXT_X, INSTRUCTION2_TEXT_Y, TEXT_INSTRUCTION2_CONTENT, COLOUR_TEXT);
  572.     updateScreen();
  573.     do{
  574.         pressedButton = pollkey();
  575.         if(pressedButton == SDLK_ESCAPE)
  576.         {
  577.             exit(3);
  578.         }
  579.         if(pressedButton == SDLK_SPACE)
  580.         {
  581.             break;
  582.         }
  583.     }while(1);
  584. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top