---
# System prepended metadata

title: 程設分享 -- 小恐龍

---

# 程設分享 -- 小恐龍
## 程式碼介紹(新增功能)
* 開始介面
```c=
int flag = 0;
    printf("\n\n\n\tPlease press SPACE to start.\n\t");
    if(getch() == 32)
    {
        flag = 1;
    }
while (flag == 1)
{....}
```
* 無敵模式(Invincible mode)
```c=
int main()
{
    ...
    if(meetStar (curFloor, playerpos, floor1) == 1)
        {
            mode = 2;             
            if(mode == 2)
            {
                startime = 10;
                printf("\tINVINCIBLE MODE\n");
            }
        }
        if(startime > 0)
        {
            startime--;
            printf("\tTime : %d\n\t", startime);
        }
        if(startime == 0)
        {
            mode = 1;
        }
        if(mode == 1)
        {
            printf("\tNORMAL MODE\n");
            if(isDead(curFloor, playerpos, floor1) == 1)
            {
                dead = 1;
                if(dead == 1)
                {
                    printf("\n\t");
                    puts("--------------------------------------------------");
                    printf("\tFINAL SCORE: %d\n\t", score);
                    puts("--------------------------------------------------");
                }
                break;
            }
        }
}

void star(char *floor, int n)
{
	int r = rand() % n;
	floor[CANVAS - r] = '*';
}

int meetStar(int curFloor, int playerpos, char *floor1)
{
	if(curFloor == 1 && floor1[playerpos] == '*')
	{
		return 1;
	}	
    else
    {
        return 0;
    }
}
```
```
新增star與meetStar副函式
碰'*'就會有10秒的無敵時間(碰到障礙物不會死)
```
## 完整程式碼
```c=
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define CANVAS 50

void display(char[], int, int);
void shiftLeft(char *);
void insertBarrier(char *, int);
int isDead(int, int, char *);
void star(char *, int);
int meetStar(int, int, char *);
 
int main()
{
    int flag = 0;
    printf("\n\n\n\tPlease press SPACE to start.\n\t");
    if(getch() == 32)
    {
        flag = 1;
    }  
    srand(time(NULL));
    char floor1[CANVAS], floor2[CANVAS];
    int playerpos = 6;
    int sleeptime = 50;
    int curFloor = 1; 
    int runtime = 0;
    int score = 0;
    int startime = 0;
    int mode = 1;
    int dead = 0;
    int i;
        
    for (i = 0; i < CANVAS; i++)
    {
        floor1[i] = '_';
        floor2[i] = ' ';
    }
    floor1[CANVAS - 1] = 'X';
    
    while (flag == 1)
    {
        score++;
        if(kbhit() && getch() == 32)
        {
            if(curFloor == 1)
            {
                runtime = 4;
                curFloor = 2; 
            }                                                     
        }
        if(runtime > 0)
        {
            runtime--;
        }
        if(runtime == 0)
        {
            curFloor = 1; 
        }
        if(curFloor == 1)
        {
            printf("\n\n\n\t");  
            display(floor2, 0, CANVAS);
            printf("\n\t");
            display(floor1, 0, playerpos);
            printf("O");
            display(floor1, playerpos+1, CANVAS);
            printf("\n");   
        } 
        else if(curFloor == 2)
        {
            printf("\n\n\n\t");
            display(floor2, 0, playerpos);
            printf("O");
            display(floor2, playerpos+1, CANVAS);
            printf("\n\t");
            display(floor1, 0, CANVAS);
            printf("\n");
        } 
        printf("\n\t"); 
        printf("SCORE: %d\n", score);

        if(score % 10 == 0)
        {
            insertBarrier(floor1, 15);
        }
        if(score % 50 == 0 && score != 0)
        {
            floor1[CANVAS - 1] = '*';
            star(floor1, 20);
        }
        if(meetStar (curFloor, playerpos, floor1) == 1)
        {
            mode = 2;             
            if(mode == 2)
            {
                startime = 10;
                printf("\tINVINCIBLE MODE\n");
            }
        }
        if(startime > 0)
        {
            startime--;
            printf("\tTime : %d\n\t", startime);
        }
        if(startime == 0)
        {
            mode = 1;
        }
        if(mode == 1)
        {
            printf("\tNORMAL MODE\n");
            if(isDead(curFloor, playerpos, floor1) == 1)
            {
                dead = 1;
                if(dead == 1)
                {
                    printf("\n\t");
                    puts("--------------------------------------------------");
                    printf("\tFINAL SCORE: %d\n\t", score);
                    puts("--------------------------------------------------");
                }
                break;
            }
        }
        Sleep(sleeptime);
        system("cls");
        shiftLeft(floor1);
    } 
}

void display(char floor[], int lowerlimit, int upperlimit)
{
    int i;
    for (i = lowerlimit; i < upperlimit; i++)
    {
        printf("%c", floor[i]);
    }
}

void shiftLeft(char *floor)
{
    int i;
    for(i = 0; i < CANVAS - 1; i++)
    {
        floor[i] = floor[i+1];
    }
    floor[CANVAS - 1] = '_';
}

void insertBarrier(char *floor, int n)
{
    int r = rand() % n;
    floor[CANVAS - r] = 'X';
}

int isDead(int curFloor, int playerpos, char *floor1)
{
    if(curFloor == 1 && floor1[playerpos] == 'X')
    {
        return 1;
    }
    else
    {
        return 0;
    } 
}

void star(char *floor, int n)
{
	int r = rand() % n;
	floor[CANVAS - r] = '*';
}

int meetStar(int curFloor, int playerpos, char *floor1)
{
	if(curFloor == 1 && floor1[playerpos] == '*')
	{
		return 1;
	}	
    else
    {
        return 0;
    }
}
```