---
# System prepended metadata

title: 小恐龍 Dino

---

# 小恐龍 Dino
1. 加入三個可互動的地圖物件
* X==>障礙物
* C==>可以清除障礙物的道具,如果碰到這個道具可以將地圖上的障礙物清空
* R==>可以累計復活次數的道具,假如碰到X掛了，如果可復活次數>=1則可以選擇原地復活
![](https://i.imgur.com/S7b6Dny.png)
2. 加裝選擇難易度的選單
輸入1,2可以選擇難易度,hard模式的:
* 障礙物較多
* 可以清除障礙物的道具較少
* 可以復活的道具較少
* 初始速度較快
easy模式則相反
![](https://i.imgur.com/PJAndlK.png)
3. 加入復活選項
* 如果持有復活的道具，則可以原地復活。
* 若沒有道具還選擇復活則印出too bad然後break。
![](https://i.imgur.com/UksHFXi.png)
4. 每過一段時間加速
* 當分數累積到100的倍數時，print出SPEED UP,然後速度會加快(SPEED UP共會印出8單位的時間，避免速度過快看不到，如圖)
![](https://i.imgur.com/fXydZzf.png)


### 以下是程式碼
```c=
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h> 
#define _CRT_SECURE_NO_WARNINGS

#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;         //放置障礙物的機率
    int clear_chance;
    int revive_chance;
    int revive_time;
    // int bullet_chance;
};
typedef struct dino Dino;

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

void move(Dino* MyDino) {
    for (int i = 0; i < ROAD_LENGTH - 1; 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;
        MyDino->clear_chance = 10;
        MyDino->revive_chance = 1;
        MyDino->revive_time = 0;

        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\n\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");
    printf("\t\033[40;31mX=barrier\033[0m");
    printf("\t\033[40;34mC=clear all barrier\033[0m");
    printf("\t\033[40;32mR=revive time +1\033[0m\n");
    printf("\t\t    your remain revive time:%d", MyDino->revive_time);
    printf("\n\n\t");
    printf("\t\tyour score is : %d\n", MyDino->score);
    if (MyDino->score % 100 == 0 || MyDino->score % 100 == 1 ||
        MyDino->score % 100 == 2 || MyDino->score % 100 == 3 ||
        MyDino->score % 100 == 4 || MyDino->score % 100 == 5 ||
        MyDino->score % 100 == 6 || MyDino->score % 100 == 7 ||
        MyDino->score % 100 == 8) {
        printf("\t\t\t\033[40;31m    SPEED UP!!\033[0m\n");
    }

    return;
}
void speed_up(Dino* MyDino) { MyDino->wait_time -= 5; }
bool is_bumped_X(Dino* MyDino) {
    return MyDino->jump_time_left == 0 && MyDino->road[DINO_PLACE] == 'X';
}
bool is_bumped_C(Dino* MyDino) {
    return MyDino->jump_time_left == 0 && MyDino->road[DINO_PLACE] == 'C';
}
bool is_bumped_R(Dino* MyDino) {
    return MyDino->jump_time_left == 0 && MyDino->road[DINO_PLACE] == 'R';
}
bool spawn_barrier(Dino* MyDino) {
    int random_number = rand() % 100;
    if (random_number < MyDino->barrier_chance)
        return true;
    return false;
}
bool spawn_clear(Dino* MyDino) {
    int random_number = rand() % 1000;
    if (random_number < MyDino->clear_chance)
        return true;
    return false;
}
bool spawn_revive(Dino* MyDino) {
    int random_number = rand() % 1000;
    if (random_number < MyDino->revive_chance)
        return true;
    return false;
}
int main() {
    /* 初始化 */
    srand(time(NULL));
    Dino* MyDino = init();
    int mode;
    int play_again;
    printf("\tchoose mode:\n");
    printf("1:easy\n");
    printf("2:hard\n");
    scanf("%d", &mode);
    if (mode == 1) {
        MyDino->barrier_chance = 10;
        MyDino->wait_time = 150;
        MyDino->clear_chance = 15;
        MyDino->revive_chance = 3;
    }
    if (mode == 2) {
        MyDino->barrier_chance = 15;
        MyDino->wait_time = 100;
        MyDino->clear_chance = 7;
        MyDino->revive_chance = 2;
    }
    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 (spawn_clear(MyDino)) {
            MyDino->road[ROAD_LENGTH - 1] = 'C';
        }
        if (spawn_revive(MyDino)) {
            MyDino->road[ROAD_LENGTH - 1] = 'R';
        }
        if (MyDino->jump_time_left > 0)
            MyDino->jump_time_left -= 1;
        if (MyDino->score % 100 == 0) {
            speed_up(MyDino);
        }
        if (is_bumped_X(MyDino)) {
            display(MyDino);
            printf("\n\t\t\t");
            printf("  \033[40;31m  GAME OVER\033[0m\n");
            printf("\t\t\t     REVIVE?\n");
            printf("\t\t       1 to revive 2 to end \n\t\t\t\t");
            scanf("%d", &play_again);
            if (play_again == 1 && MyDino->revive_time >= 1) {
                MyDino->revive_time -= 1;
                continue;
            }
            if (play_again == 1 && MyDino->revive_time == 0) {
                printf("\n\t\t\033[40;31mtoo bad you don't have enough revive time "
                    ":(\033[0m\n");
                break;
            }
            if (play_again == 2) {
                printf("\n\t\t\t\033[40;31m   BYE BYE!!:)\033[0m\n");
                break;
            }
        }
        if (is_bumped_C(MyDino)) {
            display(MyDino);
            for (int i = 0; i < ROAD_LENGTH; i++) {
                if (MyDino->road[i] == 'X') {
                    MyDino->road[i] = '_';
                }
            }
        }
        if (is_bumped_R(MyDino)) {
            display(MyDino);
            MyDino->revive_time += 1;
        }
        /* 渲染呈現 */
        display(MyDino);
        MyDino->score++;
        Sleep(MyDino->wait_time);
    }
    return 0;
}
```
