用c语言实现七巧板 simpleGUI
时间: 2023-10-24 22:05:56 浏览: 160
七巧板是一种古老的益智玩具,由七个不同形状的木板组成,可以拼出各种形状的图案。下面是使用C语言和SimpleGUI库实现的七巧板程序。
```c
#include <simplegui.h>
#define BOARD_WIDTH 300
#define BOARD_HEIGHT 400
#define BOARD_X 50
#define BOARD_Y 50
#define BLOCK_SIZE 50
#define BLOCK_NUM 7
#define BLACK 0x000000
#define GRAY 0x808080
#define WHITE 0xFFFFFF
typedef struct {
int x, y;
} Point;
typedef struct {
Point points[4];
} Block;
Block blocks[BLOCK_NUM] = {
{{{0, 0}, {0, 1}, {0, 2}, {0, 3}}}, // I
{{{0, 0}, {0, 1}, {1, 0}, {1, 1}}}, // O
{{{0, 0}, {0, 1}, {0, 2}, {1, 2}}}, // L
{{{0, 0}, {0, 1}, {0, 2}, {1, 0}}}, // J
{{{0, 0}, {0, 1}, {1, 1}, {1, 2}}}, // Z
{{{0, 1}, {0, 2}, {1, 0}, {1, 1}}}, // S
{{{0, 0}, {0, 1}, {0, 2}, {1, 1}}}, // T
};
int current_block = -1;
int current_rotation = 0;
int current_x = 0;
int current_y = 0;
int board[BOARD_HEIGHT / BLOCK_SIZE][BOARD_WIDTH / BLOCK_SIZE] = {0};
void draw_board() {
for (int i = 0; i < BOARD_HEIGHT / BLOCK_SIZE; i++) {
for (int j = 0; j < BOARD_WIDTH / BLOCK_SIZE; j++) {
if (board[i][j] == 0) {
draw_rect(j * BLOCK_SIZE + BOARD_X, i * BLOCK_SIZE + BOARD_Y, BLOCK_SIZE, BLOCK_SIZE, WHITE);
} else {
draw_rect(j * BLOCK_SIZE + BOARD_X, i * BLOCK_SIZE + BOARD_Y, BLOCK_SIZE, BLOCK_SIZE, BLACK);
}
}
}
}
void draw_block(Block block, int x, int y, int color) {
for (int i = 0; i < 4; i++) {
int px = block.points[i].x + x;
int py = block.points[i].y + y;
draw_rect(px * BLOCK_SIZE + BOARD_X, py * BLOCK_SIZE + BOARD_Y, BLOCK_SIZE, BLOCK_SIZE, color);
}
}
void draw_current_block() {
if (current_block >= 0) {
Block block = blocks[current_block];
draw_block(block, current_x, current_y, GRAY);
}
}
void draw() {
draw_board();
draw_current_block();
}
void new_block() {
current_block = rand() % BLOCK_NUM;
current_rotation = 0;
current_x = BOARD_WIDTH / BLOCK_SIZE / 2 - 2;
current_y = 0;
}
int can_move_block(Block block, int x, int y) {
for (int i = 0; i < 4; i++) {
int px = block.points[i].x + x;
int py = block.points[i].y + y;
if (px < 0 || px >= BOARD_WIDTH / BLOCK_SIZE || py < 0 || py >= BOARD_HEIGHT / BLOCK_SIZE) {
return 0;
}
if (board[py][px] != 0) {
return 0;
}
}
return 1;
}
void move_block(int dx, int dy) {
if (can_move_block(blocks[current_block], current_x + dx, current_y + dy)) {
current_x += dx;
current_y += dy;
}
}
void rotate_block() {
int next_rotation = (current_rotation + 1) % 4;
Block next_block = blocks[current_block];
for (int i = 0; i < 4; i++) {
int x = next_block.points[i].x;
int y = next_block.points[i].y;
next_block.points[i].x = -y;
next_block.points[i].y = x;
}
if (can_move_block(next_block, current_x, current_y)) {
current_rotation = next_rotation;
blocks[current_block] = next_block;
}
}
void drop_block() {
while (can_move_block(blocks[current_block], current_x, current_y + 1)) {
current_y++;
}
}
void place_block() {
for (int i = 0; i < 4; i++) {
int px = blocks[current_block].points[i].x + current_x;
int py = blocks[current_block].points[i].y + current_y;
board[py][px] = 1;
}
}
void remove_full_rows() {
int full_rows = 0;
for (int i = BOARD_HEIGHT / BLOCK_SIZE - 1; i >= 0; i--) {
int full = 1;
for (int j = 0; j < BOARD_WIDTH / BLOCK_SIZE; j++) {
if (board[i][j] == 0) {
full = 0;
break;
}
}
if (full) {
full_rows++;
for (int k = i; k > 0; k--) {
for (int j = 0; j < BOARD_WIDTH / BLOCK_SIZE; j++) {
board[k][j] = board[k - 1][j];
}
}
for (int j = 0; j < BOARD_WIDTH / BLOCK_SIZE; j++) {
board[0][j] = 0;
}
i++;
}
}
}
void update() {
if (can_move_block(blocks[current_block], current_x, current_y + 1)) {
current_y++;
} else {
place_block();
remove_full_rows();
new_block();
if (!can_move_block(blocks[current_block], current_x, current_y)) {
exit(0);
}
}
}
void key_down(int key) {
switch (key) {
case SG_KEY_LEFT:
move_block(-1, 0);
break;
case SG_KEY_RIGHT:
move_block(1, 0);
break;
case SG_KEY_DOWN:
move_block(0, 1);
break;
case SG_KEY_SPACE:
drop_block();
break;
case SG_KEY_UP:
rotate_block();
break;
}
}
void init() {
set_canvas_size(BOARD_WIDTH + BOARD_X * 2, BOARD_HEIGHT + BOARD_Y * 2);
set_title("七巧板");
new_block();
}
int main() {
init();
set_draw_handler(draw);
set_keydown_handler(key_down);
set_interval(update, 500);
start();
return 0;
}
```
这个程序使用了SimpleGUI库来实现图形界面和用户输入的处理。在程序中定义了七个不同形状的七巧板,以及一个游戏区域和一个棋盘数组。程序使用了基本的图形绘制函数来绘制游戏界面和七巧板。在每个周期内,程序检查当前的七巧板是否可以向下移动,如果可以,就让它向下移动一格,否则就将它放置到棋盘上,并检查是否有满行需要清除。如果当前的七巧板无法移动,游戏就结束了。程序还处理了用户的按键事件,让用户可以控制七巧板的移动和旋转。
阅读全文