📄 block.c
字号:
blocks_list->bottom = y; map[y][x] = mark; mark_block(map, blocks_list, mark, x + 1, y, bottom); mark_block(map, blocks_list, mark, x, y + 1, bottom); mark_block(map, blocks_list, mark, x - 1, y, bottom); mark_block(map, blocks_list, mark, x, y - 1, bottom); }}static int mark_block_group(Player * player, block_group_t * group_list, int * mark_nr){ int y; int bottom = 0; int mark = 2; for (y = BLOCK_MAP_H - 1; y >= 0; y--) { int x, xx = 0; for (x = 0; x < BLOCK_MAP_W; x++) if (player->block_map[y][x] > 0) { player->block_map[y][x] = 1; xx++; } if (xx == BLOCK_MAP_W) { player->grade++; for (x = 0; x < BLOCK_MAP_W; x++) player->block_map[y][x] = 0; if (bottom == 0) bottom = y; } } while (bottom > 0) { int left = 0; int top = 0; for (; top < bottom; top++) { for (left = 0; left < BLOCK_MAP_W && player->block_map[top][left] != 1; left++); if (left < BLOCK_MAP_W) break; } if (left == BLOCK_MAP_W) break; mark_block(player->block_map, &group_list[mark-2], mark, left, top, bottom); (*mark_nr)++; mark++; } if (mark > 2) return 1; return 0;}static void fall_list(Uchar map[BLOCK_MAP_H][BLOCK_MAP_W], block_group_t * blocks_list){ int step = BLOCK_MAP_H; block_list_t * head = blocks_list->head; while (head) { int j; block_list_t * tmp = head; head = tmp->next; for (j = 1; j <= step; j++) if (tmp->y+j == BLOCK_MAP_H || map[tmp->y+j][tmp->x]) { step = j - 1; break; } } if (step > 0) { head = blocks_list->head; while (head) { block_list_t * tmp = head; head = tmp->next; tmp->y += step; map[tmp->y][tmp->x] = 1; } }}/*下落标记过的块组*/static void fall_mark_group(Uchar map[BLOCK_MAP_H][BLOCK_MAP_W], block_group_t * blocks_list, int mark_nr){ block_group_t * tmp[mark_nr]; block_group_t * min; int i, y; for (y = 0; y < BLOCK_MAP_H; y++) { int x; for (x = 0; x < BLOCK_MAP_W; x++) if (map[y][x] > 1) map[y][x] = 0; } for (i = 0; i < mark_nr; i++) tmp[i] = blocks_list + i; for (i = 0; i < mark_nr; i++) { int j; int max = 0, top; for (j = 0; j < mark_nr; j++) { if (tmp[j] && tmp[j]->top > max) { max = tmp[j]->top; top = j; } } if (max == 0) break; min = tmp[top]; tmp[top] = 0; fall_list(map, min); }}static void reset_block_map(Player * player){ int i; int mark_nr = 0; block_group_t group_list[20]; for (i = 0; i < 20; i++) { group_list[i].top = BLOCK_MAP_H; group_list[i].bottom = 0; group_list[i].head = 0; } if (mark_block_group(player, group_list, &mark_nr)) { fall_mark_group(player->block_map, group_list, mark_nr); for (i = 0; i < mark_nr; i++) release_group_list(group_list + i); reset_block_map(player); }}/*粘合正在凝固的方块*/static void player_solidify_block(Player * player, Mtime time){ int i; Block * blocks = player->blocks; if (!player->solidify) return; if (player->time > time) { player->time -= time; return; } for (i = 0; i < 4; i++) { int x = blocks[i].x; int y = blocks[i].y; player->block_map[y][x] = 1; } reset_block_map(player); player->time = 0; player->solidify = 0; player->cur_shape = NULL; return;}static inline void solidify_block(RussiaBlock * rb, Mtime time){ int i; for (i = 0; i < 2; i++) if (rb->match[i]) player_solidify_block(rb->match[i], time);}static int check_collide(Player * player){ Block * blocks = player->blocks; int i; for (i = 0; i < 4; i++) { if (player->block_map[blocks[i].y][blocks[i].x]) return 1; } return 0;}void cp_next_shape(Player * player){ int x, y; Shape * shape = player->next_shape; for (y = 0; y < 4; y++) for (x = 0; x < 4; x++) player->next_matrix[y][x] = shape->matrix[y][x];}static void player_frame_start(Player * player, IOOPS * io_ops){}static void frame_start(RussiaBlock * rb){ int i; for (i = 0; i < 2; i++) if (rb->match[i]) player_frame_start(rb->match[i], rb->io_ops);}static int player_frame_end(Player * player, IOOPS * io_ops){ if (player->cur_shape) return 1; if (player->grade > player->old_grade) { if (player->grade / BLOCK_GRADE && !(player->grade % BLOCK_GRADE)) { player->speed *= 0.9; player->level++; } io_ops->out_grade(player); player->old_grade = player->grade; } player->cur_shape = player->next_shape; player->next_shape = shapes[rand_id()]; cp_next_shape(player); io_ops->out_next_block(player); player->direct = 0; player->x = BLOCK_MAP_W / 2 - player->cur_shape->w / 2; player->y = 0; cp_shape_to_block(player->blocks, player->x, player->y, player->cur_shape); if (check_collide(player)) return 0; return 1;}static inline void set_player_status(Player * player, int status, int set){ if (set) player->status |= status; else player->status &= ~status;}static inline int get_player_status(Player * player, int status){ return player->status & status;}void init_player(RussiaBlock * rb, Player * player){ player->event_buf = (DataBuffer *)malloc_data_buf(KEYEVENT_BUF); player->old_grade = -1; player->grade = 0; player->speed = BLOCK_INIT_SPEED; player->slice = 0; player->cur_shape = 0; player->next_shape = shapes[rand_id()]; cp_next_shape(player); if (rb->io_ops) rb->io_ops->init(player); return;}static void frame_end(RussiaBlock * rb){ switch (rb->type) { case GAMETYPE_ALONE_SINGLE: if (!player_frame_end(rb->match[0], rb->io_ops)) { set_player_status(rb->match[0], PLAYER_STATUS_MATCH, 0); rb->status &= ~STATUS_RUN; } break; case GAMETYPE_COMPUTER_MATCH: case GAMETYPE_ONLINE_SERVER: case GAMETYPE_ALONE_MATCH: { int i; for (i = 0; i < 2; i++) { if (!player_frame_end(rb->match[i], rb->io_ops)) { set_player_status(rb->match[i], PLAYER_STATUS_LOSE, 1); set_player_status(rb->match[i], PLAYER_READY, 0); if (rb->rb_ops->del_match) rb->rb_ops->del_match(rb, rb->match[i]); rb->status &= ~STATUS_RUN; } } } break; }}static void clock_block_fall(RussiaBlock * rb, Mtime time){ int c, i; for (i = 0; i < 2; i++) { if (rb->match[i]) { rb->match[i]->slice += time; if ((c = rb->match[i]->slice / rb->match[i]->speed) > 0) { block_fall(rb->match[i], c, SOLIDIFY_TIME); out_win_map(rb->match[i], rb->io_ops); rb->match[i]->slice %= rb->match[i]->speed; } } } return;}RussiaBlock * new_game(int type, void * data){ RussiaBlock * rb; if (!(rb = (RussiaBlock *)calloc(1, sizeof(RussiaBlock)))) return NULL; rb->type = type; switch (type) { case GAMETYPE_ALONE_MATCH: /*重定向输入输出到本地双人模式*/ rb->rb_ops = &native_rb_ops; rb->io_ops = &native_io_ops; break; case GAMETYPE_ALONE_SINGLE: /*重定向输入输出到本地单人模式*/ rb->rb_ops = &native_rb_single_ops; rb->io_ops = &native_io_ops; break; case GAMETYPE_COMPUTER_MATCH: break; case GAMETYPE_ONLINE_SERVER: /*重定向输入输出到网络服务器模式*/ rb->rb_ops = &remote_rb_ops; rb->io_ops = &remote_io_ops; break; case GAMETYPE_ONLINE_CLIENT: /*重定向输入输出到网络客户端模式*/ rb->rb_ops = &client_rb_ops; rb->io_ops = &native_io_ops; break; } if (rb->rb_ops && rb->rb_ops->init(rb, data)) return rb; return NULL;}/*填加新玩家*/void add_player(RussiaBlock * rb){ Player * player; RBOPS * rb_ops; if ((rb->status & STATUS_ADD_FINISH) || !(rb_ops = rb->rb_ops)) return; if (rb_ops->add_player && (player = rb_ops->add_player(rb))) { init_player(rb, player); if (rb_ops->add_match) rb_ops->add_match(rb); } return;}void game_frame(RussiaBlock * rb, Mtime time){ frame_start(rb); clock_block_fall(rb, time); response_event(rb); solidify_block(rb, time); frame_end(rb);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -