使用C语言写一个俄罗斯方块
时间: 2024-01-01 22:03:14 浏览: 68
以下是使用 C 语言实现俄罗斯方块的代码,主要使用了 ncurses 库进行图形化界面的绘制:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>
#define ROW 20
#define COL 10
// 俄罗斯方块的七种形状
const int shapes[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}
}
};
// 方块的颜色
const int colors[7] = {
COLOR_CYAN,
COLOR_BLUE,
COLOR_MAGENTA,
COLOR_YELLOW,
COLOR_GREEN,
COLOR_RED,
COLOR_WHITE
};
// 用于存储方块的位置和颜色
int block[4][4];
int color;
// 用于存储方块的位置
int row, col;
// 用于存储已经下落的方块
int board[ROW][COL];
// 初始化 ncurses 库
void init_ncurses() {
initscr();
cbreak();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
start_color();
}
// 初始化游戏界面
void init_game() {
clear();
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
board[i][j] = -1;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = 0;
}
}
row = 0;
col = COL / 2 - 2;
int index = rand() % 7;
color = colors[index];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = shapes[index][i][j];
}
}
}
// 绘制游戏界面
void draw_game() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j] != -1) {
attron(COLOR_PAIR(board[i][j]));
mvprintw(i, j * 2, " ");
attroff(COLOR_PAIR(board[i][j]));
} else {
mvprintw(i, j * 2, " ");
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j]) {
attron(COLOR_PAIR(color));
mvprintw(row + i, (col + j) * 2, " ");
attroff(COLOR_PAIR(color));
}
}
}
refresh();
}
// 判断方块是否可以下落
int can_move_down() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j]) {
int x = row + i + 1;
int y = col + j;
if (x >= ROW || board[x][y] != -1) {
return 0;
}
}
}
}
return 1;
}
// 把方块放到底部
void put_block() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j]) {
board[row + i][col + j] = color;
}
}
}
}
// 消除满行
void eliminate_lines() {
for (int i = ROW - 1; i >= 0; i--) {
int flag = 1;
for (int j = 0; j < COL; j++) {
if (board[i][j] == -1) {
flag = 0;
break;
}
}
if (flag) {
for (int j = i; j > 0; j--) {
for (int k = 0; k < COL; k++) {
board[j][k] = board[j - 1][k];
}
}
i++;
}
}
}
int main() {
srand(time(NULL));
init_ncurses();
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
while (1) {
init_game();
while (1) {
draw_game();
timeout(500);
int ch = getch();
if (ch == KEY_LEFT) {
int flag = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j]) {
int x = row + i;
int y = col + j - 1;
if (y < 0 || board[x][y] != -1) {
flag = 0;
break;
}
}
}
}
if (flag) {
col--;
}
} else if (ch == KEY_RIGHT) {
int flag = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j]) {
int x = row + i;
int y = col + j + 1;
if (y >= COL || board[x][y] != -1) {
flag = 0;
break;
}
}
}
}
if (flag) {
col++;
}
} else if (ch == KEY_DOWN) {
if (can_move_down()) {
row++;
} else {
put_block();
eliminate_lines();
break;
}
} else if (ch == KEY_UP) {
int tmp[4][4];
int cnt = 0;
for (int j = 3; j >= 0; j--) {
for (int i = 0; i < 4; i++) {
tmp[i][cnt] = block[j][i];
}
cnt++;
}
int flag = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (tmp[i][j]) {
int x = row + i;
int y = col + j;
if (x >= ROW || y < 0 || y >= COL || board[x][y] != -1) {
flag = 0;
break;
}
}
}
}
if (flag) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = tmp[i][j];
}
}
}
} else if (ch == 'q' || ch == 'Q') {
endwin();
return 0;
}
if (can_move_down()) {
row++;
} else {
put_block();
eliminate_lines();
break;
}
}
}
endwin();
return 0;
}
```
说明:此代码只是一个简单的实现,可能存在一些不足之处。
阅读全文