/** * params: * game -- the game to draw */ void game_draw(game_t *game) { field_draw(&game->field); screen_clear(&game->block_screen, 0x30); for(int x = 0; x < BLOCK_QUEUE_SIZE; ++x) { if(game->player != (game->queue + x)) { block_draw(&game->block_screen, &game->queue[x]); } } field_block_draw(&game->field, game->player); }
void game_frame(SDL_Surface *screen) { game_handle_keys(); switch(game_state) { case TS_FALLING: curr_piece.y += 4; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.y = _bx(_py(curr_piece.y)-1); ++curr_piece.lock; } if(game_get_key_DAS(TK_DOWN)) curr_piece.y += 0xFF; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.y = _bx(_py(curr_piece.y)-1); curr_piece.lock = T_LOCKDELAY+1; } if(game_get_key(TK_UP)) { while(!collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.y += 0xFF; curr_piece.y -= 0xFF; } if(game_get_key_DAS(TK_RIGHT)) { curr_piece.x += 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.x -= 1; } if(game_get_key_DAS(TK_LEFT)) { curr_piece.x -= 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) curr_piece.x += 1; } int init_rot = curr_piece.rot; if(game_get_key(TK_A)||game_get_key(TK_B)||game_get_key(TK_C)) { if(game_get_key(TK_A)||game_get_key(TK_C)) { curr_piece.rot -= 1; if(curr_piece.rot < 0) curr_piece.rot = curr_piece.t->frames-1; } else if(game_get_key(TK_B)) { curr_piece.rot += 1; if(curr_piece.rot >= curr_piece.t->frames) curr_piece.rot = 0; } if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x -= 1; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x += 2; if(collision_check(curr_piece.t, f, curr_piece.x, _py(curr_piece.y), curr_piece.rot)) { curr_piece.x -= 1; curr_piece.rot = init_rot; } } } } if(curr_piece.lock >= T_LOCKDELAY) { game_state = TS_LINECLEAR; clear_time = T_CLEARDELAY; lock_piece(); } break; case TS_LINECLEAR: if(clear_time == T_CLEARDELAY) { int x,y,numclear=0,empty; for(y=f->h-1; y>=0; --y) { empty = 0; for(x=f->w-1; x>=0; --x) { if(f->field[y*f->w + x] == 0) { empty = 1; break; } } if(empty == 0) { for(x=f->w-1; x>=0; --x) f->field[y*f->w + x] = 0xFE; ++numclear; } } if(numclear == 0) { game_state = TS_ENTRY; break; } } --clear_time; if(clear_time == 0) { int y,cy,cx; for(y=0; y<f->h; ++y) { if((unsigned char)f->field[y*f->w+0] == 0xFE) { printf("clearing line %i\n", y); for(cy=y; cy>0; --cy) for(cx=f->w-1; cx>=0; --cx) f->field[cy*f->w+cx] = f->field[(cy-1)*f->w+cx]; } } game_state = TS_ENTRY; } break; case TS_ENTRY: --are_time; if(are_time == 0) { are_time = T_ENTRYDELAY; game_next_piece(); game_state = TS_FALLING; } break; } SDL_Rect rect = { (SCREEN_WIDTH) / 2 - (10 * BLOCK_SIZE) / 2, (SCREEN_HEIGHT) - (20 * BLOCK_SIZE) - 20, BLOCK_SIZE * 10 + 1, BLOCK_SIZE * 20 + 1}; SDL_Rect next_rect = { (rect.x) + ((BLOCK_SIZE * 10/2) - (BLOCK_SIZE * 4/2)), rect.y - (4 * BLOCK_SIZE) - 10, 4 * BLOCK_SIZE+1, 4 * BLOCK_SIZE+1}; SDL_FillRect(screen, &rect, block_color[0xFF]); field_draw(f, screen, rect.x, rect.y); next_draw(next_piece, screen, next_rect.x, next_rect.y + (4-next_piece->mask_size)*BLOCK_SIZE); if(game_state == TS_FALLING) drop_draw(curr_piece, screen, rect.x, rect.y); }