俄罗斯方块游戏C语言代码
时间: 2023-08-27 11:06:53 浏览: 42
以下是一个简单的俄罗斯方块游戏的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 12
#define HEIGHT 22
int board[HEIGHT][WIDTH] = {0};
int score = 0;
int shapes[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},
{3,3,0,0},
{0,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},
{5,5,5,0},
{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},
{7,7,7,0},
{0,7,0,0},
{0,0,0,0}
}
};
int cur_x = 0;
int cur_y = 0;
int cur_shape = 0;
int cur_rotation = 0;
void print_board() {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
printf("%c", board[i][j] == 0 ? '.' : '#');
}
printf("\n");
}
}
void clear_board() {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
}
void draw_shape(int x, int y, int shape, int rotation) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(shapes[shape][i][j] != 0) {
board[y+i][x+j] = shapes[shape][i][(j+rotation)%4];
}
}
}
}
void erase_shape(int x, int y, int shape, int rotation) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(shapes[shape][i][j] != 0) {
board[y+i][x+j] = 0;
}
}
}
}
int check_collision(int x, int y, int shape, int rotation) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int si = y+i;
int sj = x+j;
if(si >= HEIGHT || sj < 0 || sj >= WIDTH) {
if(shapes[shape][i][j] != 0) {
return 1;
}
} else if(si >= 0) {
if(board[si][sj] != 0 && shapes[shape][i][j] != 0) {
return 1;
}
}
}
}
return 0;
}
void rotate_shape(int *rotation) {
erase_shape(cur_x, cur_y, cur_shape, *rotation);
*rotation = (*rotation + 1) % 4;
if(check_collision(cur_x, cur_y, cur_shape, *rotation)) {
*rotation = (*rotation + 3) % 4;
}
draw_shape(cur_x, cur_y, cur_shape, *rotation);
}
void move_shape(int x, int y) {
erase_shape(cur_x, cur_y, cur_shape, cur_rotation);
cur_x += x;
cur_y += y;
if(check_collision(cur_x, cur_y, cur_shape, cur_rotation)) {
cur_x -= x;
cur_y -= y;
}
draw_shape(cur_x, cur_y, cur_shape, cur_rotation);
}
int drop_shape() {
int collided = 0;
while(!collided) {
move_shape(0, 1);
if(check_collision(cur_x, cur_y, cur_shape, cur_rotation)) {
collided = 1;
}
}
return 0;
}
void fix_shape() {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(shapes[cur_shape][i][j] != 0) {
board[cur_y+i][cur_x+j] = shapes[cur_shape][i][j];
}
}
}
}
void remove_lines() {
int lines_cleared = 0;
for(int i = HEIGHT-1; i >= 0; i--) {
int full = 1;
for(int j = 0; j < WIDTH; j++) {
if(board[i][j] == 0) {
full = 0;
break;
}
}
if(full) {
for(int k = i-1; k >= 0; k--) {
for(int j = 0; j < WIDTH; j++) {
board[k+1][j] = board[k][j];
}
}
i++;
lines_cleared++;
}
}
if(lines_cleared == 1) {
score += 100;
} else if(lines_cleared == 2) {
score += 300;
} else if(lines_cleared == 3) {
score += 500;
} else if(lines_cleared == 4) {
score += 800;
}
}
int game_over() {
for(int j = 0; j < WIDTH; j++) {
if(board[0][j] != 0) {
return 1;
}
}
return 0;
}
int main() {
srand(time(NULL));
clear_board();
while(!game_over()) {
cur_shape = rand() % 7;
cur_rotation = 0;
cur_x = WIDTH/2 - 2;
cur_y = 0;
if(check_collision(cur_x, cur_y, cur_shape, cur_rotation)) {
break;
}
draw_shape(cur_x, cur_y, cur_shape, cur_rotation);
while(!check_collision(cur_x, cur_y+1, cur_shape, cur_rotation)) {
move_shape(0, 1);
}
fix_shape();
remove_lines();
system("cls");
printf("Score: %d\n", score);
print_board();
Sleep(500);
}
printf("Game Over!\n");
printf("Score: %d\n", score);
return 0;
}
```
这个示例代码使用了Windows API的Sleep函数来实现延迟,需要在Windows系统上运行。如果想要在其他系统上运行,可以使用标准C库的sleep函数代替。注意,这个示例代码并不完整,仅供参考。