基於EasyX和Raylib的別碰方塊

基於 EasyX

// 根據《C和C++遊戲趣味編程》第三章 別碰方塊 寫出

#include <graphics.h>
#include <conio.h> // _kbhit()
#include <stdio.h>

// 檢測按下了空格鍵
void demo_3_1_3()
{
    while (1)
    {
        if (kbhit()) // 當按鍵時
        {
            char input = getchar(); // 獲得輸入字符
            if (input == ' ')
            {
                printf("按下了空格!\n");
            }
        }
    }
}

// 按空格鍵控制小球起跳
void demo_3_2()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        Sleep(10);
    }

    closegraph();
}

// 在繪製小球的同時,繪製靜態方塊
void demo_3_3_1()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
        Sleep(10);
    }

    closegraph();
}

// 繪製小球的同時, 繪製向左勻速移動的方塊: 在 3_3_1 基礎上增加 rect_vx
void demo_3_3_2()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
        }

        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
        Sleep(10);
    }

    closegraph();
}

// 基於demo_3_3_2(), 檢測到小球和方塊碰撞時,Sleep()一段時間實現類似慢動作的效果
void demo_3_4_2()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
        }

        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            Sleep(100); // 慢動作效果
        }

        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
        Sleep(10);
    }

    closegraph();
}

// 當方塊重新出現時, 隨機方塊的速度和高度. 基於 demo_3_4_2
void demo_3_5_4()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100;
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
            rect_height = rand() % int(height / 4) + height / 4; // 方塊高度, 設置爲隨機的高度
            rect_vx = rand() / float(RAND_MAX) * 4 - 7; // [0, 4] - 7 = [-7, -3]
        }

        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            Sleep(100); // 慢動作效果
        }

        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
        Sleep(10);
    }

    closegraph();
}

// 加入得分機制,並顯示得分
// 在書上代碼基礎上, 增加得分的時機從“方塊到達左邊邊界”改爲:當前方塊沒有和小球發生碰撞,並且小球剛剛跳過方塊時,得分增加
void demo_3_6()
{
    float width = 600;
    float height = 400;
    initgraph(width, height);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100;
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    int score = 0;
    bool first_time_being_left = false;
    bool collision = false;

    while (1)
    {
        if (kbhit())
        {
            //char input = getchar(); // getchar() 會等待按下回車
            char input = _getch();  // 要想獲得實時響應,用 _getch()
            if (input == ' ')
            {
                ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
            }
            else if (input == 'q')
            {
                break;
            }
        }

        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        rect_left_x = rect_left_x + rect_vx;
        // 如果方塊跑到最左邊
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
            first_time_being_left = false;
            collision = false;

            rect_height = rand() % int(height / 4) + height / 4; // 方塊高度, 設置爲隨機的高度
            rect_vx = rand() / float(RAND_MAX) * 4 - 7; // [0, 4] - 7 = [-7, -3]
        }

        // 如果小球碰到方塊
        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            Sleep(100); // 慢動作效果
            score = 0; // 得分清零
            collision = true;
        }

        // 如果方塊沒有和小球碰撞,那麼方塊剛剛處於小球左邊時,增加得分
        if (rect_left_x + rect_width < ball_x - radius && first_time_being_left == false && collision == false)
        {
            first_time_being_left = true;
            score++; // 增加得分
        }
        
        cleardevice();
        fillcircle(ball_x, ball_y, radius);
        fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);

        char s[20] = { 0 };
        sprintf(s, "score: %d", score);

        const int fontsize = 40;
        settextstyle(fontsize, 0, _T("Consolas"));

        settextcolor(LIGHTGRAY);
        outtextxy(50, 30, s);

        Sleep(10);
    }

    closegraph();
}

int main()
{
    //demo_3_1_3();
    //demo_3_2();
    //demo_3_3_1();
    //demo_3_3_2();
    //demo_3_4_2();
    //demo_3_5_4();
    demo_3_6();

    return 0;
}

基於 Raylib

// 根據《C和C++遊戲趣味編程》第三章 別碰方塊 寫出

#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>

