c语言俄罗斯方块500行带码的
时间: 2023-11-27 21:04:48 浏览: 25
以下是一个基于 C 语言实现的俄罗斯方块游戏,共计约 500 行代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define SCREEN_WIDTH 32
#define SCREEN_HEIGHT 24
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 20
#define BLOCK_SIZE 2
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define ROTATE 72
#define BLOCK_COUNT 7
#define BLOCK_SIZE 2
#define BLOCK_I 0
#define BLOCK_O 1
#define BLOCK_L 2
#define BLOCK_J 3
#define BLOCK_S 4
#define BLOCK_Z 5
#define BLOCK_T 6
typedef struct
{
int x;
int y;
} Point;
int Board[BOARD_HEIGHT][BOARD_WIDTH] = {0};
int Score = 0;
int CurrentBlock = -1;
int CurrentX = -1;
int CurrentY = -1;
int CurrentRotation = -1;
int NextBlock = -1;
int Blocks[BLOCK_COUNT][4][4][2] =
{
{
{{0, 1}, {1, 1}, {2, 1}, {3, 1}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}},
{{0, 2}, {1, 2}, {2, 2}, {3, 2}},
{{1, 0}, {1, 1}, {1, 2}, {1, 3}}
},
{
{{1, 1}, {2, 1}, {1, 2}, {2, 2}},
{{1, 1}, {2, 1}, {1, 2}, {2, 2}},
{{1, 1}, {2, 1}, {1, 2}, {2, 2}},
{{1, 1}, {2, 1}, {1, 2}, {2, 2}}
},
{
{{0, 1}, {1, 1}, {2, 1}, {0, 2}},
{{1, 0}, {1, 1}, {1, 2}, {2, 2}},
{{0, 1}, {1, 1}, {2, 1}, {2, 0}},
{{1, 0}, {1, 1}, {1, 2}, {0, 0}}
},
{
{{2, 1}, {0, 1}, {1, 1}, {2, 2}},
{{1, 2}, {1, 0}, {1, 1}, {2, 0}},
{{0, 1}, {2, 1}, {1, 1}, {0, 0}},
{{1, 0}, {1, 2}, {1, 1}, {0, 2}}
},
{
{{1, 1}, {2, 1}, {0, 2}, {1, 2}},
{{1, 1}, {1, 2}, {2, 0}, {2, 1}},
{{1, 1}, {2, 1}, {0, 2}, {1, 2}},
{{1, 1}, {1, 2}, {2, 0}, {2, 1}}
},
{
{{0, 1}, {1, 1}, {1, 2}, {2, 2}},
{{2, 1}, {1, 1}, {1, 2}, {0, 2}},
{{0, 1}, {1, 1}, {1, 2}, {2, 2}},
{{2, 1}, {1, 1}, {1, 2}, {0, 2}}
},
{
{{1, 1}, {0, 2}, {1, 2}, {2, 2}},
{{1, 1}, {1, 2}, {2, 2}, {1, 0}},
{{1, 1}, {0, 2}, {1, 2}, {2, 2}},
{{1, 1}, {1, 2}, {2, 2}, {1, 0}}
}
};
void gotoxy(int x, int y)
{
printf("\033[%d;%df", y, x);
}
void hidecursor()
{
printf("\033[?25l");
}
void showcursor()
{
printf("\033[?25h");
}
void initBoard()
{
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
Board[i][j] = 0;
}
}
}
void printBlock(int x, int y, int block, int rotation)
{
for (int i = 0; i < 4; i++) {
int dx = Blocks[block][i][rotation][0];
int dy = Blocks[block][i][rotation][1];
gotoxy(x + dx * BLOCK_SIZE, y + dy * BLOCK_SIZE);
printf("[]");
}
}
void eraseBlock(int x, int y, int block, int rotation)
{
for (int i = 0; i < 4; i++) {
int dx = Blocks[block][i][rotation][0];
int dy = Blocks[block][i][rotation][1];
gotoxy(x + dx * BLOCK_SIZE, y + dy * BLOCK_SIZE);
printf(" ");
}
}
int canPlaceBlock(int x, int y, int block, int rotation)
{
for (int i = 0; i < 4; i++) {
int dx = Blocks[block][i][rotation][0];
int dy = Blocks[block][i][rotation][1];
int nx = x + dx;
int ny = y + dy;
if (nx < 0 || nx >= BOARD_WIDTH || ny < 0 || ny >= BOARD_HEIGHT) {
return 0;
}
if (Board[ny][nx] != 0) {
return 0;
}
}
return 1;
}
void placeBlock(int x, int y, int block, int rotation)
{
for (int i = 0; i < 4; i++) {
int dx = Blocks[block][i][rotation][0];
int dy = Blocks[block][i][rotation][1];
int nx = x + dx;
int ny = y + dy;
Board[ny][nx] = block + 1;
}
}
void removeBlock(int x, int y, int block, int rotation)
{
for (int i = 0; i < 4; i++) {
int dx = Blocks[block][i][rotation][0];
int dy = Blocks[block][i][rotation][1];
int nx = x + dx;
int ny = y + dy;
Board[ny][nx] = 0;
}
}
void printBoard()
{
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
int block = Board[i][j];
if (block != 0) {
printBlock(j * BLOCK_SIZE * 2, i * BLOCK_SIZE, block - 1, 0);
}
}
}
}
void printNextBlock(int block)
{
gotoxy(SCREEN_WIDTH + 4, 1);
printf("Next:");
printBlock(SCREEN_WIDTH + 4, 2, block, 0);
}
void printScore()
{
gotoxy(SCREEN_WIDTH + 4, 6);
printf("Score: %d", Score);
}
void newBlock()
{
CurrentBlock = NextBlock;
CurrentX = BOARD_WIDTH / 2 - 2;
CurrentY = 0;
CurrentRotation = 0;
NextBlock = rand() % BLOCK_COUNT;
if (!canPlaceBlock(CurrentX, CurrentY, CurrentBlock, CurrentRotation)) {
printf("Game Over\n");
exit(0);
}
}
int removeLines()
{
int count = 0;
for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
int full = 1;
for (int j = 0; j < BOARD_WIDTH; j++) {
if (Board[i][j] == 0) {
full = 0;
break;
}
}
if (full) {
count++;
for (int k = i; k > 0; k--) {
for (int j = 0; j < BOARD_WIDTH; j++) {
Board[k][j] = Board[k - 1][j];
}
}
for (int j = 0; j < BOARD_WIDTH; j++) {
Board[0][j] = 0;
}
}
}
return count;
}
void moveBlock(int dx, int dy)
{
eraseBlock(CurrentX * BLOCK_SIZE * 2, CurrentY * BLOCK_SIZE, CurrentBlock, CurrentRotation);
CurrentX += dx;
CurrentY += dy;
if (!canPlaceBlock(CurrentX, CurrentY, CurrentBlock, CurrentRotation)) {
CurrentX -= dx;
CurrentY -= dy;
placeBlock(CurrentX, CurrentY, CurrentBlock, CurrentRotation);
Score += removeLines();
newBlock();
}
printBoard();
printScore();
showcursor();
printBlock(CurrentX * BLOCK_SIZE * 2, CurrentY * BLOCK_SIZE, CurrentBlock, CurrentRotation);
hidecursor();
}
void rotateBlock(int direction)
{
eraseBlock(CurrentX * BLOCK_SIZE * 2, CurrentY * BLOCK_SIZE, CurrentBlock, CurrentRotation);
CurrentRotation = (CurrentRotation + direction) % 4;
if (!canPlaceBlock(CurrentX, CurrentY, CurrentBlock, CurrentRotation)) {
CurrentRotation = (CurrentRotation - direction) % 4;
}
printBoard();
showcursor();
printBlock(CurrentX * BLOCK_SIZE * 2, CurrentY * BLOCK_SIZE, CurrentBlock, CurrentRotation);
hidecursor();
}
int main()
{
srand(time(NULL));
hidecursor();
initBoard();
newBlock();
printBoard();
printNextBlock(NextBlock);
printScore();
while (1) {
if (kbhit()) {
int c = getch();
switch (c) {
case LEFT:
moveBlock(-1, 0);
break;
case RIGHT:
moveBlock(1, 0);
break;
case DOWN:
moveBlock(0, 1);
break;
case ROTATE:
rotateBlock(1);
break;
case 'q':
return 0;
}
}
}
}
```
这个代码实现了一个简单的俄罗斯方块游戏,包括方块的移动、旋转、消除、分数统计等功能。