Ejemplo n.º 1
0
void move_block(Block * block, int dx, int dy, Field field) {
  if (NULL_BLOCK(*block)) return;

  Block new_block = *block;
  new_block.x += dx;
  new_block.y += dy;

  if (collision(&new_block, field)) {
    // if the block is moving down, then it should come to rest.
    // otherwise just do nothing.
    if (dy) {
      freeze_block(block, field);
    }
  } else {
    *block = new_block;
  }
}
Ejemplo n.º 2
0
static void *worker_thread_fn(void *arg)
{
    int i;
    int status;
    int timeout = INITIAL_TIMEOUT;
    struct game_score game_score = { game_options.initial_level, 
        0, 0, 0, global_highscore };
    struct thread_data *data = (struct thread_data *)arg;

    /* setup the initial timeout (by reducing all deltas upto initial_level */
    for (i = 1; i <= game_options.initial_level; i++)
        timeout -= TIMEOUT_DELTA(i);

    draw_score_board(&game_score);

    /* main game loop */
    while (1) {
        /* wait till timeout */
        snooze(timeout);

        /* acquire the lock */
        pthread_mutex_lock(&data->lock);

        if (data->game_over) {
            pthread_mutex_unlock(&data->lock);
            break;
        }

        if (!data->current) {
            data->current = update_current_block(NULL);

            status = move_block(data->current, ACTION_PLACE_NEW);
            if (status == FAILURE) {
                data->game_over = 1;
                pthread_mutex_unlock(&data->lock);
                break;
            }

            draw_next_block(next_block, next_block_orientation);
        } else {
            /* try moving the block downwards */
            status = move_block(data->current, ACTION_MOVE_DOWN);
            if (status == FAILURE) {
                int ret, num_rows;

                /* freeze this block in the game board */
                freeze_block(data->current);
                data->current = NULL;	/* reset the current block pointer */
                data->block_dropped_ignore_input = 0; /* reset this now */

                num_rows = clear_even_rows();
                if (num_rows) {
                    ret = update_score_level(&game_score, num_rows, &timeout);
                    draw_score_board(&game_score);

                    /* see if the level has changed */
                    if (ret) {
                        draw_game_board(data->current);
                        draw_level_info(game_score.level);
                    }
                }
            }
        }

        draw_game_board(data->current);
        pthread_mutex_unlock(&data->lock);
    }

    if (game_score.score > global_highscore) {
        global_highscore = game_score.score;
        data->new_highscore = 1;
    }

    return arg;
}