用c语言做一个俄罗斯方块小游戏
时间: 2023-12-31 10:05:07 浏览: 71
c语言实现俄罗斯方块小游戏
以下是一个基于C语言的简单俄罗斯方块小游戏。代码中使用了Windows API库,因此只能在Windows平台上运行。
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 20
#define BLOCK_SIZE 4
int board[BOARD_HEIGHT][BOARD_WIDTH] = { 0 };
int current_block[BLOCK_SIZE][BLOCK_SIZE] = { 0 };
int current_x = 0, current_y = 0;
int score = 0;
void init_block() {
int blocks[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, 0, 3, 3 },
{ 0, 3, 3, 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 },
{ 0, 5, 5, 5 },
{ 0, 0, 5, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 6, 6, 6 },
{ 0, 6, 0, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 7, 7, 7 },
{ 0, 0, 0, 7 },
{ 0, 0, 0, 0 }
}
};
int block_id = rand() % 7;
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
current_block[i][j] = blocks[block_id][i][j];
}
}
current_x = BOARD_WIDTH / 2 - 2;
current_y = 0;
}
void draw_block(int x, int y) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (current_block[i][j] != 0) {
int px = x + j, py = y + i;
if (px >= 0 && px < BOARD_WIDTH && py >= 0 && py < BOARD_HEIGHT) {
board[py][px] = current_block[i][j];
}
}
}
}
}
void clear_block(int x, int y) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (current_block[i][j] != 0) {
int px = x + j, py = y + i;
if (px >= 0 && px < BOARD_WIDTH && py >= 0 && py < BOARD_HEIGHT) {
board[py][px] = 0;
}
}
}
}
}
void draw_board() {
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
printf("%c", board[i][j] == 0 ? ' ' : '*');
}
printf("\n");
}
printf("Score: %d\n", score);
}
void clear_board() {
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
board[i][j] = 0;
}
}
}
int check_collision(int x, int y) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (current_block[i][j] != 0) {
int px = x + j, py = y + i;
if (px < 0 || px >= BOARD_WIDTH || py >= BOARD_HEIGHT || (py >= 0 && board[py][px] != 0)) {
return 1;
}
}
}
}
return 0;
}
void clear_line(int y) {
for (int i = y; i > 0; i--) {
for (int j = 0; j < BOARD_WIDTH; j++) {
board[i][j] = board[i - 1][j];
}
}
score += 10;
}
void check_lines() {
for (int i = BOARD_HEIGHT - 1; i >= 0; i--) {
int line_complete = 1;
for (int j = 0; j < BOARD_WIDTH; j++) {
if (board[i][j] == 0) {
line_complete = 0;
break;
}
}
if (line_complete) {
clear_line(i);
i++;
}
}
}
int main() {
srand(time(NULL));
clear_board();
init_block();
while (1) {
draw_board();
if (check_collision(current_x, current_y + 1)) {
draw_block(current_x, current_y);
check_lines();
init_block();
if (check_collision(current_x, current_y)) {
break;
}
}
else {
clear_block(current_x, current_y);
current_y++;
}
Sleep(100);
system("cls");
}
printf("Game over. Score: %d\n", score);
return 0;
}
```
这个游戏使用10x20的方格作为游戏区域,每次随机生成一个方块并放在游戏区域的顶部,玩家可以使用键盘上的方向键来移动和旋转方块,当方块落到底部或者碰到已有的方块时,就会停止下落,玩家可以继续操作下一个方块。当一行方块被填满时,该行将被消除,并获得相应的分数。当方块无法放置到游戏区域中时,游戏结束。
阅读全文