基本功能介紹!

新增功能介紹

程式碼介紹!

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

#define CANVAS 50

void startgame();
void display(char[], int, int); 
/////
void shiftLeft(char *, int); 
void shiftLeft2(char *, int);
void shiftLeft3(char *, int);
////
void changemode(int);
int kbhit(void);
/////
void insertBarrier(char *floor, int n);
void insertlife(char *floor, int n);
void insertspeed(char *floor, int n);
////
int isDead_fl1(int curfloor, int playerPos, char floor1[]);
int isDead_fl2(int curfloor, int playerPos, char floor2[]);
int islife(int curfloor, int playerPos, char floor1[]);
int isspeedup(int curfloor, int playerPos, char floor2[]);
////
////
int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}
int main()
{
    int test = 0;
    int rank[100] = {0}, playtimes = 0;
    startgame();
    while (scanf("%d", &test) && test == 1 || test == 2)
    {
        if (test == 1)
        {
            int playerPos = 6;
            int sleeptime = 22000;
            int jump_time = 0;
            int score = 0;
            int life = 3;
            int delay = 0;
            int floor3_flag = 0;
            int speedup = 100;
            int speed_count = 10;
            int end_game_flag = 0;
            char floor1[CANVAS], floor2[CANVAS], floor3[CANVAS];
            for (int i = 0; i < CANVAS; i++)
            {
                floor1[i] = '_';
                floor2[i] = ' ';
                floor3[i] = ' ';
            }
            int current_floor = 1;
            srand(time(NULL));
            while (1)
            {
                changemode(1);
                while (!kbhit())
                {
                    if (speed_count == 0)
                    {
                        speedup = 0;
                        speed_count = 90;
                    }
                    if (jump_time <= 0)
                    {
                        current_floor = 1;
                    }
                    printf("score : %d", score);
                    if (life > 1)
                    {
                        printf("      life : %d", life);
                    }
                    if (speedup)
                    {
                        printf("  speed time left: %d", speed_count);
                        speed_count--;
                    }
                    if (isDead_fl1(current_floor, playerPos, floor1) || isDead_fl2(current_floor, playerPos, floor2))
                    {
                        if (speedup == 0)
                        {
                            life--;
                            if (life <= 0)
                            {
                                end_game_flag = 1;
                            }
                        }
                    }
                    if (islife(current_floor, playerPos, floor1))
                    {
                        floor1[playerPos] = '_';
                        life++;
                    }
                    if (isspeedup(current_floor, playerPos, floor2))
                    {
                        floor2[playerPos] = ' ';
                        speedup = 1;
                    }
                    if (score % 100 == 1)
                    {
                        insertspeed(floor2, 15);
                    }
                    if (score % 100 == 3)
                    {
                        insertBarrier(floor2, 15);
                    }
                    else if (score % 32 == 3)
                    {
                        insertBarrier(floor1, 15);
                    }
                    if (score % 100 == 1)
                    {
                        insertlife(floor1, 15);
                    }
                    if (current_floor == 1)
                    {
                        printf("\n\n\t");
                        display(floor2, 0, CANVAS);
                        printf("\n\t");
                        display(floor1, 0, playerPos);
                        printf("0");
                        display(floor1, playerPos + 1, CANVAS);
                        printf("\n\n");
                        delay = 0;
                    }
                    else if (current_floor == 2)
                    {
                        jump_time--;
                        printf("\n\n\t");
                        display(floor2, 0, playerPos);
                        printf("0");
                        display(floor2, playerPos + 1, CANVAS);
                        printf("\n\t");
                        display(floor1, 0, CANVAS);
                        printf("\n\n");
                    }
                    else if (current_floor == 3)
                    {   
                        printf("\n\t");
                        display(floor3, 0, playerPos);
                        printf("0");
                        display(floor3, playerPos + 1, CANVAS);
                        printf("\n\t");
                        display(floor2, 0, CANVAS);
                        printf("\n\t");
                        display(floor1, 0, CANVAS);
                        printf("\n\n");
                        jump_time--;
                    }
                    shiftLeft(floor1, CANVAS);
                    shiftLeft2(floor2, CANVAS);
                    if (speedup)
                    {
                        usleep(sleeptime * 4 / 7);
                    }
                    else
                    {
                        if (sleeptime > 15000)
                        {
                            usleep(sleeptime -= 10);
                        }
                        else if (sleeptime > 12000)
                        {
                            usleep(sleeptime -= 5);
                        }
                        else
                        {
                            usleep(sleeptime);
                        }
                    }
                    score++;
                    system("clear");
                    if (end_game_flag)
                    {
                        break;
                    }
                }
                if (kbhit())
                {
                    char a = getchar();
                    if (a == 32 && current_floor == 1)
                    {
                        current_floor = 2;
                        jump_time = 6;
                    }
                    else if (current_floor == 2)
                    {
                        current_floor = 3;
                        jump_time = 6;
                    }
                    else
                        continue;
                }
                if (end_game_flag)
                {
                    printf("your score is %d\n", score);
                    break;
                }
                changemode(0);
            }
            rank[playtimes] = score;
            playtimes++;
            startgame();
        }
        else
        {
            qsort(rank, playtimes, sizeof(int) , cmp);
            system("clear");
            printf("==============================================\n");
            for(int i = 0 ; i < playtimes ; i++)
            {
                printf("%d.%d\n" , i+1 ,rank[i]);
            }
            printf("==============================================\n");
            startgame();
        }
    }

    return 0;
}
void startgame()
{
    printf("choose one mode\n");
    printf("1. start game\n");
    printf("2.ranknig\n");
    printf("3.quit\n");
}
void display(char *floor, int lowerlimit, int upperlimit)
{
    for (int i = lowerlimit; i < upperlimit; i++)
    {
        printf("%c", floor[i]);
    }
}

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

