用c语言写一个俄罗斯方块代码
时间: 2023-12-31 20:05:56 浏览: 77
下面是一个简单的俄罗斯方块游戏的C语言代码实现,基于Windows console编写,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10 // 游戏区域宽度
#define HEIGHT 20 // 游戏区域高度
#define BLOCK_SIZE 4 // 方块大小
int board[WIDTH][HEIGHT] = {0}; // 游戏区域
int block[4][4] = {0}; // 当前方块
int block_x = 0, block_y = 0; // 当前方块的位置
int block_type = 0; // 当前方块的类型
int score = 0; // 得分
// 方块类型定义
int block_shape[7][4][4] = {
// I型
{
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// J型
{
{1, 0, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// L型
{
{0, 0, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// O型
{
{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// S型
{
{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// T型
{
{0, 1, 0, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
// Z型
{
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
};
// 检测当前方块是否与游戏区域发生碰撞
int check_collision(int x, int y, int type)
{
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block_shape[type][i][j] == 1 &&
(x+j >= WIDTH || x+j < 0 || y+i >= HEIGHT || (y+i >= 0 && board[x+j][y+i] == 1))) {
return 1;
}
}
}
return 0;
}
// 将当前方块放到游戏区域中
void set_block(int x, int y, int type)
{
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block_shape[type][i][j] == 1) {
board[x+j][y+i] = 1;
}
}
}
}
// 绘制游戏区域
void draw_board()
{
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (board[j][i] == 1) {
printf("[]");
} else {
printf(" ");
}
}
printf("\n");
}
}
// 绘制当前方块
void draw_block()
{
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
if (block[i][j] == 1) {
printf("[]");
} else {
printf(" ");
}
}
printf("\n");
}
}
// 生成一个随机方块
void new_block()
{
block_type = rand() % 7;
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
block[i][j] = block_shape[block_type][i][j];
}
}
block_x = WIDTH / 2 - BLOCK_SIZE / 2;
block_y = -1 * BLOCK_SIZE;
}
// 消除已满的行
void clear_lines()
{
int i, j, k;
for (i = 0; i < HEIGHT; i++) {
int full = 1;
for (j = 0; j < WIDTH; j++) {
if (board[j][i] == 0) {
full = 0;
break;
}
}
if (full == 1) {
score += 10;
for (k = i; k > 0; k--) {
for (j = 0; j < WIDTH; j++) {
board[j][k] = board[j][k-1];
}
}
for (j = 0; j < WIDTH; j++) {
board[j][0] = 0;
}
}
}
}
int main()
{
srand((unsigned)time(NULL));
// 隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = 0;
SetConsoleCursorInfo(handle, &CursorInfo);
// 初始化游戏区域
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
board[j][i] = 0;
}
}
// 循环执行游戏
while (1) {
system("cls");
// 生成新方块
if (block_y == -1 * BLOCK_SIZE) {
new_block();
if (check_collision(block_x, block_y, block_type)) {
break;
}
}
// 处理用户输入
if (kbhit()) {
int c = getch();
switch (c) {
case 'a':
if (!check_collision(block_x-1, block_y, block_type)) {
block_x--;
}
break;
case 'd':
if (!check_collision(block_x+1, block_y, block_type)) {
block_x++;
}
break;
case 's':
if (!check_collision(block_x, block_y+1, block_type)) {
block_y++;
}
break;
case 'w':
int new_type = (block_type + 1) % 7;
if (!check_collision(block_x, block_y, new_type)) {
block_type = new_type;
int i, j;
for (i = 0; i < BLOCK_SIZE; i++) {
for (j = 0; j < BLOCK_SIZE; j++) {
block[i][j] = block_shape[block_type][i][j];
}
}
}
break;
case 'q':
exit(0);
break;
}
}
// 尝试下落
if (!check_collision(block_x, block_y+1, block_type)) {
block_y++;
} else {
set_block(block_x, block_y, block_type);
clear_lines();
block_y = -1 * BLOCK_SIZE;
}
// 绘制游戏区域和当前方块
draw_board();
printf("\nScore: %d\n", score);
printf("Use W/A/S/D to move, Q to quit.\n");
draw_block();
// 稍微延迟一下
Sleep(50);
}
// 游戏结束
system("cls");
printf("Game over.\n");
printf("Score: %d\n", score);
printf("Press any key to exit.\n");
getch();
return 0;
}
```
阅读全文