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
- void updatingState (int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece[4][4]);
- 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);
- /*after checking the movement 'legality' deplace the piece*/
- void endingGame();
- void checkingBoundry();
- 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);
- /*Puts the piece at the center of upper frame of the game area*/
- 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);
- }
- }
- void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4])
- {
- int columnCounter,rowCounter; /*counters while working on arrays */
- char result[31];
- filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK); /*clears the screen*/
- textout(0,10,"Use arrowCounters to move the piece and space to rotate it. Tdimensionso quit press 'q'.",WHITE);
- textout(0,20,"You will receive additional points for cancelling multiple rowCounters.",WHITE);
- /*Instruction on how to play*/
- filledRect(xLeftUpperCordinate-5, yLeftUpperCordinate, xLeftUpperCordinate-1, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4, WHITE);/* the frame around the area dependingGameing on its size */
- filledRect(xLeftUpperCordinate, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE), xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)-1, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4,WHITE);
- filledRect(xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE), yLeftUpperCordinate, xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)+4, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4, WHITE);
- /* the 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]);
- /* the preview of the next piece will be placed on the right from the game's area*/
- textout( xLeftUpperCordinate+(GAME_WIDTH*20)+11, yLeftUpperCordinate,"next piece: ",WHITE);
- 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);
- sprintf(result,"Your score is: %d");
- textout(xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)+11,yLeftUpperCordinate+100,result,WHITE);
- }
- 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; /* if index is out of boundary the movement is not possible */
- }
- 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; /* if the piece is out of the game area */
- }
- if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
- {
- return 0; /* if the 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++) /* 10 truns (to move the piece before it moves down) 0.1s each, which gives 1s time for the movement downward*/
- {
- if(isKeyDown('q')==1) return 0; /* in order to quit the game */
- switch(pollkey())
- {
- case(SDLK_DOWN): /*accelerated movement downward*/
- while(isPossible(area,x,y,whichPiece[rotation],'d')==1)
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
- y++; /* move downward */
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }
- goto saving;
- case(SDLK_RIGHT): /* move to the right */
- if(isPossible(area,x,y,whichPiece[rotation],'r')==1)
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK); /* draw the black piece */
- x++;/* move to the right */
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }break;
- case(SDLK_LEFT): /* move to the left*/
- if(isPossible(area,x,y,whichPiece[rotation],'l')==1)
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
- x--; /* move left*/
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }break;
- case(SDLK_SPACE):
- if(isPossible(area,x,y,whichPiece[(rotation+1)%4],'0'))
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
- ++rotation;/* rotate */rotation%=4;
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- 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],BLACK);/* draw the black piece */
- ++rotation;/* rotate */rotation%=4; x--;
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }
- else if (isPossible(area,x+1,y,whichPiece[(rotation+1)%4],'0'))
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
- ++rotation;/* rotate */rotation%=4; x++;
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }
- break;
- };
- usleep(PICES_TIME); /*This function makes the programm to wait for PICES_TIME before proceeding with other instructions */
- }
- if(isPossible(area,x,y,whichPiece[rotation],'d')==1)
- {
- startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
- y++; /*not accelerated movement downward */
- startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
- updateScreen();
- }
- else break; /* if it is impossible to move the piece downward the piece's place is stored and may not be changed any more */
- }
- saving: /* the piece's storage, we can jump to this etiquette pressing SDLK_DOWN key */
- 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 startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[4][4],enum color colour)
- {
- 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];// why 31?
- filledRect(0, 0,screenWidth() - 1, screenHeight() -1, BLACK);
- 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