c语言设计俄罗斯方块
时间: 2023-12-15 13:32:30 浏览: 30
以下是一个简单的C语言俄罗斯方块的设计示例,包括视图和模型的实现:
1. 视图部分(view.h和view.cpp):
view.h:
```c
#ifndef VIEW_H
#define VIEW_H
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
void gotoxy(int x, int y);
void draw_block(int x, int y, int color);
void draw_board();
void draw_shape(int x, int y, int shape, int direction, int color);
void clear_shape(int x, int y, int shape, int direction);
#endif
```
view.cpp:
```c
#include "view.h"
void gotoxy(int x, int y) {
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void draw_block(int x, int y, int color) {
gotoxy(x * 2, y);
printf("[]", color);
}
void draw_board() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
draw_block(j, i, 15);
}
else {
draw_block(j, i, 0);
}
}
}
}
void draw_shape(int x, int y, int shape, int direction, int color) {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if ((shape >> (i * 4 + j)) & 0x0001) {
draw_block(x + j + 1, y + i + 1, color);
}
}
}
}
void clear_shape(int x, int y, int shape, int direction) {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if ((shape >> (i * 4 + j)) & 0x0001) {
draw_block(x + j + 1, y + i + 1, 0);
}
}
}
}
```
2. 模型部分(model.h和model.cpp):
model.h:
```c
#ifndef MODEL_H
#define MODEL_H
#include <time.h>
#define SHAPE_COUNT 7
enum {
SHAPE_I,
SHAPE_J,
SHAPE_L,
SHAPE_O,
SHAPE_S,
SHAPE_T,
SHAPE_Z
};
enum {
DIR_UP,
DIR_RIGHT,
DIR_DOWN,
DIR_LEFT
};
typedef struct {
int x;
int y;
int shape;
int direction;
int color;
} Block;
void init_model();
void new_block();
int move_block(int dx, int dy, int dshape);
int drop_block();
int rotate_block();
int is_gameover();
#endif
```
model.cpp:
```c
#include "model.h"
int shapes[SHAPE_COUNT][4][4] = {
{
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 0, 1, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 0, 1, 1 },
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 1 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 0 }
}
};
Block current_block;
int board[HEIGHT][WIDTH];
void init_model() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
srand((unsigned int)time(NULL));
new_block();
}
void new_block() {
current_block.x = WIDTH / 2 - 2;
current_block.y = 0;
current_block.shape = rand() % SHAPE_COUNT;
current_block.direction = DIR_UP;
current_block.color = rand() % 7 + 9;
if (move_block(0, 0, 0)) {
exit(0);
}
}
int move_block(int dx, int dy, int dshape) {
int x = current_block.x + dx;
int y = current_block.y + dy;
int shape = current_block.shape + dshape;
int direction = current_block.direction;
if (shape < 0) {
shape = 3;
}
else if (shape > 3) {
shape = 0;
}
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if ((shapes[current_block.shape][i][j] >> direction) & 0x0001) {
if (y + i >= HEIGHT || x + j < 0 || x + j >= WIDTH || board[y + i][x + j]) {
return 1;
}
}
}
}
current_block.x = x;
current_block.y = y;
current_block.shape = shape;
return 0;
}
int drop_block() {
while (!move_block(0, 1, 0));
return 1;
}
int rotate_block() {
return move_block(0,0, 1);
}
int is_gameover() {
return board[0][WIDTH / 2] || board[0][WIDTH / 2 + 1] || board[0][WIDTH / 2 - 1];
}
```
以上是一个简单的C语言俄罗斯方块的设计示例,包括视图和模型的实现。你可以根据需要进行修改和扩展。