用linux写一个俄罗斯方块的代码
时间: 2023-03-31 07:02:25 浏览: 65
你好,我可以回答这个问题。以下是用 C 语言编写俄罗斯方块的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#define ROWS 20
#define COLS 10
int board[ROWS][COLS] = {};
int shapes[7][4][4][2] = {
{
{{, 1}, {, }, {1, }, {1, 1}},
{{, 1}, {, }, {1, }, {1, 1}},
{{, 1}, {, }, {1, }, {1, 1}},
{{, 1}, {, }, {1, }, {1, 1}}
},
{
{{, }, {, 1}, {, 2}, {, 3}},
{{, }, {1, }, {2, }, {3, }},
{{, }, {, 1}, {, 2}, {, 3}},
{{, }, {1, }, {2, }, {3, }}
},
{
{{, }, {, 1}, {1, 1}, {1, 2}},
{{, 1}, {1, }, {1, 1}, {2, }},
{{, }, {, 1}, {1, 1}, {1, 2}},
{{, 1}, {1, }, {1, 1}, {2, }}
},
{
{{, 2}, {, 1}, {, }, {1, }},
{{, }, {1, }, {2, }, {2, 1}},
{{, 2}, {, 1}, {, }, {1, }},
{{, }, {1, }, {2, }, {2, 1}}
},
{
{{, }, {, 1}, {1, 1}, {1, 2}},
{{, 1}, {1, }, {1, 1}, {2, }},
{{, }, {, 1}, {1, 1}, {1, 2}},
{{, 1}, {1, }, {1, 1}, {2, }}
},
{
{{, 1}, {, 2}, {1, }, {1, 1}},
{{, }, {1, }, {1, 1}, {2, 1}},
{{, 1}, {, 2}, {1, }, {1, 1}},
{{, }, {1, }, {1, 1}, {2, 1}}
},
{
{{, }, {, 1}, {, 2}, {1, 1}},
{{, 1}, {1, }, {1, 1}, {2, 1}},
{{, 1}, {1, }, {1, 1}, {1, 2}},
{{, 1}, {1, 1}, {1, 2}, {2, 1}}
}
};
int current_shape[4][2];
int current_row, current_col, current_type;
int get_key() {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
void print_board() {
int i, j;
for (i = ; i < ROWS; i++) {
for (j = ; j < COLS; j++) {
if (board[i][j] == ) {
printf(".");
} else {
printf("*");
}
}
printf("\n");
}
}
void print_shape() {
int i, j;
for (i = ; i < 4; i++) {
for (j = ; j < 4; j++) {
if (current_shape[i][j] == ) {
printf(".");
} else {
printf("*");
}
}
printf("\n");
}
}
void generate_shape() {
current_type = rand() % 7;
int i, j;
for (i = ; i < 4; i++) {
for (j = ; j < 2; j++) {
current_shape[i][j] = shapes[current_type][][i][j];
}
}
current_row = ;
current_col = COLS / 2 - 2;
}
int can_move(int row, int col, int type) {
int i, new_row, new_col;
for (i = ; i < 4; i++) {
new_row = row + shapes[type][i][][];
new_col = col + shapes[type][i][][1];
if (new_row < || new_row >= ROWS || new_col < || new_col >= COLS || board[new_row][new_col] != ) {
return ;
}
}
return 1;
}
void move_shape(int row, int col, int type) {
int i, new_row, new_col;
for (i = ; i < 4; i++) {
new_row = row + shapes[type][i][][];
new_col = col + shapes[type][i][][1];
current_shape[i][] = new_row;
current_shape[i][1] = new_col;
}
current_row = row;
current_col = col;
current_type = type;
}
void rotate_shape() {
int new_type = (current_type + 1) % 4;
if (can_move(current_row, current_col, new_type)) {
move_shape(current_row, current_col, new_type);
}
}
void drop_shape() {
int i, new_row = current_row + 1;
if (can_move(new_row, current_col, current_type)) {
move_shape(new_row, current_col, current_type);
} else {
for (i = ; i < 4; i++) {
board[current_shape[i][]][current_shape[i][1]] = 1;
}
generate_shape();
}
}
void move_left() {
int new_col = current_col - 1;
if (can_move(current_row, new_col, current_type)) {
move_shape(current_row, new_col, current_type);
}
}
void move_right() {
int new_col = current_col + 1;
if (can_move(current_row, new_col, current_type)) {
move_shape(current_row, new_col, current_type);
}
}
int main() {
srand(time(NULL));
generate_shape();
while (1) {
system("clear");
print_board();
print_shape();
usleep(100000);
int key = get_key();
switch (key) {
case 'q':
exit();
case 'a':
move_left();
break;
case 'd':
move_right();
break;
case 's':
drop_shape();
break;
case 'w':
rotate_shape();
break;
}
}
return ;
}