---
# System prepended metadata

title: 小專題3 小恐龍進化了 ?!?!

---

# 小專題3 小恐龍進化了 ?!?!
## 主選單 & 選擇難度
```c=
void main_menu(Dino* MyDino)
{
    int difficulty;
    system("cls");

    printf("\n\n\n\n");
    printf("\t\t=======================================\n");
    printf("\t\t||                                   ||\n");
    printf("\t\t||          AdventureDino            ||\n");
    printf("\t\t||                                   ||\n");
    printf("\t\t=======================================\n\n\n\n");
    printf("\t\t          Select difficulty  \n");
    printf("\t\t 1. Normal\n");
    printf("\t\t 2. Hard\n");
    printf("\t\t Enter your choice (1 or 2): ");

    scanf("%d", &difficulty);
    switch (difficulty) {
    case 1:
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
    case 2:
        MyDino->wait_time = 75;
        MyDino->barrier_chance = 20;
        break;
    default:
        printf("\t    Invalid choice. Defaulting to normal difficulty.\n");
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
    }

    _getch();

    return;
}
```
在遊戲開始之前先在主選單選擇難度(普通或是困難)

![](https://i.imgur.com/tVzk1IM.png)

輸入1或是2來選擇難度，若是選擇1或是2以外的數字會報錯並且預設難度1進行遊戲
![](https://i.imgur.com/y1fOHqv.png)

```c=
switch (difficulty) {
    case 1:
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
    case 2:
        MyDino->wait_time = 75;
        MyDino->barrier_chance = 20;
        break;
    default:
        printf("\t    Invalid choice. Defaulting to normal difficulty.\n");
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
```
在困難難度下，遊戲速度更快、障礙物出現的機率更高

## 再玩一次
```c=
int main()
{
    /* 初始化 */
    srand(time(NULL));
    Dino* MyDino = init();


    /* 遊戲本體 */
    while (true)
    {
        main_menu(MyDino);

        display(MyDino);

        while (true)
        {
            /* 邏輯運算 */
            if (space_pressed() && MyDino->jump_time_left == 0)
            {
                MyDino->jump_time_left = 7;
            }
            move(MyDino);
            if (spawn_barrier(MyDino))
            {
                MyDino->road[ROAD_LENGTH - 1] = 'X';
            }
            if (MyDino->jump_time_left > 0)
                MyDino->jump_time_left -= 1;
            if (is_bumped(MyDino))
            {
                display(MyDino);
                break;
            }

            /* 渲染呈現 */
            display(MyDino);
            MyDino->score++;
            Sleep(MyDino->wait_time);
            MyDino->wait_time = MyDino->wait_time > 50 ? MyDino->wait_time - 1 : 50;
        }
        printf("\n\n\n\t  GAME OVER！Press any key to play the game again\n");
        _getch();
        MyDino = init();
    }
    return 0;
}
```

遊戲結束之後顯示GAME OVER並且按下任意鍵可以回到主選單重新選擇難度再玩一次

![](https://i.imgur.com/GzZddWt.png)

## 完整程式碼
```c=
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<windows.h>
#include<time.h>

#define ROAD_LENGTH 50 //路面長度
#define DINO_PLACE 6   //小恐龍在路面上的第幾格

struct dino {
    char road[ROAD_LENGTH + 1];//字串結尾需要'\0'
    int jump_time_left;      //還剩多久掉回地面
    int score;               //分數
    int wait_time;           //每次迴圈之間等待的時間
    int barrier_chance;      //放置障礙物的機率
};
typedef struct dino Dino;

bool space_pressed(void)
{
    return _kbhit() != 0 && _getch() == 32;
}

void move(Dino* MyDino)
{
    for (int i = 0; i < ROAD_LENGTH; i++)
    {
        MyDino->road[i] = MyDino->road[i + 1];
    }
    MyDino->road[ROAD_LENGTH - 1] = '_';
}

Dino* init(void)
{
    Dino* MyDino = (Dino*)malloc(sizeof(Dino));

    if (MyDino != NULL)
    {
        MyDino->score = 0;
        MyDino->jump_time_left = 0;
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;

        for (int i = 0; i < ROAD_LENGTH; i++)
        {
            MyDino->road[i] = '_';
        }

        MyDino->road[ROAD_LENGTH] = '\0';
    }

    return MyDino;
}

void display(Dino* MyDino)
{
    system("cls");

    printf("\n\n\t");



    for (int i = 0; i < ROAD_LENGTH; i++)
    {
        if (i == DINO_PLACE && MyDino->jump_time_left > 0)
            printf("O");
        else
            printf(" ");
    }
    printf("\n\t");


    for (int i = 0; i < ROAD_LENGTH; i++)
    {
        if (i == DINO_PLACE && MyDino->jump_time_left == 0)
            printf("O");
        else
            printf("%c", MyDino->road[i]);
    }

    printf("\n\n\n\t");
    printf("\t\tYour score is : %d\n", MyDino->score);

    return;
}

bool is_bumped(Dino* MyDino)
{
    return MyDino->jump_time_left == 0 && MyDino->road[DINO_PLACE] == 'X';
}

bool spawn_barrier(Dino* MyDino)
{
    int random_number = rand() % 100;
    if (random_number < MyDino->barrier_chance)
        return true;
    return false;
}

void main_menu(Dino* MyDino)
{
    int difficulty;
    system("cls");

    printf("\n\n\n\n");
    printf("\t\t=======================================\n");
    printf("\t\t||                                   ||\n");
    printf("\t\t||          AdventureDino            ||\n");
    printf("\t\t||                                   ||\n");
    printf("\t\t=======================================\n\n\n\n");
    printf("\t\t          Select difficulty  \n");
    printf("\t\t 1. Normal\n");
    printf("\t\t 2. Hard\n");
    printf("\t\t Enter your choice (1 or 2): ");

    scanf("%d", &difficulty);
    switch (difficulty) {
    case 1:
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
    case 2:
        MyDino->wait_time = 75;
        MyDino->barrier_chance = 20;
        break;
    default:
        printf("\t    Invalid choice. Defaulting to normal difficulty.\n");
        MyDino->wait_time = 150;
        MyDino->barrier_chance = 10;
        break;
    }

    _getch();

    return;
}

int main()
{
    /* 初始化 */
    srand(time(NULL));
    Dino* MyDino = init();


    /* 遊戲本體 */
    while (true)
    {
        main_menu(MyDino);

        display(MyDino);

        while (true)
        {
            /* 邏輯運算 */
            if (space_pressed() && MyDino->jump_time_left == 0)
            {
                MyDino->jump_time_left = 7;
            }
            move(MyDino);
            if (spawn_barrier(MyDino))
            {
                MyDino->road[ROAD_LENGTH - 1] = 'X';
            }
            if (MyDino->jump_time_left > 0)
                MyDino->jump_time_left -= 1;
            if (is_bumped(MyDino))
            {
                display(MyDino);
                break;
            }

            /* 渲染呈現 */
            display(MyDino);
            MyDino->score++;
            Sleep(MyDino->wait_time);
            MyDino->wait_time = MyDino->wait_time > 50 ? MyDino->wait_time - 1 : 50;
        }
        printf("\n\n\n\t  GAME OVER！Press any key to play the game again\n");
        _getch();
        MyDino = init();
    }
    return 0;
}
```

