用c语言写一个俄罗斯方块的小游戏
时间: 2023-11-19 11:05:35 浏览: 84
好的,以下是一个简单的俄罗斯方块游戏的 C 语言代码:
```
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define WIDTH 10 // 游戏区宽度
#define HEIGHT 20 // 游戏区高度
#define MAX_BLOCK 7 // 最大方块种类数
char game[HEIGHT][WIDTH]; // 游戏区
int score; // 得分
// 从 x, y 开始画一个颜色为 c 的方块
void draw_block(int x, int y, int c) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { x * 2, y };
SetConsoleCursorPosition(hOut, pos);
SetConsoleTextAttribute(hOut, c);
printf("[]");
}
// 画出游戏区
void draw_game() {
int i, j;
system("cls");
for (i = 0; i < HEIGHT; i++)
for (j = 0; j < WIDTH; j++)
draw_block(j, i, game[i][j]);
printf("Score: %d\n", score);
}
// 移动方块
void move_block(int sx, int sy, int ex, int ey) {
game[ey][ex] = game[sy][sx];
game[sy][sx] = ' ';
}
// 旋转方块
void rotate_block(char block[][4], char new_block[][4]) {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
new_block[i][j] = block[3 - j][i];
}
// 判断是否出界或碰到别的方块
int check(int sx, int sy, char block[][4]) {
int i, j, x, y;
for (i = 0, y = sy; i < 4; i++, y++)
for (j = 0, x = sx; j < 4; j++, x++)
if (block[i][j] != ' ' && (x < 0 || x >= WIDTH || y >= HEIGHT || game[y][x] != ' '))
return 0;
return 1;
}
// 判断是否可以消行
void check_line() {
int i, j, k;
for (i = HEIGHT - 1; i >= 0; i--) {
for (j = 0; j < WIDTH; j++)
if (game[i][j] == ' ') break;
if (j == WIDTH) {
for (k = i; k > 0; k--)
memcpy(game[k], game[k - 1], WIDTH);
memset(game[0], ' ', WIDTH);
score++;
i++;
draw_game();
}
}
}
// 主函数
int main() {
char blocks[MAX_BLOCK][4][4] = { // 方块种类
{ " []",
" []",
" ",
" " },
{ " []",
" []",
" ",
" " },
{ " []",
" []",
" ",
" " },
{ "[][]",
"[][]",
" ",
" " },
{ " [] ",
"[][]",
" [] ",
" " },
{ " [][]",
"[] ",
"[] ",
" " },
{ "[] ",
"[][]",
" [] ",
" " }
};
int block_color[MAX_BLOCK] = { // 每种方块对应的颜色
0x07, // 白色
0x0E, // 黄色
0x0A, // 绿色
0x0B, // 青色
0x09, // 蓝色
0x0D, // 紫色
0x0C // 红色
};
int block_x, block_y, block_id;
char block[4][4], new_block[4][4];
int ch, speed = 10;
srand((unsigned)time(NULL));
memset(game, ' ', sizeof(game));
draw_game();
while (1) {
block_id = rand() % MAX_BLOCK;
memcpy(block, blocks[block_id], sizeof(block));
block_x = WIDTH / 2 - 2;
block_y = 0;
draw_game();
while (1) {
if (_kbhit()) {
ch = _getch();
if (ch == 'a' && check(block_x - 1, block_y, block)) {
move_block(block_x, block_y, block_x - 1, block_y);
block_x--;
} else if (ch == 'd' && check(block_x + 1, block_y, block)) {
move_block(block_x, block_y, block_x + 1, block_y);
block_x++;
} else if (ch == 's' && check(block_x, block_y + 1, block)) {
move_block(block_x, block_y, block_x, block_y + 1);
block_y++;
} else if (ch == 'w') {
rotate_block(block, new_block);
if (check(block_x, block_y, new_block))
memcpy(block, new_block, sizeof(block));
} else if (ch == ' ') {
while (check(block_x, block_y + 1, block)) {
move_block(block_x, block_y, block_x, block_y + 1);
block_y++;
}
}
}
if (! check(block_x, block_y + 1, block)) break;
move_block(block_x, block_y, block_x, block_y + 1);
block_y++;
Sleep(speed);
draw_game();
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (block[i][j] != ' ') game[block_y + i][block_x + j] = block[i][j];
check_line();
if (block_y < 3) break;
}
printf("Game Over!\n");
return 0;
}
```
希望能够帮助你!
阅读全文