Exemple #1
0
void pong(void)
{
    enable_computer = 1;
    paddle_touch_id[L] = -1;
    paddle_touch_id[R] = -1;

    // middle line
    fb_add_rect(0, fb_height/2 - 1, fb_width, 1, WHITE);

    score[L] = fb_add_text(0, fb_height/2 - SIZE_EXTRA*16 - 20, WHITE, SIZE_EXTRA, "0");
    score[R] = fb_add_text(0, fb_height/2 + 20, WHITE, SIZE_EXTRA, "0");

    paddles[L] = fb_add_rect(100, PADDLE_Y, PADDLE_W, PADDLE_H, WHITE);
    paddles[R] = fb_add_rect(100, fb_height-PADDLE_Y-PADDLE_H, PADDLE_W, PADDLE_H, WHITE);

    ball = fb_add_rect(0, 0, BALL_W, BALL_W, WHITE);

    pong_spawn_ball(rand()%2);
    pong_calc_movement();

    add_touch_handler(&pong_touch_handler, NULL);

    int step = 0;
    volatile int run = 1;
    while(run)
    {
        switch(get_last_key())
        {
            case KEY_POWER:
                run = 0;
                break;
            case KEY_VOLUMEUP:
                ball_speed += 5;
                pong_spawn_ball(rand()%2);
                pong_calc_movement();
                step = 0;
                break;
            case KEY_VOLUMEDOWN:
                if(ball_speed > 5)
                    ball_speed -= 5;
                pong_spawn_ball(rand()%2);
                pong_calc_movement();
                step = 0;
                break;
        }

        step = pong_do_movement(step);

        fb_draw();
        usleep(1000);
    }

    rm_touch_handler(&pong_touch_handler, NULL);

    list_clear(&movement_steps, &free);
}
Exemple #2
0
int pong_do_movement(int step)
{
    if(!movement_steps[step])
    {
        pong_calc_movement();
        return 0;
    }

    int col = movement_steps[step]->collision;
    if(col == COL_NONE || col == COL_LEFT || col == COL_RIGHT)
    {
        ball->x = movement_steps[step]->x;
        ball->y = movement_steps[step]->y;
        if(enable_computer)
            pong_handle_ai();
    }

    switch(col)
    {
        case COL_NONE:
            return step+1;
        case COL_TOP:
        case COL_BOTTOM:
            ball_speed_x = -ball_speed_x;
            break;
        case COL_LEFT:
        case COL_RIGHT:
        {
            int s = col - 1;
            if(ball->x+BALL_W >= paddles[s]->x && ball->x <= paddles[s]->x+PADDLE_W)
            {
                // Increase X speed according to distance from center of paddle.
                ball_speed_x = (float)((ball->x + BALL_W/2) - (paddles[s]->x + PADDLE_W/2))/BALL_SPEED_MOD;
                ball_speed_y = -ball_speed_y;
            }
            else
            {
                pong_add_score(!s);
                for(col = 0; col < 1000; col += 16)
                {
                    fb_request_draw();
                    usleep(16000);
                }
                pong_spawn_ball(s);
            }
            break;
        }
    }

    pong_calc_movement();
    return 0;
}
Exemple #3
0
void pong(void)
{
    enable_computer = 1;
    paddle_touch_id[L] = -1;
    paddle_touch_id[R] = -1;

    score_val[L] = 0;
    score_val[R] = 0;

    fb_set_background(BLACK);

    fb_text_proto *p = fb_text_create(0, 0, GRAYISH, SIZE_SMALL, "Press power button to go back");
    p->style = STYLE_ITALIC;
    fb_text *help = fb_text_finalize(p);
    help->y = fb_height/2 - help->h*1.5;
    center_text(help, 0, -1, fb_width, -1);

    // middle line
    fb_add_rect(0, fb_height/2 - 1, fb_width, 1, WHITE);

    score[L] = fb_add_text(0, 0, WHITE, SIZE_EXTRA, "0");
    score[L]->y = fb_height/2 - score[L]->h - 20*DPI_MUL;
    score[R] = fb_add_text(0, fb_height/2 + 20*DPI_MUL, WHITE, SIZE_EXTRA, "0");

    paddles[L] = fb_add_rect(100, PADDLE_Y, PADDLE_W, PADDLE_H, WHITE);
    paddles[R] = fb_add_rect(100, fb_height-PADDLE_Y-PADDLE_H, PADDLE_W, PADDLE_H, WHITE);

    ball = fb_add_rect(0, 0, BALL_W, BALL_W, WHITE);

    pong_spawn_ball(rand()%2);
    pong_calc_movement();

    add_touch_handler(&pong_touch_handler, NULL);

    int step = 0;
    volatile int run = 1;
    while(run)
    {
        switch(get_last_key())
        {
            case KEY_POWER:
                run = 0;
                break;
            case KEY_VOLUMEUP:
                ball_speed += 5;
                pong_spawn_ball(rand()%2);
                pong_calc_movement();
                step = 0;
                break;
            case KEY_VOLUMEDOWN:
                if(ball_speed > 5)
                    ball_speed -= 5;
                pong_spawn_ball(rand()%2);
                pong_calc_movement();
                step = 0;
                break;
        }

        step = pong_do_movement(step);

        fb_request_draw();
        usleep(16000);
    }

    rm_touch_handler(&pong_touch_handler, NULL);

    list_clear(&movement_steps, &free);
}