你能提供一个俄罗斯方块的c语言代码吗?
时间: 2024-03-21 21:42:33 浏览: 149
当然可以!下面是一个简单的俄罗斯方块游戏的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define ROWS 20
#define COLS 10
int board[ROWS][COLS] = {0}; // 游戏面板
int shape[7][4][4] = { // 七种俄罗斯方块的四个状态
{ {0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0} },
{ {0,0,0,0},
{0,2,2,0},
{0,2,2,0},
{0,0,0,0} },
{ {0,0,0,0},
{0,3,0,0},
{3,3,3,0},
{0,0,0,0} },
{ {0,0,0,0},
{0,0,4,4},
{0,4,4,0},
{0,0,0,0} },
{ {0,0,0,0},
{0,5,5,0},
{0,0,5,5},
{0,0,0,0} },
{ {0,0,0,0},
{0,6,6,6},
{0,0,6,0},
{0,0,0,0} },
{ {0,0,0,0},
{0,0,7,0},
{0,7,7,7},
{0,0,0,0} }
};
int cur_shape[4][4] = {0}; // 当前俄罗斯方块的状态
int cur_row = 0; // 当前俄罗斯方块的行坐标
int cur_col = 0; // 当前俄罗斯方块的列坐标
int score = 0; // 当前得分
// 随机生成一个俄罗斯方块
void generate_shape() {
int type = rand() % 7;
int state = rand() % 4;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cur_shape[i][j] = shape[type][state][i][j];
}
}
cur_row = 0;
cur_col = COLS / 2 - 2;
}
// 在游戏面板上显示当前俄罗斯方块
void show_shape() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
board[cur_row+i][cur_col+j] = cur_shape[i][j];
}
}
}
}
// 从游戏面板上清除当前俄罗斯方块
void clear_shape() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
board[cur_row+i][cur_col+j] = 0;
}
}
}
}
// 判断当前俄罗斯方块是否能够向下移动
int can_move_down() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
if (cur_row+i == ROWS-1 || board[cur_row+i+1][cur_col+j] != 0) {
return 0;
}
}
}
}
return 1;
}
// 判断当前俄罗斯方块是否能够向左移动
int can_move_left() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
if (cur_col+j == 0 || board[cur_row+i][cur_col+j-1] != 0) {
return 0;
}
}
}
}
return 1;
}
// 判断当前俄罗斯方块是否能够向右移动
int can_move_right() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
if (cur_col+j == COLS-1 || board[cur_row+i][cur_col+j+1] != 0) {
return 0;
}
}
}
}
return 1;
}
// 判断当前俄罗斯方块是否能够旋转
int can_rotate() {
int next_shape[4][4] = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
next_shape[i][j] = cur_shape[3-j][i];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (next_shape[i][j] != 0) {
int row = cur_row+i;
int col = cur_col+j;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || board[row][col] != 0) {
return 0;
}
}
}
}
return 1;
}
// 将当前俄罗斯方块固定在游戏面板上
void fix_shape() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (cur_shape[i][j] != 0) {
board[cur_row+i][cur_col+j] = cur_shape[i][j];
}
}
}
}
// 消除已经填满的行
void clear_rows() {
int count = 0;
for (int i = ROWS-1; i >= 0; i--) {
int full = 1;
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
full = 0;
break;
}
}
if (full) {
count++;
for (int k = i; k > 0; k--) {
for (int j = 0; j < COLS; j++) {
board[k][j] = board[k-1][j];
}
}
for (int j = 0; j < COLS; j++) {
board[0][j] = 0;
}
i++;
}
}
score += count * 100;
}
// 显示游戏面板
void show_board() {
system("cls");
printf("Score: %d\n", score);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
printf(".");
}
else {
printf("*");
}
}
printf("\n");
}
}
int main() {
srand((unsigned)time(NULL));
generate_shape();
while (1) {
show_board();
if (kbhit()) {
char c = getch();
if (c == 'a' && can_move_left()) {
clear_shape();
cur_col--;
show_shape();
}
else if (c == 'd' && can_move_right()) {
clear_shape();
cur_col++;
show_shape();
}
else if (c == 's' && can_move_down()) {
clear_shape();
cur_row++;
show_shape();
}
else if (c == 'w' && can_rotate()) {
clear_shape();
int next_shape[4][4] = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
next_shape[i][j] = cur_shape[3-j][i];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cur_shape[i][j] = next_shape[i][j];
}
}
show_shape();
}
else if (c == 'q') {
break;
}
}
else {
if (can_move_down()) {
clear_shape();
cur_row++;
show_shape();
}
else {
fix_shape();
clear_rows();
generate_shape();
if (!can_move_down()) {
break;
}
}
}
Sleep(100);
}
printf("Game Over!\n");
return 0;
}
```
这个程序使用了Windows系统中的 `<conio.h>` 和 `<windows.h>` 头文件来实现游戏的界面和操作。游戏面板和俄罗斯方块均使用二维数组来表示,每个方块的状态使用数字来表示。在游戏循环中,根据用户的输入或者定时器的触发来判断俄罗斯方块是否能够移动,旋转或者固定在游戏面板上。最后,游戏结束后输出游戏结果。
阅读全文