Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include "primlib.h"
- #include "pieces.inl"
- #define SQUARE_SIZE 20
- #define GAME_WIDTH 15
- #define GAME_HEIGHT 20
- #define MOVING_PIECE_COLOR YELLOW
- #define STATIC_PIECE_COLOR BLUE
- #define TIME 100000
- #if (GAME_WIDTH < 10 || GAME_WIDTH > 20 || GAME_HEIGHT < 10 || GAME_HEIGHT > 20)
- #error: "the wrong dimension of the game's area"
- #endif
- void updateState (char nextPiece[4][4]);
- bool isPossible(int x, int y, char piece[4][4], char move);
- int play(int x,int y, char whichPiece[4][4][4]);
- void deleteRows();
- void putPiece(int x,int y, char thatPiece[4][4], enum color colour);
- void end();
- int area[GAME_WIDTH][GAME_HEIGHT][3];
- int leftCorner_x = 100;
- int leftCorner_y = 70;
- int main()
- {
- int column, row;
- int whichPiece, nextPiece;
- initGraph();
- for(column = 0; column < GAME_WIDTH; column++)
- {
- for(row = 0;row < GAME_HEIGHT; row++)
- {
- area[column][row][0] = leftCorner_x + column * SQUARE_SIZE;
- area[column][row][1] = leftCorner_y + row * SQUARE_SIZE;
- area[column][row][2] = 0;
- }
- }
- whichPiece = (int) rand() % 7;
- while(1)
- {
- nextPiece = (int) rand() % 7;
- updateState(pieces[nextPiece][0]);
- updateScreen();
- if(isPossible((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == false)
- {
- end();
- break;
- }
- putPiece((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR);
- updateScreen();
- if(play((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
- {
- end();
- break;
- }
- deleteRows();
- whichPiece = nextPiece;
- }
- updateScreen();
- return 0;
- }
- void updateState(char nextPiece [4][4])
- {
- int column, row;
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- filledRect(leftCorner_x - 5, leftCorner_y,
- leftCorner_x - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
- filledRect(leftCorner_x, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE),
- leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
- filledRect(leftCorner_x + (GAME_WIDTH * SQUARE_SIZE), leftCorner_y,
- leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 4, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
- for(column = 0; column < GAME_WIDTH; column++)
- {
- for(row = 0; row < GAME_HEIGHT; row++)
- {
- filledRect(area[column][row][0] + 1, area[column][row][1] + 1,
- area[column][row][0] + SQUARE_SIZE - 2, area[column][row][1] + SQUARE_SIZE - 2, area[column][row][2]);
- }
- }
- textout( leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 11, leftCorner_y, "next piece: ", WHITE);
- for(column = 0; column < 4; column++)
- {
- for(row = 0; row < 4; row++)
- {
- if (nextPiece[column][row] != 0)
- {
- filledRect(area[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, area[column][row][1] + 11,
- area[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, area[column][row][1] + 28, MOVING_PIECE_COLOR);
- }
- }
- }
- }
- bool isPossible(int x, int y, char piece[4][4], char move)
- {
- int column, row;
- 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 false;
- }
- for(column = 0; column < 4; column++)
- {
- for(row = 0; row < 4; row++)
- {
- if(piece[column][row] != 0 && (x + column >= GAME_WIDTH || y + row >= GAME_HEIGHT))
- {
- return false;
- }
- if (piece[column][row] != 0 && area[x + column][y + row][2] != 0)
- {
- return false;
- }
- }
- }
- return true;
- }
- void putPiece(int x, int y, char piece[4][4], enum color colour)
- {
- int column,row;
- for(column = 0; column < 4; column++)
- {
- for(row = 0; row < 4; row++)
- {
- if(piece[column][row] != 0)
- {
- filledRect(area[x + column][y + row][0] + 1, area[x + column][y + row][1] + 1,
- area[x + column][y + row][0] + SQUARE_SIZE - 2, area[x + column][y + row][1] + SQUARE_SIZE - 2, colour);
- }
- }
- }
- }
- int play(int x, int y, char whichPiece[4][4][4])
- {
- int column, row, turn;
- int rotation = 0;
- while(1)
- {
- for(turn = 0; turn < 10; turn++)
- {
- if(isKeyDown('q') == 1)
- {
- return 0;
- }
- switch(pollkey())
- {
- case(SDLK_DOWN):
- while(isPossible(x, y, whichPiece[rotation], 'd') == true)
- {
- putPiece(x, y, whichPiece[rotation], BLACK);
- y++;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- goto saving;
- case(SDLK_RIGHT):
- if(isPossible(x, y, whichPiece[rotation], 'r') == true)
- {
- putPiece(x,y,whichPiece[rotation],BLACK);
- x++;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- break;
- case(SDLK_LEFT):
- if(isPossible(x, y, whichPiece[rotation], 'l') == true)
- {
- putPiece(x, y, whichPiece[rotation], BLACK);
- x--;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- break;
- case(SDLK_SPACE):
- if(isPossible(x, y, whichPiece[(rotation + 1) % 4], '0'))
- {
- putPiece(x, y, whichPiece[rotation], BLACK);
- ++rotation;
- rotation %= 4;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- else if (isPossible(x - 1, y, whichPiece[(rotation+1)%4], '0'))
- {
- putPiece(x, y, whichPiece[rotation], BLACK);
- ++rotation;
- rotation%=4;
- x--;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- else if (isPossible(x + 1, y, whichPiece[(rotation + 1) % 4], '0'))
- {
- putPiece(x, y, whichPiece[rotation], BLACK);
- ++rotation;
- rotation %= 4;
- x++;
- putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- break;
- }
- usleep(TIME);
- }
- if(isPossible(x, y, whichPiece[rotation], 'd') == true)
- {
- putPiece(x,y,whichPiece[rotation],BLACK);
- y++;
- putPiece(x,y,whichPiece[rotation], MOVING_PIECE_COLOR);
- updateScreen();
- }
- else
- {
- break;
- }
- }
- saving:
- for(column = 0; column < 4; column++)
- {
- for(row = 0; row < 4; row++)
- {
- if(whichPiece[rotation][column][row] != 0)
- {
- area[x + column][y + row][2] = STATIC_PIECE_COLOR;
- }
- }
- }
- return 1;
- }
- void deleteRows()
- {
- int column, row;
- int deletedRow;
- int alfa = 1;
- for(row = 0; row < GAME_HEIGHT; row++)
- {
- for(column = 0; column < GAME_WIDTH; column++)
- {
- alfa *= area[column][row][2];
- if (alfa == 0)
- {
- break;
- }
- }
- if(alfa != 0)
- {
- for(deletedRow = row; deletedRow > 0; deletedRow--)
- {
- for(column = 0; column < GAME_WIDTH; column++)
- {
- area[column][deletedRow][2] = area[column][deletedRow - 1][2];
- }
- }
- for(column = 0; column < GAME_WIDTH; column++)
- {
- area[column][0][2] = 0;
- }
- }
- alfa = 1;
- }
- }
- void end()
- {
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- textout((screenWidth() - 1) / 2 - 100, (screenHeight() - 1) / 2, "GAME OVER", WHITE);
- updateScreen();
- sleep(3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement