用c语言编写编写俄罗斯方块游戏代码
时间: 2023-10-21 16:18:09 浏览: 37
以下是使用 C 语言编写俄罗斯方块游戏的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
int board[HEIGHT][WIDTH] = {0};
int shapes[7][4][4] = {
{
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 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},
{1, 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, 0, 0},
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0}
}
};
int colors[7] = {1, 2, 3, 4, 5, 6, 7};
void draw_block(int x, int y, int shape) {
int color = colors[shape];
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, color);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shapes[shape][i][j]) {
int px = x + j;
int py = y + i;
COORD pos = {px, py};
SetConsoleCursorPosition(console, pos);
printf(" ");
}
}
}
}
void erase_block(int x, int y, int shape) {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, 7);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shapes[shape][i][j]) {
int px = x + j;
int py = y + i;
COORD pos = {px, py};
SetConsoleCursorPosition(console, pos);
printf(" ");
}
}
}
}
int check_collision(int x, int y, int shape) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shapes[shape][i][j]) {
int px = x + j;
int py = y + i;
if (px < 0 || px >= WIDTH || py >= HEIGHT) {
return 1;
}
if (py >= 0 && board[py][px]) {
return 1;
}
}
}
}
return 0;
}
void merge_block(int x, int y, int shape) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (shapes[shape][i][j]) {
int px = x + j;
int py = y + i;
board[py][px] = shape + 1;
}
}
}
}
void clear_lines() {
int lines_cleared = 0;
for (int i = HEIGHT - 1; i >= 0; i--) {
int is_full = 1;
for (int j = 0; j < WIDTH; j++) {
if (!board[i][j]) {
is_full = 0;
break;
}
}
if (is_full) {
lines_cleared++;
for (int k = i; k > 0; k--) {
for (int j = 0; j < WIDTH; j++) {
board[k][j] = board[k - 1][j];
}
}
for (int j = 0; j < WIDTH; j++) {
board[0][j] = 0;
}
i++;
}
}
if (lines_cleared) {
printf("Lines cleared: %d\n", lines_cleared);
}
}
void draw_board() {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, 7);
COORD pos = {0, 0};
SetConsoleCursorPosition(console, pos);
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j]) {
int color = colors[board[i][j] - 1];
SetConsoleTextAttribute(console, color);
printf(" ");
} else {
printf(".");
}
}
printf("\n");
}
}
int main() {
int x = WIDTH / 2 - 2;
int y = 0;
int shape = rand() % 7;
int rotation = 0;
int speed = 1;
int score = 0;
int game_over = 0;
while (!game_over) {
draw_board();
draw_block(x, y, shape);
Sleep(1000 / speed);
erase_block(x, y, shape);
y++;
if (check_collision(x, y, shape)) {
y--;
merge_block(x, y, shape);
clear_lines();
shape = rand() % 7;
rotation = 0;
x = WIDTH / 2 - 2;
y = 0;
if (check_collision(x, y, shape)) {
game_over = 1;
}
}
if (kbhit()) {
int key = getch();
switch (key) {
case 'a':
if (!check_collision(x - 1, y, shape)) {
x--;
}
break;
case 'd':
if (!check_collision(x + 1, y, shape)) {
x++;
}
break;
case 's':
if (!check_collision(x, y + 1, shape)) {
y++;
}
break;
case 'q':
rotation--;
if (rotation < 0) {
rotation = 3;
}
if (check_collision(x, y, (shape + rotation) % 4)) {
rotation++;
if (rotation > 3) {
rotation = 0;
}
}
break;
case 'e':
rotation++;
if (rotation > 3) {
rotation = 0;
}
if (check_collision(x, y, (shape + rotation) % 4)) {
rotation--;
if (rotation < 0) {
rotation = 3;
}
}
break;
}
}
}
printf("Game over! Score: %d\n", score);
return 0;
}
```
上述代码使用了 Windows 控制台 API 来实现绘制和控制光标等操作,因此只能在 Windows 系统上运行。如果需要在其他系统上运行,需要使用相应的绘图库来进行绘制和输入输出操作。