用C语言实现俄罗斯方块小游戏,内附完整代码。
```c
int board[ROWS][COLS] = {0}; // 游戏板
int current[4][2] = {0}; // 当前方块
int current_type = 0; // 当前方块类型
int current_row = 0; // 当前方块所在行
int current_col = 0; // 当前方块所在列
int score = 0; // 得分
// 方块类型及其形态
int types[7][4][2] = {
{{0, 0}, {0, 1}, {1, 0}, {1, 1}}, // 正方形
{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, // 一字型
{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, // L型
{{0, 0}, {0, 1}, {0, 2}, {-1, 2}}, // 反L型
{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, // Z型
{{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, // 反Z型
{{0, 0}, {0, 1}, {0, 2}, {1, 1}} // T型
};
// 在游戏板上绘制方块
void draw_block(int row, int col, int type) {
for (int i = 0; i < 4; i++) {
int r = row + types[type][i][0];
int c = col + types[type][i][1];
if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {
board[r][c] = 1;
}
}
}
// 从游戏板上移除方块
void remove_block(int row, int col, int type) {
for (int i = 0; i < 4; i++) {
int r = row + types[type][i][0];
int c = col + types[type][i][1];
if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {
board[r][c] = 0;
}
}
}
// 判断方块是否可以下落
int can_fall(int row, int col, int type) {
for (int i = 0; i < 4; i++) {
int r = row + types[type][i][0] + 1;
int c = col + types[type][i][1];
if (r >= ROWS || board[r][c]) {
return 0;
}
}
return 1;
}
// 判断方块是否可以左移
int can_move_left(int row, int col, int type) {
for (int i = 0; i < 4; i++) {
int r = row + types[type][i][0];
int c = col + types[type][i][1] - 1;
if (c < 0 || board[r][c]) {
return 0;
}
}
return 1;
}
// 判断方块是否可以右移
int can_move_right(int row, int col, int type) {
for (int i = 0; i < 4; i++) {
int r = row + types[type][i][0];
int c = col + types[type][i][1] + 1;
if (c >= COLS || board[r][c]) {
return 0;
}
}
return 1;
}
// 将当前方块固定在游戏板上
void fix_block() {
draw_block(current_row, current_col, current_type);
current_type = rand() % 7;
current_row = 0;
current_col = COLS / 2 - 2;
for (int i = 0; i < 4; i++) {
current[i][0] = types[current_type][i][0];
current[i][1] = types[current_type][i][1];
}
}
// 判断是否有一行已满
int is_full(int row) {
for (int i = 0; i < COLS; i++) {
if (board[row][i] == 0) {
return 0;
}
}
return 1;
}
// 将游戏板上的一行删除
void remove_row(int row) {
for (int i = row; i > 0; i--) {
for (int j = 0; j < COLS; j++) {
board[i][j] = board[i - 1][j];
}
}
for (int j = 0; j < COLS; j++) {
board[0][j] = 0;
}
score++;
}
// 显示游戏板和当前方块
void display() {
system("cls");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j]) {
printf("*");
} else if (i >= current_row && i < current_row + 4 && j >= current_col && j < current_col + 4 && current[i - current_row][j - current_col]) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
printf("Score: %d\n", score);
}
int main() {
srand(time(NULL));
fix_block();
while (1) {
display();
if (kbhit()) {
char c = getch();
if (c == 'a' && can_move_left(current_row, current_col, current_type)) {
remove_block(current_row, current_col, current_type);
current_col--;
} else if (c == 'd' && can_move_right(current_row, current_col, current_type)) {
remove_block(current_row, current_col, current_type);
current_col++;
} else if (c == 's' && can_fall(current_row, current_col, current_type)) {
remove_block(current_row, current_col, current_type);
current_row++;
} else if (c == ' ') {
while (can_fall(current_row, current_col, current_type)) {
remove_block(current_row, current_col, current_type);
current_row++;
}
fix_block();
}
}
if (can_fall(current_row, current_col, current_type)) {
remove_block(current_row, current_col, current_type);
current_row++;
} else {
fix_block();
for (int i = 0; i < ROWS; i++) {
if (is_full(i)) {
remove_row(i);
}
}
}
}
return 0;
}
```
评论