用C語言實(shí)現(xiàn)俄羅斯方塊小游戲,內(nèi)附完整代碼。
```cint board[ROWS][COLS] = {0}; // 游戲板int current[4][2] = {0}; // 當(dāng)前方塊int current_type = 0; // 當(dāng)前方塊類型int current_row = 0; // 當(dāng)前方塊所在行int current_col = 0; // 當(dāng)前方塊所在列int score = 0; // 得分// 方塊類型及其形態(tài)int types[7][4][2] = {{{0, 0}, {0, 1}, {1, 0}, {1, 1}}, // 正方形{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, // 一字型{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, // L型{{0, 0}, {0, 1}, {0, 2}, {-1, 2}}, // 反L型{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, // Z型{{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, // 反Z型{{0, 0}, {0, 1}, {0, 2}, {1, 1}} // T型};// 在游戲板上繪制方塊void draw_block(int row, int col, int type) {for (int i = 0; i < 4; i++) {int r = row + types[type][i][0];int c = col + types[type][i][1];if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {board[r][c] = 1;}}}// 從游戲板上移除方塊void remove_block(int row, int col, int type) {for (int i = 0; i < 4; i++) {int r = row + types[type][i][0];int c = col + types[type][i][1];if (r >= 0 && r < ROWS && c >= 0 && c < COLS) {board[r][c] = 0;}}}// 判斷方塊是否可以下落int can_fall(int row, int col, int type) {for (int i = 0; i < 4; i++) {int r = row + types[type][i][0] + 1;int c = col + types[type][i][1];if (r >= ROWS || board[r][c]) {return 0;}}return 1;}// 判斷方塊是否可以左移int can_move_left(int row, int col, int type) {for (int i = 0; i < 4; i++) {int r = row + types[type][i][0];int c = col + types[type][i][1] - 1;if (c < 0 || board[r][c]) {return 0;}}return 1;}// 判斷方塊是否可以右移int can_move_right(int row, int col, int type) {for (int i = 0; i < 4; i++) {int r = row + types[type][i][0];int c = col + types[type][i][1] + 1;if (c >= COLS || board[r][c]) {return 0;}}return 1;}// 將當(dāng)前方塊固定在游戲板上void fix_block() {draw_block(current_row, current_col, current_type);current_type = rand() % 7;current_row = 0;current_col = COLS / 2 - 2;for (int i = 0; i < 4; i++) {current[i][0] = types[current_type][i][0];current[i][1] = types[current_type][i][1];}}// 判斷是否有一行已滿int is_full(int row) {for (int i = 0; i < COLS; i++) {if (board[row][i] == 0) {return 0;}}return 1;}// 將游戲板上的一行刪除void remove_row(int row) {for (int i = row; i > 0; i--) {for (int j = 0; j < COLS; j++) {board[i][j] = board[i - 1][j];}}for (int j = 0; j < COLS; j++) {board[0][j] = 0;}score++;}// 顯示游戲板和當(dāng)前方塊void display() {system("cls");for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {if (board[i][j]) {printf("*");} else if (i >= current_row && i < current_row + 4 && j >= current_col && j < current_col + 4 && current[i - current_row][j - current_col]) {printf("*");} else {printf(" ");}}printf("\n");}printf("Score: %d\n", score);}int main() {srand(time(NULL));fix_block();while (1) {display();if (kbhit()) {char c = getch();if (c == 'a' && can_move_left(current_row, current_col, current_type)) {remove_block(current_row, current_col, current_type);current_col--;} else if (c == 'd' && can_move_right(current_row, current_col, current_type)) {remove_block(current_row, current_col, current_type);current_col++;} else if (c == 's' && can_fall(current_row, current_col, current_type)) {remove_block(current_row, current_col, current_type);current_row++;} else if (c == ' ') {while (can_fall(current_row, current_col, current_type)) {remove_block(current_row, current_col, current_type);current_row++;}fix_block();}}if (can_fall(current_row, current_col, current_type)) {remove_block(current_row, current_col, current_type);current_row++;} else {fix_block();for (int i = 0; i < ROWS; i++) {if (is_full(i)) {remove_row(i);}}}}return 0;}```
評(píng)論
圖片
表情
