작은 걸음 II — 만들며 배우기

게임에 점수 붙이기

게임에 점수 붙이기

이번에 배울 것

게임 규칙은 어떤 사건이 일어났을 때 상태를 어떻게 바꿀지 정한 것입니다. 공을 받아 냈을 때만 점수를 늘립니다.

실행해 보기

예제 · example2.cpp
void small_main()
{
    Window window;
    window.set_title("Catch the Ball");
    window.open(640, 480);

    double player_x = 320;
    double ball_x = 100;
    double ball_y = 80;
    double ball_vx = 180;
    double ball_vy = 140;
    int score = 0;

    StopWatch watch;

    while (window.is_open())
    {
        double dt = watch.elapsed();
        watch.reset();

        if (window.key_down(Key::Left)) player_x = player_x - 250 * dt;
        if (window.key_down(Key::Right)) player_x = player_x + 250 * dt;

        ball_x = ball_x + ball_vx * dt;
        ball_y = ball_y + ball_vy * dt;

        if (ball_x < 15)
        {
            ball_x = 15;
            ball_vx = -ball_vx;
        }

        if (ball_x > 625)
        {
            ball_x = 625;
            ball_vx = -ball_vx;
        }

        if (ball_y < 15)
        {
            ball_y = 15;
            ball_vy = -ball_vy;
        }

        if (ball_y > 430 && ball_y < 460 &&
            ball_x > player_x - 60 && ball_x < player_x + 60)
        {
            ball_y = 430;
            ball_vy = -ball_vy;
            score = score + 1;
            play_sound(Sound::Coin);
        }

        if (ball_y > 500)
        {
            ball_x = 100;
            ball_y = 80;
        }

        window.set_title("Score: ", score);
        window.clear(Black);
        window.fill_rectangle(player_x - 60, 450, 120, 12, White);
        window.fill_circle(ball_x, ball_y, 15, Cyan);
        window.show();
    }
}

좌우 키로 막대를 움직이세요. 공이 막대의 범위에 닿으면 튕기고 score가 1 증가합니다. set_title은 현재 점수를 창 제목에 보여 줍니다.

놓쳤을 때는 공만 시작 위치로 돌립니다. 적중과 실패를 각각 시험하세요. 문제가 있으면 위치 갱신·조건·점수 변경 중 어느 단계인지 나누어 확인합니다.

Exercise — score를 제목에 표시

시작 코드의 paddle을 좌우 방향키로 움직여 보세요. 공이 paddle에 맞을 때만 score를 1 증가시키고 window.set_title("Score: ", score)로 창 제목에 표시하세요. paddle을 옆으로 치워 공을 놓쳤을 때에는 점수가 늘어나면 안 됩니다. 충돌 검사는 시작 코드에 제공되어 있습니다.

연습 문제 코드 · exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);
    int score = 0;
    double paddle_x = 320;
    double ball_x = 320;
    double ball_y = 100;
    double ball_vy = 180;
    StopWatch watch;
    while (window.is_open())
    {
        double dt = watch.elapsed();
        watch.reset();
        if (window.key_down(Key::Left)) { paddle_x = paddle_x - 250 * dt; }
        if (window.key_down(Key::Right)) { paddle_x = paddle_x + 250 * dt; }
        if (paddle_x < 70) { paddle_x = 70; }
        if (paddle_x > 570) { paddle_x = 570; }
        double previous_y = ball_y;
        ball_y = ball_y + ball_vy * dt;
        if (ball_vy > 0 && previous_y <= 430 && ball_y >= 430 &&
            ball_x >= paddle_x - 70 && ball_x <= paddle_x + 70)
        {
            ball_y = 430;
            ball_vy = -ball_vy;
            // Increase score only on a paddle hit.
            play_sound(Sound::Hit);
        }
        if (ball_y < 20) { ball_y = 20; ball_vy = -ball_vy; }
        if (ball_y > 500) { ball_y = 100; ball_vy = 180; }
        // show score in the window title.
        window.clear(Black);
        window.fill_rectangle(paddle_x - 70, 450, 140, 10, White);
        window.fill_circle(ball_x, ball_y, 20, Yellow);
        window.show();
    }
}

Hint

score는 loop 밖에서 0으로 만들고 충돌 if 안에서 증가시키세요.

풀이 보기
void small_main()
{
    Window window;
    window.open(640, 480);
    int score = 0;
    double paddle_x = 320;
    double ball_x = 320;
    double ball_y = 100;
    double ball_vy = 180;
    StopWatch watch;
    while (window.is_open())
    {
        double dt = watch.elapsed();
        watch.reset();
        if (window.key_down(Key::Left)) { paddle_x = paddle_x - 250 * dt; }
        if (window.key_down(Key::Right)) { paddle_x = paddle_x + 250 * dt; }
        if (paddle_x < 70) { paddle_x = 70; }
        if (paddle_x > 570) { paddle_x = 570; }
        double previous_y = ball_y;
        ball_y = ball_y + ball_vy * dt;
        if (ball_vy > 0 && previous_y <= 430 && ball_y >= 430 &&
            ball_x >= paddle_x - 70 && ball_x <= paddle_x + 70)
        {
            ball_y = 430;
            ball_vy = -ball_vy;
            score = score + 1;
            play_sound(Sound::Hit);
        }
        if (ball_y < 20) { ball_y = 20; ball_vy = -ball_vy; }
        if (ball_y > 500) { ball_y = 100; ball_vy = 180; }
        window.set_title("Score: ", score);
        window.clear(Black);
        window.fill_rectangle(paddle_x - 70, 450, 140, 10, White);
        window.fill_circle(ball_x, ball_y, 20, Yellow);
        window.show();
    }
}
← 이전 레슨다음 레슨 →