俄罗斯方块代码c语言
时间: 2023-12-19 19:31:03 浏览: 43
以下是一个简单的C语言实现俄罗斯方块游戏的代码,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define WIDTH 10 // 地图宽度
#define HEIGHT 20 // 地图高度
#define SIZE 4 // 方块大小
// 方块类型
enum BlockType {
I_BLOCK,
J_BLOCK,
L_BLOCK,
O_BLOCK,
S_BLOCK,
T_BLOCK, Z_BLOCK
};
// 方块结构体
struct Block {
int x[SIZE]; // 方块的x坐标
int y[SIZE]; // 方块的y坐标
int type; // 方块类型
int dir; // 方块的方向(0-3)
};
// 地图数组
int map[HEIGHT][WIDTH];
// 方块数组
struct Block blocks[] = {
{ {0, 1, 2, 3}, {0, 0, 0, 0}, I_BLOCK, 0 },
{ {0, 1, 2, 2}, {0, 0, 0, 1}, J_BLOCK, 0 },
{ {0, 1, 2, 2}, {0, 0, 0, 1}, L_BLOCK, 0 },
{ {0, 1, 0, 1}, {0, 0, 1, 1}, O_BLOCK, 0 },
{ {1, 2, 0, 1}, {0, 0, 1, 1}, S_BLOCK, 0 },
{ {0, 1, 2, 1}, {0, 0, 0, 1}, T_BLOCK, 0 },
{ {0, 1, 1, 2}, {0, 0, 1, 1}, Z_BLOCK, 0 }
};
// 随机生成一个方块
struct Block generateBlock() {
struct Block block = blocks[rand() % 7];
for (int i = 0; i < SIZE; i++) {
block.x[i] += WIDTH / 2 - 1;
block.y[i] += 1;
}
return block;
}
// 判断方块是否可以移动
int canMove(struct Block block, int dx, int dy) {
for (int i = 0; i < SIZE; i++) {
int x = block.x[i] + dx;
int y = block.y[i] + dy;
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || map[y][x]) {
return 0;
}
}
return 1;
}
// 移动方块
void moveBlock(struct Block *block, int dx, int dy) {
if (canMove(*block, dx, dy)) {
for (int i = 0; i < SIZE; i++) {
block->x[i] += dx;
block->y[i] += dy;
}
}
}
// 旋转方块
void rotateBlock(struct Block *block) {
int dir = block->dir;
block->dir = (dir + 1) % 4;
int x = block->x[1];
int y = block->y[1];
int dx, dy;
for (int i = 0; i < SIZE; i++) {
dx = block->x[i] - x;
dy = block->y[i] - y;
block->x[i] = x + dy;
block->y[i] = y - dx;
}
if (!canMove(*block, 0, 0)) {
block->dir = dir;
for (int i = 0; i < SIZE; i++) {
dx = block->x[i] - x;
dy = block->y[i] - y;
block->x[i] = x - dy;
block->y[i] = y + dx;
}
}
}
// 将方块放入地图
void putBlock(struct Block block) {
for (int i = 0; i < SIZE; i++) {
map[block.y[i]][block.x[i]] = block.type + 1;
}
}
// 消除满行
void clearLines() {
int count = 0;
for (int i = HEIGHT - 1; i >= 0; i--) {
int flag = 1;
for (int j = 0; j < WIDTH; j++) {
if (!map[i][j]) {
flag = 0;
break;
}
}
if (flag) {
count++;
for (int k = i; k > 0; k--) {
for (int j = 0; j < WIDTH; j++) {
map[k][j] = map[k - 1][j];
}
}
for (int j = 0; j < WIDTH; j++) {
map[0][j] = 0;
}
i++;
}
}
}
// 绘制地图
void drawMap() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[i][j]) {
printf("[]");
} else {
printf(" ");
}
}
printf("\n");
}
}
int main() {
srand(time(NULL));
struct Block block = generateBlock();
int score = 0;
while (1) {
drawMap();
if (_kbhit()) {
char ch = _getch();
switch (ch) {
case 'a':
moveBlock(&block, -1, 0);
break;
case 'd':
moveBlock(&block, 1, 0);
break;
case 's':
moveBlock(&block, 0, 1);
break;
case 'w':
rotateBlock(&block);
break;
case 'q':
exit(0);
break;
}
}
if (!canMove(block, 0, 1)) {
putBlock(block);
clearLines();
score += 10;
block = generateBlock();
if (!canMove(block, 0, 0)) {
break;
}
} else {
moveBlock(&block, 0, 1);
}
Sleep(100);
}
printf("Game over! Your score is %d.\n", score);
return 0;
}
```