void shiftLeft2(char *floor, int upperlimit)
{
    for (int i = 0; i < upperlimit - 1; i++)
    {
        floor[i] = floor[i + 1];
    }
    floor[upperlimit - 1] = ' ';
}

void shiftLeft3(char *floor, int upperlimit)
{
    for (int i = 0; i < upperlimit - 1; i++)
    {
        floor[i] = floor[i + 1];
    }
    floor[upperlimit - 1] = ' ';
}
void changemode(int dir)
{
    static struct termios oldt, newt;
    if (dir == 1)
    {
        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    }
    else
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}

int kbhit(void)
{
    struct timeval tv;
    fd_set rdfs;

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    FD_ZERO(&rdfs);
    FD_SET(STDIN_FILENO, &rdfs);

    select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
    return FD_ISSET(STDIN_FILENO, &rdfs);
}

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

void insertlife(char *floor, int n)
{
    int r = rand() % n;
    floor[CANVAS - r] = '+';
}
///////////////////dead//////////////
int isDead_fl1(int curfloor, int playerPos, char floor1[])
{
    if (floor1[playerPos] == 'X' && curfloor == 1)
    {
        return 1;
    }
    return 0; //沒撞到東西,
}
int isDead_fl2(int curfloor, int playerPos, char floor2[])
{
    if (floor2[playerPos] == 'X' && curfloor == 2)
    {
        return 1;
    }
    return 0; //沒撞到東西,
}
//////////////// life ++/////////////////
int islife(int curfloor, int playerPos, char floor1[])
{
    if (floor1[playerPos] == '+' && curfloor == 1)
    {
        return 1;
    }
    return 0; //沒撞到東西,
}
////////// Speed //////////
void insertspeed(char *floor, int n)
{
    int r = rand() % n;
    floor[CANVAS - r] = '>';
}
int isspeedup(int curfloor, int playerPos, char floor2[])
{
    if (floor2[playerPos] == '>' && curfloor == 2)
    {
        return 1;
    }
    return 0; //沒撞到東西,
}

增加功能講解

  1. 血條功能

    hen簡單 只要剛開始先print血條 預設為三

    if (life > 1)
    {
    	printf("      life : %d", life);
    }
    /////////
    int islife(int curfloor, int playerPos, char floor1[])
    {
        if (floor1[playerPos] == '+' && curfloor == 1)
        {
            return 1;
        }
        return 0; //沒撞到東西,
    }
    /////////
    
  2. 補包功能

    用insert barrier 的方式增加補包

    void insertlife(char *floor, int n)
    {
        int r = rand() % n;
        floor[CANVAS - r] = '+';
    }
    
  3. 加血功能

    跟isdead方法一樣 然後更改要做的事

    if (islife(current_floor, playerPos, floor1))
                        {
                            floor1[playerPos] = '_';
                            life++;
                        }
    
  4. 加速功能

同上面的做法

void insertspeed(char *floor, int n)
{
    int r = rand() % n;
    floor[CANVAS - r] = '>';
}
/////
int isspeedup(int curfloor, int playerPos, char floor2[])
{
    if (floor2[playerPos] == '>' && curfloor == 2)
    {
        return 1;
    }
    return 0; //沒撞到東西,
}

https://replit.com/@stu98035/little-din#main.c

謝謝老師