Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>//usleep zawiesza wykonanie na czas wyrażony w mikrosekundach
- #include <stdio.h>
- #include "primlib.h"
- #include "pieces.inl"
- #include <stdbool.h>
- #define SQUARE_SIZE 20
- #define GAME_WIDTH 15
- #define GAME_HEIGHT 20
- #define MOVING_PIECE_COLOR MAGENTA
- #define STATIC_PIECE_COLOR BLUE
- #define PICES_TIME 20000
- #define SLEEP 3
- #define ARENA_FRAME_COLOUR WHITE
- #define GROUND_COLOR BLACK
- #define ROTATING_COLOR RED
- #define TEXT_COLOR MAGENTA
- #define X_LEFT_UPPER_CORDINATE 100
- #define Y_LEFT_UPPER_CORDINATE 65
- int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[5][5], char move);
- int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][5][5]);
- void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char thatPiece[5][5], enum color colour, enum color colors);
- void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [5][5]);
- void checkingBoundry();
- bool deleteRows(int area[GAME_WIDTH][GAME_HEIGHT][3]);
- void endingGame();
- int main ()
- {
- int columnCounter;
- int rowCounter;
- int area[GAME_WIDTH][GAME_HEIGHT][3];
- int whichPiece;
- int nextPiece;
- initGraph();
- for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
- {
- for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
- {
- area [columnCounter][rowCounter][0] = X_LEFT_UPPER_CORDINATE + columnCounter * SQUARE_SIZE;
- area [columnCounter][rowCounter][1] = Y_LEFT_UPPER_CORDINATE + rowCounter * SQUARE_SIZE;
- area [columnCounter][rowCounter][2] = 0;
- }
- }
- whichPiece = (int)rand()%7;
- while(1)
- {
- nextPiece = (int)rand()%7;
- updatingState(area, pieces[nextPiece][0]);
- updateScreen();
- if(isPossible(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == 0)
- {
- endingGame();
- break;
- }
- startingPiecePosition(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- if(play(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
- {
- endingGame();
- break;
- }
- deleteRows(area);
- whichPiece = nextPiece;
- }
- updateScreen();
- return 0;
- }
- void checkingBoundry()
- {
- if(GAME_WIDTH < 10 || GAME_WIDTH > 20)
- {
- printf("Invalid GAME_WIDTH \n");
- exit(3);
- }
- if(GAME_HEIGHT < 10 || GAME_HEIGHT >20)
- {
- printf("Invalid GAME_HEIGHT\n");
- exit(3);
- }
- }
- int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x,int y,char piece[5][5], char move)
- {
- int columnCounter;
- int rowCounter;
- switch(move)
- {
- case('l'):
- x--;
- break;
- case('r'):
- x++;
- break;
- case('d'):
- y++;
- break;
- case('0'):
- break;
- };
- if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
- {
- return 0;
- }
- for(columnCounter = 0; columnCounter < 5; columnCounter++)
- {
- for(rowCounter = 0 ; rowCounter < 5; rowCounter++)
- {
- if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT || x + columnCounter < 0))
- {
- return 0; //piece out of the game area
- }
- if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
- {
- return 0; // piece reaches the non-empty place
- }
- }
- }
- return 1;
- }
- bool deleteRows(int area[GAME_WIDTH][GAME_HEIGHT][3])
- {
- int columnCounter;
- int rowCounter;
- int deletedRow;
- int checkingEmptySpot;
- for(rowCounter = 0; rowCounter < GAME_HEIGHT; rowCounter++)
- {
- for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
- {
- checkingEmptySpot*= area[columnCounter][rowCounter][2]; //because the black colour is'0' value
- if (checkingEmptySpot == false)
- {
- break; // if empty spot we not need check anything more
- }
- }
- if(checkingEmptySpot != 0)
- {
- for(deletedRow = rowCounter; deletedRow > 0; deletedRow--)
- {
- for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
- {
- area[columnCounter][deletedRow][2] = area[columnCounter][deletedRow - 1][2];
- }
- }
- for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
- {
- area[columnCounter][0][2]=0;
- }
- }
- checkingEmptySpot = true;
- }
- }
- int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][5][5])
- {
- int columnCounter;
- int rowCounter;
- int turnCounter;
- int rotation = 0;
- while(1)
- {
- for(turnCounter = 0; turnCounter < 10; turnCounter++) //10moves 0.1s each,gives 1s time for the movement downward
- {
- if(isKeyDown('e') == 1) return 0; // to quit the game
- switch(pollkey())
- {
- case(SDLK_DOWN):
- while(isPossible(area, x, y, whichPiece[rotation], 'd') == 1)
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- y++;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }
- goto storingPices;
- case(SDLK_RIGHT):
- if(isPossible(area, x, y, whichPiece[rotation], 'r') == 1)
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- x++;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }break;
- case(SDLK_LEFT):
- if(isPossible(area, x, y, whichPiece[rotation],'l') == 1)
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- x--;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }break;
- case(SDLK_SPACE):
- if(isPossible(area, x, y, whichPiece[(rotation + 1) % 4],'0'))
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- ++rotation;
- rotation %= 4;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }
- /* 'else-if' checks if the movement to right or left enables the rotation */
- else if (isPossible(area, x - 1, y, whichPiece[(rotation + 1) % 4],'0'))
- {
- startingPiecePosition(area,x , y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- ++rotation;
- rotation %= 4;
- x--;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }
- else if (isPossible(area, x+1, y, whichPiece[(rotation + 1) % 4], '0'))
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- ++rotation;
- rotation %= 4;
- x++;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }
- break;
- };
- usleep(PICES_TIME);
- }
- if(isPossible(area, x, y, whichPiece[rotation],'d') == 1)
- {
- startingPiecePosition(area, x, y,whichPiece[rotation], GROUND_COLOR, GROUND_COLOR);
- y++; //not accelerated down
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATING_COLOR);
- updateScreen();
- }
- else break;
- }
- storingPices:
- for(columnCounter = 0; columnCounter < 5; columnCounter++)
- {
- for(rowCounter = 0;rowCounter < 5; rowCounter++)
- {
- if(whichPiece[rotation][columnCounter][rowCounter] != 0)
- {
- area[x+columnCounter][y+rowCounter][2] = STATIC_PIECE_COLOR;
- }
- }
- }
- return 1;
- }
- void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [5][5])
- {
- int columnCounter;
- int rowCounter;
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, GROUND_COLOR);
- textout(0, 10, "Use arrows on your keyboard to move the piece and space to rotate it.", TEXT_COLOR);
- textout(0, 20, "To Accelerate the pice press downn arrow.TO END THE GAME PRESS 'e'", TEXT_COLOR);
- filledRect(X_LEFT_UPPER_CORDINATE - 5, Y_LEFT_UPPER_CORDINATE, X_LEFT_UPPER_CORDINATE - 1, Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4,
- ARENA_FRAME_COLOUR);
- filledRect(X_LEFT_UPPER_CORDINATE, Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE), X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * SQUARE_SIZE) - 1,
- Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
- filledRect(X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * SQUARE_SIZE), Y_LEFT_UPPER_CORDINATE, X_LEFT_UPPER_CORDINATE + (GAME_WIDTH*SQUARE_SIZE) + 4,
- Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
- //drawingStaticPices
- for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
- {
- for(rowCounter = 0; rowCounter < GAME_HEIGHT; rowCounter++)
- {
- filledRect(area[columnCounter][rowCounter][0] + 1, area[columnCounter][rowCounter][1] + 1,
- area[columnCounter][rowCounter][0] + SQUARE_SIZE - 2, area[columnCounter][rowCounter][1] + SQUARE_SIZE - 2,
- area[columnCounter][rowCounter][2]);
- }
- }
- //drawingNextPices
- textout( X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * 20) + 11, Y_LEFT_UPPER_CORDINATE, "next piece: ", TEXT_COLOR);
- for(columnCounter = 0; columnCounter < 5; columnCounter++)
- {
- for(rowCounter = 0; rowCounter < 5; rowCounter++)
- {
- if (nextPiece[columnCounter][rowCounter]!= 0)
- {
- filledRect(area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 50, area[columnCounter][rowCounter][1] + 50,
- area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 67, area[columnCounter][rowCounter][1] + 67,
- MOVING_PIECE_COLOR);
- }
- }
- }
- }
- void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[5][5],enum color colour, enum color color)// enum is the keyword to declare a new enumeration type. color is the tag name that you can use later as a type name.
- {
- int columnCounter;
- int rowCounter;
- for(columnCounter = 0; columnCounter < 5; columnCounter++)
- {
- for(rowCounter = 0;rowCounter < 5; rowCounter++)
- {
- if(piece[columnCounter][rowCounter] == 1)
- {
- filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
- area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, colour);
- }
- if(piece[columnCounter][rowCounter] == 2)
- {
- filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
- area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, color);
- }
- }
- }
- }
- void endingGame()
- {
- filledRect(0, 0,screenWidth() - 1, screenHeight() -1, GROUND_COLOR);
- textout(280, 240, "Game over , sorry :( ", TEXT_COLOR );
- updateScreen();
- sleep(SLEEP);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement