用c语言写一个俄罗斯方块代码
时间: 2023-05-24 19:02:55 浏览: 67
用C语言写的俄罗斯方块代码
4星 · 用户满意度95%
A:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define LENGTH 14
#define WIDTH 20
#define SHAPE_NUM 7
#define SHAPE_SIZE 4
const int shapes[SHAPE_NUM][SHAPE_SIZE][SHAPE_SIZE] = {
{
{0,0,0,0},
{1,1,1,1},
{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,0,0,0},
{0,3,3,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},
{0,5,5,0},
{5,5,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,6,0,0},
{6,6,6,0},
{0,0,0,0}
},
{
{0,0,0,0},
{7,7,0,0},
{0,7,7,0},
{0,0,0,0}
}
};
int board[WIDTH][LENGTH] = {0};
int curr_shape[SHAPE_SIZE][SHAPE_SIZE] = {0};
int curr_pos_x = 0;
int curr_pos_y = 0;
int curr_shape_index = 0;
void print_board() {
for (int i = 0; i < LENGTH; i++) {
for (int j = 0; j < WIDTH; j++) {
int val = board[j][i];
if (val == 0) {
printf(".");
} else {
printf("%d", val);
}
}
printf("\n");
}
}
void load_next_shape() {
curr_shape_index = rand() % SHAPE_NUM;
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
curr_shape[i][j] = shapes[curr_shape_index][i][j];
}
}
curr_pos_x = LENGTH / 2 - 2;
curr_pos_y = 0;
}
int piece_fits_at(int x, int y, int rot) {
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
if (curr_shape[i][j] != 0) {
int nx = x + j;
int ny = y + i;
if (nx < 0 || nx >= LENGTH || ny >= WIDTH) {
return 0;
}
if (ny >= 0 && board[nx][ny] != 0) {
return 0;
}
}
}
}
return 1;
}
void put_piece() {
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
if (curr_shape[i][j] != 0) {
board[curr_pos_x + j][curr_pos_y + i] = curr_shape_index + 1;
}
}
}
curr_shape_index = -1;
}
void remove_piece() {
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
if (curr_shape[i][j] != 0) {
board[curr_pos_x + j][curr_pos_y + i] = 0;
}
}
}
}
void rotate_shape() {
int temp[SHAPE_SIZE][SHAPE_SIZE] = {0};
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
temp[i][j] = curr_shape[i][j];
}
}
for (int i = 0; i < SHAPE_SIZE; i++) {
for (int j = 0; j < SHAPE_SIZE; j++) {
curr_shape[i][j] = temp[SHAPE_SIZE - j - 1][i];
}
}
}
void move_left() {
if (piece_fits_at(curr_pos_x - 1, curr_pos_y, 0)) {
remove_piece();
curr_pos_x--;
put_piece();
}
}
void move_right() {
if (piece_fits_at(curr_pos_x + 1, curr_pos_y, 0)) {
remove_piece();
curr_pos_x++;
put_piece();
}
}
void move_down() {
if (piece_fits_at(curr_pos_x, curr_pos_y + 1, 0)) {
remove_piece();
curr_pos_y++;
put_piece();
} else {
put_piece();
load_next_shape();
}
}
void drop_piece() {
while (piece_fits_at(curr_pos_x, curr_pos_y + 1, 0)) {
curr_pos_y++;
}
put_piece();
load_next_shape();
print_board();
}
int kbhit() {
struct timeval tv;
fd_set read_fd;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&read_fd);
FD_SET(_getch(),&read_fd);
if(select(1, &read_fd, NULL, NULL, &tv) == -1)
return 0;
if(FD_ISSET(0,&read_fd))
return 1;
return 0;
}
int main() {
srand(time(NULL));
load_next_shape();
print_board();
while (1) {
if (kbhit()) {
int ch = _getch();
if (ch == 27) {
return 0;
} else if (ch == 75) {
move_left();
} else if (ch == 77) {
move_right();
} else if (ch == 72) {
rotate_shape();
} else if (ch == 80) {
move_down();
} else if (ch == 32) {
drop_piece();
}
system("cls");
print_board();
}
Sleep(70);
if (piece_fits_at(curr_pos_x, curr_pos_y + 1, 0)) {
remove_piece();
curr_pos_y++;
put_piece();
system("cls");
print_board();
} else {
put_piece();
load_next_shape();
system("cls");
print_board();
}
}
return 0;
}
```
阅读全文