Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <stdlib.h>
- #include <time.h>
- #include <conio.h>
- #define BOARD_HEIGHT 20
- #define BOARD_WIDTH 10
- #define BOARD_X 4
- #define BOARD_Y 2
- #define CBLOCK_X 13
- #define CBLOCK_Y 2
- #define DELAY 100
- #define RAND 4
- #define LEFT 75
- #define RIGHT 77
- #define UP 72
- #define DOWN 80
- #define SPACE 32
- int static score = 0;
- int static level = 1;
- int static speed = 180;
- int curX, curY;
- int board[BOARD_HEIGHT + 1][BOARD_WIDTH + 2] = { 0, };
- int block[][4][4] = {
- {
- { 0, 0, 0, 0 },
- { 1, 0, 0, 0 },
- { 1, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 0, 0 },
- { 1, 0, 0, 0 },
- { 1, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 0, 1, 0 },
- { 1, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 1, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 1, 0 },
- { 1, 0, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 0, 0, 0 },
- { 1, 0, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 1, 0, 0 },
- { 1, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 1, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 },
- { 1, 1, 1, 0 },
- { 0, 1, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 1, 0 },
- { 0, 1, 0, 0 } },
- {
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 },
- { 1, 1, 1, 1 },
- { 0, 0, 0, 0 } },
- {
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 },
- { 1, 1, 1, 1 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 1, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 1, 1, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 1, 0, 0 },
- { 0, 1, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 0, 1, 1, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 1, 0, 0 },
- { 0, 1, 1, 0 },
- { 0, 0, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 1, 0 },
- { 0, 1, 1, 0 },
- { 0, 1, 0, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 1, 1, 0 },
- { 0, 0, 0, 0 } },
- {
- { 0, 0, 1, 0 },
- { 0, 1, 1, 0 },
- { 0, 1, 0, 0 },
- { 0, 0, 0, 0 } },
- };
- void removeCursor(void)
- {
- CONSOLE_CURSOR_INFO curInfo;
- GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
- curInfo.bVisible = 0;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
- }
- void setCursor(int x, int y)
- {
- COORD pos = { x, y };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- }
- COORD getCursor(void)
- {
- COORD cur;
- CONSOLE_SCREEN_BUFFER_INFO curInfo;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
- cur.X = curInfo.dwCursorPosition.X;
- cur.Y = curInfo.dwCursorPosition.Y;
- return cur;
- }
- void showBoard(void)
- {
- int x, y;
- for (x = 1; x <= BOARD_WIDTH + 1; x++)
- {
- board[BOARD_HEIGHT][x] = 1;
- setCursor((BOARD_X)+x * 2, BOARD_Y + BOARD_HEIGHT);
- printf("━");
- }
- for (y = 0; y<BOARD_HEIGHT + 1; y++)
- {
- board[y][0] = 1;
- setCursor(BOARD_X, BOARD_Y + y);
- if (y == BOARD_HEIGHT)
- printf("┗");
- else
- printf("┃");
- }
- for (y = 0; y<BOARD_HEIGHT + 1; y++)
- {
- board[y][BOARD_WIDTH + 1] = 1;
- setCursor(BOARD_X + (BOARD_WIDTH + 2) * 2, BOARD_Y + y);
- if (y == BOARD_HEIGHT)
- printf("┛");
- else
- printf("┃");
- }
- board[20][0] = 1;
- board[20][11] = 1;
- puts(" ");
- }
- void initial(int x, int y)
- {
- curX = x;
- curY = y;
- setCursor(x, y);
- }
- int detect(int rotation, int move1, int move2)
- {
- int x, y;
- int arrX, arrY;
- COORD pos = getCursor();
- arrX = pos.X + move1;
- arrY = pos.Y + move2;
- arrX = (arrX / 2) - 2;
- arrY = arrY - BOARD_Y;
- for (y = 0; y<4; y++)
- {
- for (x = 0; x<4; x++)
- {
- if ((block[rotation][y][x] == 1) && board[arrY + y][arrX + x] == 1)
- return 1;
- }
- }
- return 0;
- }
- void showBlock(int rotation)
- {
- int x, y;
- COORD cursor = getCursor();
- int prove;
- prove = detect(rotation, 0, 0);
- if (prove == 0)
- {
- for (y = 0; y<4; y++)
- {
- for (x = 0; x<4; x++)
- {
- setCursor(cursor.X + (x * 2), cursor.Y + y);
- if (block[rotation][y][x] == 1)
- printf("■");
- }
- }
- setCursor(cursor.X, cursor.Y);
- }
- }
- void removeBlock(int rotation, int move1, int move2)
- {
- int pr;
- int x, y;
- COORD cursor = getCursor();
- pr = detect(rotation, move1, move2);
- if (pr == 0)
- {
- for (y = 0; y<4; y++)
- {
- for (x = 0; x<4; x++)
- {
- setCursor(cursor.X + (x * 2), cursor.Y + y);
- if (block[rotation][y][x] == 1)
- printf(" ");
- }
- }
- setCursor(cursor.X + move1, cursor.Y + move2);
- }
- }
- void boardConginition(int n, int move1, int move2)
- {
- int x1, y1;
- COORD pos = getCursor();
- int arrX = pos.X + move1;
- int arrY = pos.Y + move2;
- int x, y;
- arrX = arrX / 2 - 2;
- arrY = arrY - 2;
- for (y = 0; y<4; y++)
- {
- for (x = 0; x<4; x++)
- {
- if (block[n][y][x] == 1)
- {
- board[arrY + y][arrX + x] = 1;
- }
- }
- }
- return;
- }
- void array_down(int column)
- {
- int y, x;
- for (y = column; y >= 0; y--)
- {
- for (x = 1; x <= 10; x++)
- {
- board[y][x] = board[y - 1][x];
- }
- }
- for (x = 1; x <= 10; x++)
- board[0][x] = 0;
- for (y = 1; y <= 19; y++)
- {
- for (x = 1; x <= 10; x++)
- {
- setCursor((BOARD_X)+x * 2 + 1, y + BOARD_Y);
- if (board[y][x] == 1)
- printf("■");
- else
- printf(" ");
- }
- }
- }
- void scoreLevel(void)
- {
- setCursor(40, 3);
- printf("Misson : Level 10 Game Clear");
- setCursor(40, 5);
- printf("Level:%d\n", level);
- setCursor(40, 7);
- printf("Score:%d\n", score);
- }
- void countScore(void)
- {
- score += 10;
- if (score % 30 == 0)
- {
- level += 1;
- speed -= 30;
- }
- scoreLevel();
- }
- void control(void)
- {
- int i;
- int x, y;
- int z = 0;
- for (y = 19; y >= 1; y--)
- {
- for (z = 0; z<4; z++)
- {
- i = 0;
- for (x = 1; x<11; x++)
- {
- if (board[y][x] == 1)
- {
- i++;
- if (i == 10)
- {
- for (x = 1; x<11; x++)
- {
- setCursor((x + 2) * 2, y + 2);
- printf(" ");
- }
- countScore();
- array_down(y);
- }
- }
- }
- }
- }
- }
- int gameOver(int rotation)
- {
- if (detect(rotation, 0, 0))
- return 5;
- else
- return 0;
- }
- int gameWin(void)
- {
- if (level == 10)
- return 1;
- else
- return 0;
- }
- void moveBlock(void)
- {
- int n;
- int kb;
- int prove;
- int winOver;
- srand(time(NULL));
- int c = 2;
- while (1)
- {
- initial(CBLOCK_X, CBLOCK_Y);
- n = rand() % 7;
- n = n * 4;
- if (gameWin())
- {
- setCursor(35, 20);
- printf("GAME WIN");
- getchar();
- exit(1);
- }
- if (gameOver(n))
- break;
- while (1)
- {
- int ww = 0;
- int var;
- int k = 0;
- int tmp;
- while (!_kbhit())
- {
- showBlock(n);
- Sleep(DELAY + speed);
- if (detect(n, 0, 1) == 1)
- {
- ww = 1;
- boardConginition(n, 0, 0);
- control();
- break;
- }
- removeBlock(n, 0, 1);
- }
- if (ww == 1)
- break;
- kb = _getch();
- switch (kb)
- {
- case LEFT:
- removeBlock(n, -2, 0);
- showBlock(n);
- break;
- case RIGHT:
- removeBlock(n, 2, 0);
- showBlock(n);
- break;
- case UP:
- k = n / 4;
- k *= 4;
- if ((n + 1) <= (k + 3))
- {
- k = n + 1;
- }
- prove = detect(k, 0, 0);
- if (prove == 0)
- {
- removeBlock(n, 0, 0);
- n = k;
- showBlock(n);
- break;
- }
- break;
- case DOWN:
- removeBlock(n, 0, 2);
- break;
- case SPACE:
- while (1)
- {
- removeBlock(n, 0, 1);
- if (detect(n, 0, 1) == 1)
- {
- showBlock(n);
- boardConginition(n, 0, 0);
- break;
- }
- }
- }
- }
- }
- setCursor(35, 20);
- printf("GAME OVER");
- }
- int main()
- {
- removeCursor();
- setCursor(2, 1);
- showBoard();
- scoreLevel();
- moveBlock();
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment