c语言编写俄罗斯方块程序
时间: 2023-11-27 18:48:21 浏览: 198
以下是一个简单的C语言实现俄罗斯方块的程序,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define WIDTH 10
#define HEIGHT 20
int map[HEIGHT][WIDTH] = {0}; // 地图数组,0表示空,1表示有方块
int block[4][4] = {0}; // 方块数组,0表示空,1表示有方块
int block_x = 0, block_y = 0; // 方块左上角坐标
int score = 0; // 得分
// 画地图
void draw_map() {
system("cls"); // 清屏
printf("Score: %d\n", score);
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[i][j] == 0) {
printf(".");
} else {
printf("*");
}
}
printf("\n");
}
}
// 生成新方块
void new_block() {
int type = rand() % 7; // 随机生成方块类型
switch (type) {
case 0: // I型
block[0][1] = block[1][1] = block[2][1] = block[3][1] = 1;
break;
case 1: // J型
block[0][0] = block[1][0] = block[2][0] = 1;
block[2][1] = 1;
break;
case 2: // L型
block[0][1] = block[1][1] = block[2][1] = 1;
block[2][0] = 1;
break;
case 3: // O型
block[0][0] = block[0][1] = block[1][0] = block[1][1] = 1;
break;
case 4: // S型
block[1][0] = block[2][0] = 1;
block[0][1] = block[1][1] = 1;
break;
case 5: // T型
block[0][1] = block[1][0] = block[1][1] = block[1][2] = 1;
break;
case 6: // Z型
block[0][0] = block[1][0] = 1;
block[1][1] = block[2][1] = 1;
break;
}
block_x = WIDTH / 2 - 2; // 初始位置在中间偏左
block_y = 0;
}
// 显示方块
void show_block() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j] == 1) {
map[block_y + i][block_x + j] = 1;
}
}
}
}
// 消除满行
void clear_line() {
int full;
for (int i = HEIGHT - 1; i >= 0; i--) {
full = 1;
for (int j = 0; j < WIDTH; j++) {
if (map[i][j] == 0) {
full = 0;
break;
}
}
if (full) {
score += 10;
for (int k = i; k > 0; k--) {
for (int j = 0; j < WIDTH; j++) {
map[k][j] = map[k - 1][j];
}
}
i++; // 重新检查当前行
}
}
}
// 判断是否可以下落
int can_fall() {
for (int i = 3; i >= 0; i--) {
for (int j = 0; j < 4; j++) {
if (block[i][j] == 1) {
if (block_y + i == HEIGHT - 1 || map[block_y + i + 1][block_x + j] == 1) {
return 0;
}
}
}
}
return 1;
}
// 方块下落
void fall_block() {
if (can_fall()) {
block_y++;
} else {
show_block();
clear_line();
new_block();
}
}
// 方块左移
void move_left() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j] == 1) {
if (block_x + j == 0 || map[block_y + i][block_x + j - 1] == 1) {
return;
}
}
}
}
block_x--;
}
// 方块右移
void move_right() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[i][j] == 1) {
if (block_x + j == WIDTH - 1 || map[block_y + i][block_x + j + 1] == 1) {
return;
}
}
}
}
block_x++;
}
// 方块变形
void rotate_block() {
int temp[4][4] = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
temp[i][j] = block[i][j];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
block[i][j] = temp[3 - j][i];
}
}
if (block_x < 0) {
block_x = 0;
} else if (block_x > WIDTH - 4) {
block_x = WIDTH - 4;
}
}
int main() {
srand(time(NULL)); // 随机数种子
new_block();
while (1) {
draw_map();
if (_kbhit()) { // 检测键盘输入
char ch = _getch();
switch (ch) {
case 'a':
move_left();
break;
case 'd':
move_right();
break;
case 's':
fall_block();
break;
case 'w':
rotate_block();
break;
case 'q':
exit(0);
break;
}
}
fall_block();
Sleep(100); // 100毫秒下落一格
}
return 0;
}
```
阅读全文