SHARE
TWEET

TETRIS JUPIII

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