📄 block.c
字号:
/*主体代码*/#include <stdio.h>#include <stdlib.h>#include "block.h"#include "common.h"#include "native.h"#include "remote.h"#include "client.h"/*所有形状的方块位图数组*/Shape shapes[7][4] = { {{{{1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 4}, {{{1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}}, 1}, {{{1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 4}, {{{1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}}, 1}}, {{{{1, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{0, 1, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, 2}, {{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 0, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}, 2}}, {{{{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 2}, {{{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 2}, {{{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 2}, {{{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 2}}, {{{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{0, 1, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}, 2}, {{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{0, 1, 0, 0}, {1, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}, 2}}, {{{{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, 2}, {{{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, 2}}, {{{{1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, 2}, {{{0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 0, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}}, 2}}, {{{{1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{0, 1, 0, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}}, 2}, {{{1, 0, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, 3}, {{{1, 1, 0, 0}, {1, 0, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}, 2}}};/* 设置方块落地后的凝固时间*/static int set_glue_block(Player * player, Mtime time){ int i; Block * blocks = player->blocks; for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; if (y + 1 == BLOCK_MAP_H || player->block_map[y+1][x]) { if (!player->solidify) { player->time = time; player->solidify = 1; } return 1; } } player->solidify = 0; player->time = 0; return 0;}/*输出(BLOCK_MAP_H * BLOCK_MAP_W)大小的位图到本地或是网络*/static void out_win_map(Player * player, IOOPS * io_ops){ int i; int x, y; Block * blocks = player->blocks; Uchar out_map[BLOCK_MAP_H][BLOCK_MAP_W]; for (y = 0; y < BLOCK_MAP_H; y++) { for (x = 0; x < BLOCK_MAP_W; x++) { out_map[y][x] = player->block_map[y][x]; } } for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; out_map[y][x] = 1; } io_ops->out_win_map(player, out_map);}/*下落方块*/static int block_fall(Player * player, int count, Mtime time){ Block * blocks = player->blocks; int i, step = count; for (i = 0; i < 4; i++) { int j; int x = blocks[i].x; int y = blocks[i].y; for (j = 1; j <= step; j++) { if (y+j == BLOCK_MAP_H || player->block_map[y+j][x]) { step = j - 1; break; } } if (step == 0) break; } if (step > 0) { for (i = 0; i < 4; i++) blocks[i].y += step; player->y += step; } set_glue_block(player, time); return 1;}/*左移方块*/static int block_left(Player * player){ Block * blocks = player->blocks; int i; for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; if (x == 0 || player->block_map[y][x-1]) return 0; } for (i = 0; i < 4; i++) blocks[i].x--; player->x--; return 1;}/*右移方块*/static int block_right(Player * player){ Block * blocks = player->blocks; int i; for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; if (x == (BLOCK_MAP_W - 1) || player->block_map[y][x+1]) return 0; } for (i = 0; i < 4; i++) blocks[i].x++; player->x++; return 1;}/*从方块位图数组中拷贝一个方块位图*/static void cp_shape_to_block(Block * blocks, int x, int y, Shape * shape){ int i, k = 0; for (i = 0; i < 4 && k < 4; i++) { int j; for (j = 0; j < 4 && k < 4; j++) { if (shape->matrix[i][j] && k < 4) { blocks[k].x = x + j; blocks[k].y = y + i; k++; } } }}/*旋转方块*/static int block_turn(Player * player){ int i; int direct = (player->direct + 1) % 4; Block blocks[4]; cp_shape_to_block(blocks, player->x, player->y, player->cur_shape + direct); for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; if (x >= BLOCK_MAP_W || y >= BLOCK_MAP_H || player->block_map[y][x]) return 0; } player->direct = direct; for (i = 0; i < 4; i++) { player->blocks[i].x = blocks[i].x; player->blocks[i].y = blocks[i].y; } return 1;}/*从buf接收方向事件,buf中的方向事件可以从网络和本地获取*/static int rb_event(Player * player){ int event; if (read_data_from_buf(player->event_buf, (char *)&event, sizeof(event)) < sizeof(event)) return -1; return event;}/*响应方向事件*/static void player_response_event(Player * player, IOOPS * io_ops){ int event; if ((event = rb_event(player)) < 0) return; switch (event) { case EVENT_UP: block_turn(player); set_glue_block(player, SOLIDIFY_TIME); break; case EVENT_DOWN: block_fall(player, BLOCK_MAP_H, 0); break; case EVENT_LEFT: block_left(player); set_glue_block(player, SOLIDIFY_TIME); break; case EVENT_RIGHT: block_right(player); set_glue_block(player, SOLIDIFY_TIME); break; case EVENT_SPACE: case EVENT_ESCAPE: break; } out_win_map(player, io_ops); return;}static void response_event(RussiaBlock * rb){ int i; for (i = 0; i < 2; i++) if (rb->match[i]) player_response_event(rb->match[i], rb->io_ops); return;}typedef struct _block_list_t { int x, y; struct _block_list_t * next;} block_list_t;typedef struct { int top; int bottom; block_list_t * head;} block_group_t;static void release_group_list(block_group_t * blocks_list){ block_list_t * head = blocks_list->head; while (head) { block_list_t * tmp = head; head = tmp->next; free(tmp); }}/*消除行后用递归标记分隔的块组*/static void mark_block(Uchar map[BLOCK_MAP_H][BLOCK_MAP_W], block_group_t * blocks_list, int mark, int x, int y, int bottom){ if (x < 0 || x == BLOCK_MAP_W || y < 0 || y == bottom) return; if (map[y][x] == 1) { block_list_t * list; list = (block_list_t *)malloc(sizeof(block_list_t)); list->x = x; list->y = y; list->next = blocks_list->head; blocks_list->head = list; if (y < blocks_list->top) blocks_list->top = y; if (y > blocks_list->bottom)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -