c语言 俄罗斯方块代码
时间: 2023-08-12 15:41:38 浏览: 212
俄罗斯方块
以下是一个基于C语言的简单俄罗斯方块代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
int score = 0; // 记录得分
int map[HEIGHT][WIDTH] = { 0 }; // 地图
int x = 0, y = 0; // 方块左上角坐标
int block[4][4] = { 0 }; // 当前方块
int next[4][4] = { 0 }; // 下一个方块
int blockType = 0; // 当前方块类型
int nextType = 0; // 下一个方块类型
// 初始化方块
void initBlock() {
int types[7][4][4] = {
{ // I
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 }
},
{ // J
{ 0, 0, 0, 0 },
{ 1, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
},
{ // L
{ 0, 0, 0, 0 },
{ 1, 1, 1, 0 },
{ 1, 0, 0, 0 },
{ 0, 0, 0, 0 }
},
{ // O
{ 0, 0, 0, 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 },
{ 0, 0, 0, 0 }
},
{ // S
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 1, 1, 0, 0 },
{ 0, 0, 0, 0 }
},
{ // T
{ 0, 0, 0, 0 },
{ 1, 1, 1, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 0 }
},
{ // Z
{ 0, 0, 0, 0 },
{ 1, 1, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
}
};
blockType = nextType;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = next[i][j];
}
}
nextType = rand() % 7;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
next[i][j] = types[nextType][i][j];
}
}
}
// 判断是否可以移动或旋转方块
int check(int offsetX, int offsetY, int newBlock[4][4]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (newBlock[i][j] == 1) {
int newX = x + offsetX + j;
int newY = y + offsetY + i;
if (newX < 0 || newX >= WIDTH || newY < 0 || newY >= HEIGHT) {
return 0;
}
if (map[newY][newX] == 1) {
return 0;
}
}
}
}
return 1;
}
// 将方块放置到地图上
void setBlock() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j] == 1) {
map[y + i][x + j] = 1;
}
}
}
}
// 消除满行
void clearLine() {
for (int i = HEIGHT - 1; i >= 0; i--) {
int j = 0;
while (j < WIDTH && map[i][j] == 1) {
j++;
}
if (j == WIDTH) {
score++;
for (int k = i; k > 0; k--) {
for (int l = 0; l < WIDTH; l++) {
map[k][l] = map[k - 1][l];
}
}
i++;
}
}
}
// 绘制界面
void draw() {
system("cls");
printf("Score: %d\n", score);
printf("Next:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (next[i][j] == 1) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
printf("\n");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[i][j] == 1) {
printf("* ");
} else if (i >= y && i < y + 4 && j >= x && j < x + 4 && block[i - y][j - x] == 1) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
}
int main() {
srand((unsigned int)time(NULL));
initBlock();
while (1) {
draw();
Sleep(500);
if (check(0, 1, block)) {
y++;
} else {
setBlock();
clearLine();
x = 0;
y = 0;
initBlock();
if (!check(0, 0, block)) {
break;
}
}
if (_kbhit()) {
char c = _getch();
switch (c) {
case 'a':
if (check(-1, 0, block)) {
x--;
}
break;
case 'd':
if (check(1, 0, block)) {
x++;
}
break;
case 's':
if (check(0, 1, block)) {
y++;
}
break;
case 'w':
{
int newBlock[4][4] = { 0 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
newBlock[i][j] = block[3 - j][i];
}
}
if (check(0, 0, newBlock)) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = newBlock[i][j];
}
}
}
}
break;
default:
break;
}
}
}
printf("Game Over! Score: %d\n", score);
return 0;
}
```
这段代码基于控制台,使用了Windows.h库的Sleep函数和conio.h库的_kbhit、_getch函数。运行后,使用方向键和wasd键控制方块移动,按下空格键可以将方块直接放到底部。
阅读全文