// 檢測按下了空格鍵
void demo_3_1_3()
{
    InitWindow(600, 600, "don't touch the square");
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        // 這種寫法, 會在每一幀都輸出。感覺不夠靈敏。
        // char input = getchar();
        // if (input == ' ')
        // if (IsKeyDown(KEY_SPACE))
        // {
        //     printf("按下了空格!\n");
        // }
        // else
        // {
        //     printf("不知道啥\n");
        // }

        // 只在檢測到按鍵按下,並且按鍵爲空格的情況下,才輸出內容。符合預期。
        if (GetKeyPressed() == ' ')
        {
            printf("按下了空格!\n");
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 按空格鍵控制小球起跳
void demo_3_2()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    while (!WindowShouldClose())
    {
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}


// 在繪製小球的同時,繪製靜態方塊
void demo_3_3_1()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標

    while (!WindowShouldClose())
    {
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
            DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 繪製小球的同時, 繪製向左勻速移動的方塊: 在 3_3_1 基礎上增加 rect_vx
void demo_3_3_2()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    while (!WindowShouldClose())
    {
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
            DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 基於demo_3_3_2(), 檢測到小球和方塊碰撞時,Sleep()一段時間實現類似慢動作的效果
void demo_3_4_2()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;

    while (!WindowShouldClose())
    {
        rect_vx = -3;
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        // 由於 raylib 是一開始生成窗口時就指定了 FPS, 這裏碰撞的效果不能像 easyx 一樣直接調用 Sleep()
        // 解決辦法是,在更新 rect_left_x 之前判斷和減小 rect_vx 的絕對值。
        // 並且在每一幀的最初處理部分, 恢復 rect_vx = -3
        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            rect_vx = -1;
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
            DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 當方塊重新出現時, 隨機方塊的速度和高度. 基於 demo_3_4_2
void demo_3_5_4()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;
    float old_rect_vx = -3; // 之前用於碰撞減速的trick,更新爲使用變量記錄前一次的速度

    while (!WindowShouldClose())
    {
        rect_vx = old_rect_vx;
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        // 由於 raylib 是一開始生成窗口時就指定了 FPS, 這裏碰撞的效果不能像 easyx 一樣直接調用 Sleep()
        // 解決辦法是,在更新 rect_left_x 之前判斷和減小 rect_vx 的絕對值。
        // 並且在每一幀的最初處理部分, 恢復 rect_vx = -3
        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            old_rect_vx = rect_vx;
            rect_vx = rect_vx + 2;
        }

        rect_left_x = rect_left_x + rect_vx;
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
            rect_height = rand() % int(height / 4) + height / 4; // 方塊高度, 設置爲隨機的高度
            rect_vx = rand() / float(RAND_MAX) * 4 - 7; // [0, 4] - 7 = [-7, -3]
            old_rect_vx = rect_vx;
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
            DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 加入得分機制,並顯示得分
// 在書上代碼基礎上, 增加得分的時機從“方塊到達左邊邊界”改爲:當前方塊沒有和小球發生碰撞,並且小球剛剛跳過方塊時,得分增加
void demo_3_6()
{
    float width = 600;
    float height = 400;
    InitWindow(width, height, "don't touch the square");
    SetTargetFPS(60);

    float gravity = 0.6;
    float radius = 20;
    float ball_x = width / 4;
    float ball_y = height - radius;
    float ball_vy = 0;

    float rect_height = 100; // 方塊高度
    float rect_width = 20; // 方塊寬度
    float rect_left_x = width * 3 / 4; // 方塊左邊 x 座標
    float rect_top_y = height - rect_height; // 方塊頂部 y 座標
    float rect_vx = -3;
    float old_rect_vx = -3; // 之前用於碰撞減速的trick,更新爲使用變量記錄前一次的速度

    int score = 0;
    bool first_time_being_left = false;
    bool collision = false;

    while (!WindowShouldClose())
    {
        rect_vx = old_rect_vx;
        // Update
        if (GetKeyPressed() == ' ')
        {
            ball_vy = -16; // 當按下空格鍵時, 給小球一個向上的初速度
        }
        else if (GetKeyPressed() == 'q') // 對應到 ESC 鍵而不是q鍵,很奇怪
        {
            break;
        }
        ball_vy = ball_vy + gravity; // 根據重力加速度,更新小球y方向速度
        ball_y = ball_y + ball_vy; // 根據小球y方向速度更新其y座標
        if (ball_y >= height - radius) // 如果小球落到地面上
        {
            ball_vy = 0; // y 速度爲0
            ball_y = height - radius; // 規範其y座標,避免落到地面下
        }

        // 如果小球碰到方塊
        // 由於 raylib 是一開始生成窗口時就指定了 FPS, 這裏碰撞的效果不能像 easyx 一樣直接調用 Sleep()
        // 解決辦法是,在更新 rect_left_x 之前判斷和減小 rect_vx 的絕對值。
        // 並且在每一幀的最初處理部分, 恢復 rect_vx = -3
        if ( (rect_left_x <= ball_x + radius)
            && (rect_left_x + rect_width >= ball_x - radius)
            && (height - rect_height <= ball_y + radius))
        {
            score = 0;
            collision = true;
            old_rect_vx = rect_vx;
            rect_vx = rect_vx + 2;
        }

        // 如果方塊沒有和小球碰撞,那麼方塊剛剛處於小球左邊時,增加得分
        if (rect_left_x + rect_width < ball_x - radius && first_time_being_left == false && collision == false)
        {
            first_time_being_left = true;
            score++; // 增加得分
        }

        rect_left_x = rect_left_x + rect_vx;
        // 如果方塊跑到最左邊
        if (rect_left_x <= 0)
        {
            rect_left_x = width; // 在最右邊重新出現
            first_time_being_left = false;
            collision = false;

            rect_height = rand() % int(height / 4) + height / 4; // 方塊高度, 設置爲隨機的高度
            rect_vx = rand() / float(RAND_MAX) * 4 - 7; // [0, 4] - 7 = [-7, -3]
            old_rect_vx = rect_vx;
        }

        char s[20] = { 0 };
        sprintf(s, "score: %d", score);

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(ball_x, ball_y, 20, WHITE);
            DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE);

            const int pos_x = 50;
            const int pos_y = 30;
            const int font_size = 40;
            DrawText(s, pos_x, pos_y, font_size, LIGHTGRAY);
        }
        EndDrawing();
    }

    CloseWindow();
}

int main()
{
    //demo_3_1_3();
    //demo_3_2();
    //demo_3_3_1();
    //demo_3_3_2();
    //demo_3_4_2();
    //demo_3_5_4();
    demo_3_6();

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章