Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include "primlib.h"
- #include "pieces.inl"
- #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
- int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], char move);
- int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4]);
- void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char thatPiece[4][4], enum color colour);
- void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4]);
- void endingGame();
- void checkingBoundry();
- //------global variables ---------//
- int xLeftUpperCordinate = 100;
- int yLeftUpperCordinate = 65;
- 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] = xLeftUpperCordinate + columnCounter * SQUARE_SIZE;
- area [columnCounter][rowCounter][1] = yLeftUpperCordinate + 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);
- updateScreen();
- if(play(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
- {
- endingGame();
- break;
- }
- 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[4][4], 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 < 4; columnCounter++)
- {
- for(rowCounter = 0 ; rowCounter < 4; rowCounter++)
- {
- if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT))
- {
- 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;
- }
- int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4])
- {
- 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);//black piece
- y++; //move down
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- goto storingPices;
- case(SDLK_RIGHT):
- if(isPossible(area, x, y, whichPiece[rotation], 'r') == 1)
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR);
- x++;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }break;
- case(SDLK_LEFT):
- if(isPossible(area, x, y, whichPiece[rotation],'l') == 1)
- {
- startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR);
- x--;
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }break;
- /*add here case whith rotation*/
- };
- usleep(PICES_TIME);
- }
- if(isPossible(area, x, y, whichPiece[rotation],'d') == 1)
- {
- startingPiecePosition(area, x, y,whichPiece[rotation], GROUND_COLOR);
- y++; //not accelerated down
- startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- else break;
- }
- storingPices:
- for(columnCounter = 0; columnCounter < 4; columnCounter++)
- {
- for(rowCounter = 0;rowCounter < 4; 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 [4][4])
- {
- int columnCounter;
- int rowCounter;
- char result[31];
- 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.", ARENA_FRAME_COLOUR);
- textout(0, 20, "To Accelerate the pice press downn arrow.TO END THE GAME PRESS 'e'", ARENA_FRAME_COLOUR);
- filledRect(xLeftUpperCordinate - 5, yLeftUpperCordinate, xLeftUpperCordinate - 1, yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4,
- ARENA_FRAME_COLOUR);
- filledRect(xLeftUpperCordinate, yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE), xLeftUpperCordinate + (GAME_WIDTH * SQUARE_SIZE) - 1,
- yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
- filledRect(xLeftUpperCordinate + (GAME_WIDTH * SQUARE_SIZE), yLeftUpperCordinate, xLeftUpperCordinate + (GAME_WIDTH*SQUARE_SIZE) + 4,
- yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
- // static pieces on the game's area
- 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]);
- }
- }
- //next pice
- for(columnCounter = 0; columnCounter < 4; columnCounter++)
- {
- for(rowCounter = 0; rowCounter < 4; rowCounter++)
- {
- if (nextPiece[columnCounter][rowCounter]!= 0)
- {
- filledRect(area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, area[columnCounter][rowCounter][1] + 11,
- area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, area[columnCounter][rowCounter][1] + 28,
- MOVING_PIECE_COLOR);
- }
- }
- }
- }
- void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[4][4],enum color colour)// 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 < 4; columnCounter++)
- {
- for(rowCounter = 0;rowCounter < 4; rowCounter++)
- {
- if(piece[columnCounter][rowCounter] != 0) /*draws pices of when their colour is other than black */
- {
- 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);
- }
- }
- }
- }
- void endingGame()
- {
- char result[31];
- filledRect(0, 0,screenWidth() - 1, screenHeight() -1, GROUND_COLOR);
- sprintf(result, " GAME OVER !!!");
- textout((screenWidth() - 1) / 2 - 100,(screenHeight() - 1) / 2, result, MAGENTA);
- updateScreen();
- sleep(SLEEP);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement