Advertisement
Monika__

patryks tetris

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