C语言写一个俄罗斯方块
时间: 2023-12-29 15:45:39 浏览: 64
C语言写的俄罗斯方块
5星 · 资源好评率100%
俄罗斯方块是一种经典的游戏,使用C语言实现这个游戏需要一些基本的编程知识和技巧,下面是一个简单的代码实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
int brick[7][4][4] = {
{{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}}, // I
{{0, 0, 0, 0}, {0, 2, 2, 0}, {0, 2, 2, 0}, {0, 0, 0, 0}}, // O
{{0, 0, 0, 0}, {0, 3, 3, 0}, {3, 3, 0, 0}, {0, 0, 0, 0}}, // Z
{{0, 0, 0, 0}, {4, 4, 0, 0}, {0, 4, 4, 0}, {0, 0, 0, 0}}, // S
{{0, 0, 0, 0}, {5, 5, 5, 0}, {0, 0, 5, 0}, {0, 0, 0, 0}}, // L
{{0, 0, 0, 0}, {6, 6, 6, 0}, {6, 0, 0, 0}, {0, 0, 0, 0}}, // J
{{0, 0, 0, 0}, {0, 7, 0, 0}, {7, 7, 7, 0}, {0, 0, 0, 0}} // T
};
int board[HEIGHT][WIDTH] = {0};
int cur_brick[4][4];
int cur_x, cur_y, cur_type;
int score = 0;
void init()
{
srand((unsigned)time(NULL));
cur_type = rand() % 7;
memcpy(cur_brick, brick[cur_type], sizeof(cur_brick));
cur_x = WIDTH / 2 - 2;
cur_y = 0;
}
int check(int x, int y)
{
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (cur_brick[i][j] && (x + j >= WIDTH || x + j < 0 || y + i >= HEIGHT || (board[y + i][x + j]))) {
return 0;
}
}
}
return 1;
}
void draw_brick(int x, int y, int t)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (cur_brick[i][j]) {
SetConsoleCursorPosition(handle, (COORD){2 * (x + j), y + i});
printf("\033[1;%dm%c\033[0m", t, '#');
}
}
}
}
void draw_board()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
if (board[i][j]) {
SetConsoleCursorPosition(handle, (COORD){2 * j, i});
printf("\033[1;%dm%c\033[0m", board[i][j], '#');
}
}
}
}
void update_board()
{
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (cur_brick[i][j]) {
board[cur_y + i][cur_x + j] = cur_type + 1;
}
}
}
}
void remove_lines()
{
int lines = 0;
for (int i = HEIGHT - 1; i >= 0; --i) {
int full = 1;
for (int j = 0; j < WIDTH; ++j) {
if (!board[i][j]) {
full = 0;
break;
}
}
if (full) {
++lines;
for (int k = i; k > 0; --k) {
memcpy(board[k], board[k - 1], sizeof(board[k]));
}
memset(board[0], 0, sizeof(board[0]));
++i;
}
}
score += lines * lines;
}
int main()
{
system("cls");
init();
while (1) {
if (kbhit()) {
int ch = getch();
if (ch == 'a' || ch == 'A') {
if (check(cur_x - 1, cur_y)) {
--cur_x;
}
} else if (ch == 'd' || ch == 'D') {
if (check(cur_x + 1, cur_y)) {
++cur_x;
}
} else if (ch == 's' || ch == 'S') {
if (check(cur_x, cur_y + 1)) {
++cur_y;
}
} else if (ch == 'w' || ch == 'W') {
int type = (cur_type + 1) % 7;
int bak[4][4];
memcpy(bak, cur_brick, sizeof(bak));
memcpy(cur_brick, brick[type], sizeof(cur_brick));
if (!check(cur_x, cur_y)) {
memcpy(cur_brick, bak, sizeof(cur_brick));
} else {
cur_type = type;
}
}
}
if (check(cur_x, cur_y + 1)) {
++cur_y;
} else {
update_board();
remove_lines();
init();
}
system("cls");
draw_board();
draw_brick(cur_x, cur_y, cur_type + 1);
printf("\nScore: %d", score);
Sleep(100);
}
return 0;
}
```
这个游戏的界面使用了控制台来显示,在Windows平台下可以直接使用,如果在其他平台上运行需要修改一些代码。
阅读全文