Linux内ubuntu利用ncurse实现俄罗斯方块简单代码,可输出执行文件
时间: 2024-03-18 08:39:55 浏览: 18
好的,以下是一个简单的使用ncurses库实现俄罗斯方块的代码,你可以编译生成可执行文件:
```c++
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 20
#define COLS 10
int block[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, 3, 0},
{3, 3, 0, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{4, 4, 0, 0},
{0, 4, 4, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{0, 5, 5, 0},
{5, 5, 0, 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},
{7, 7, 7, 0},
{7, 0, 0, 0},
{0, 0, 0, 0}}
};
int board[ROWS][COLS] = {0};
int cur_block[4][4] = {0};
int cur_row = 0, cur_col = 0, cur_type = 0;
int score = 0;
void init_board()
{
int i, j;
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
board[i][j] = 0;
}
}
}
void draw_block(int row, int col, int type)
{
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
if(block[type][i][j] != 0) {
mvprintw(row+i, col+j*2, "[]");
}
}
}
}
void erase_block(int row, int col, int type)
{
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
if(block[type][i][j] != 0) {
mvprintw(row+i, col+j*2, " ");
}
}
}
}
void draw_board()
{
int i, j;
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
if(board[i][j] != 0) {
mvprintw(i, j*2, "[]");
}
}
}
}
void new_block()
{
cur_row = 0;
cur_col = COLS / 2 - 2;
cur_type = rand() % 7;
memcpy(cur_block, block[cur_type], sizeof(cur_block));
}
int can_move(int row, int col)
{
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
if(cur_block[i][j] != 0) {
int r = row + i;
int c = col + j;
if(r < 0 || r >= ROWS || c < 0 || c >= COLS || board[r][c] != 0) {
return 0;
}
}
}
}
return 1;
}
void merge_block()
{
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
if(cur_block[i][j] != 0) {
int r = cur_row + i;
int c = cur_col + j;
board[r][c] = cur_block[i][j];
}
}
}
}
void check_full_row()
{
int i, j;
for(i = ROWS-1; i >= 0; i--) {
int full = 1;
for(j = 0; j < COLS; j++) {
if(board[i][j] == 0) {
full = 0;
break;
}
}
if(full) {
memcpy(board[1], board[0], COLS*sizeof(int));
memset(board[0], 0, COLS*sizeof(int));
score += 10;
}
}
}
void game_over()
{
erase_block(cur_row, cur_col, cur_type);
mvprintw(ROWS/2, COLS, "Game Over! Score: %d\n", score);
refresh();
getch();
}
void move_left()
{
if(can_move(cur_row, cur_col-1)) {
erase_block(cur_row, cur_col, cur_type);
cur_col--;
draw_block(cur_row, cur_col, cur_type);
}
}
void move_right()
{
if(can_move(cur_row, cur_col+1)) {
erase_block(cur_row, cur_col, cur_type);
cur_col++;
draw_block(cur_row, cur_col, cur_type);
}
}
void move_down()
{
if(can_move(cur_row+1, cur_col)) {
erase_block(cur_row, cur_col, cur_type);
cur_row++;
draw_block(cur_row, cur_col, cur_type);
} else {
merge_block();
check_full_row();
new_block();
if(!can_move(cur_row, cur_col)) {
game_over();
exit(0);
}
}
}
void rotate()
{
int new_block[4][4] = {0};
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
new_block[i][j] = cur_block[3-j][i];
}
}
if(can_move(cur_row, cur_col) && can_move(cur_row, cur_col+1)) {
erase_block(cur_row, cur_col, cur_type);
memcpy(cur_block, new_block, sizeof(cur_block));
draw_block(cur_row, cur_col, cur_type);
}
}
int main()
{
srand(time(NULL));
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
timeout(1000);
init_board();
new_block();
while(1) {
draw_board();
draw_block(cur_row, cur_col, cur_type);
char ch = getch();
switch(ch) {
case 'q':
endwin();
exit(0);
case KEY_LEFT:
move_left();
break;
case KEY_RIGHT:
move_right();
break;
case KEY_DOWN:
move_down();
break;
case ' ':
rotate();
break;
}
}
endwin();
return 0;
}
```
编译生成可执行文件的命令:
```
gcc -o tetris tetris.c -lncurses
```
运行可执行文件:
```
./tetris
```