Lesson21 PoEdu培训第二课 C语言篇(13) 函数 打字程序作业
文章类别: 培训作业 0 评论

Lesson21 PoEdu培训第二课 C语言篇(13) 函数 打字程序作业

文章类别: 培训作业 0 评论

老师未批改

作业

完成一个控制台的打字游戏

程序代码

#include <stdio.h>
#include <windows.h>
#include <stdbool.h>    // 提供true

#define BASE_LEVEL_SPEED 500    // 基本等级下落速度
#define LEVEL_UP_SCORE 50        // 每升一级的分数
#define DOWN_MAX 20                // 最多下落多少次判定失败
#define ONE_SCORE 10            // 没对或错一次加减分数
#define FAILD_SCORE -10            // 失败的分数

// 定义枚举类型
typedef enum _SPECIAL_CHAR_TYPE {
    SPACE_CHAR = 0,
    TAB_CHAR,
    ENTER_CHAR
} SPECIAL_CHAR_TYPE;

// 奇淫巧技
#define midleDisplay(str) midleDisplayStr(str, 1)
#define printSpace(num) printSpecialChar(num, SPACE_CHAR)
#define printTab(num) printSpecialChar(num, TAB_CHAR)
#define printEnter(num) printSpecialChar(num, ENTER_CHAR)

//////////////////////////////////////////////////////////////////////////
// 打印空格 回车 制表符
void printSpecialChar(int num, SPECIAL_CHAR_TYPE eType)
{
    char ch[3] = { '\0' };
    switch (eType)
    {
    case SPACE_CHAR:
        sprintf_s(ch, 3, "%c", ' ');
        break;
    case TAB_CHAR:
        sprintf_s(ch, 3, "%c", '\t');
        break;
    case ENTER_CHAR:
        sprintf_s(ch, 3, "%s", "\r\n");
        break;
    default:
        break;
    }
    for (int i = 0; i < num; i++)
    {
        printf("%s", ch);
    }
}

//////////////////////////////////////////////////////////////////////////
// 居中屏幕显示
int midleDisplayStr(char* strDisplay, int isCls)
{
    if (isCls)
    {
        // 清屏
        system("cls");
        printEnter(15);
    }
    printSpace(40);
    printf("%s\n", strDisplay);
    return 0;
}

//////////////////////////////////////////////////////////////////////////
// 显示游戏头
void displayTitle(int levels, int score)
{
    // 清屏
    system("cls");
    // 打印等级
    printSpace(13);
    printf("Levels:[%d]", levels);
    printSpace(36);
    // 打印分数
    printf("Score:[%d]\r\n", score);
    // 打印提示
    printSpace(10);
    printf("Press 1 to Pasue");
    printTab(4);
    printf("Press 0 to Exit.\r\n");

    for (int i = 0; i < 80; i++)
    {
        printf("-");
    }

    printf("\r\n");
}

//////////////////////////////////////////////////////////////////////////
// 显示游戏结束
void gameOver()
{
    system("cls");
    midleDisplay("You Failed! Press Any Key to Exit!");
    _getch();
    exit(0);
}

//////////////////////////////////////////////////////////////////////////
// 检测键盘事件
int checkInput(char cCheckChar, int* score, int* lines, int levels)
{
    // 响应键盘
    if (kbhit())
    {
        char ch = _getch();
        if (ch == 0xE0 || ch == 0)
        {
            // shift
            ch = _getch();
        }
        if (ch == cCheckChar)
        {
            *score += ONE_SCORE;
            *lines = 0;
            return 1;
        }
        else if (ch == '1')
        {
            midleDisplay("Game Pause, Press 1 to Continue!");
            while (_getch() != '1');
            displayTitle(levels, *score);
            printEnter(*lines);
        }
        else if (ch == '0')
        {
            midleDisplay("Are you sure to Exit the Game?\n\n");
            midleDisplayStr("Press 1 to Resume and Press AnyKey Otherwise to Exit!", 0);
            if (_getch() != '1')
            {
                exit(0);
            }
            displayTitle(levels, score);
            printEnter(*lines);
        }
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////
// 计算等级
int calcLevel(int score)
{
    int level = score / LEVEL_UP_SCORE;
    return level ? level : 1;
}

//////////////////////////////////////////////////////////////////////////
// 检测是否达到失败条件
int checkFailed(int* lines, int* score)
{
    if (*lines > DOWN_MAX)
    {
        *score -= ONE_SCORE;
        *lines = 0;
        if (*score < FAILD_SCORE)
        {
            gameOver();
        }
        return 1;
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////
// 初始化要打的字符
char initOutputChar(int* col)
{
    // 置随机数种子
    srand((unsigned)time(NULL));
    // 初始化随机显示字符
    int num = rand() % 26;
    // 初始化显示的列
    *col = rand() % 80;
    // 大小写字母
    int iType = rand() % 2;

    switch (iType)
    {
    case 0:
        return 'A' + num;
    default:
        return 'a' + num;
    }
}

int initRunEnv()
{
    // 隐藏光标
    // HWND hOut = GetConsoleWindow();
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO curInfo;
    curInfo.dwSize = 1;             // 如果没赋值的话,隐藏光标无效  
    curInfo.bVisible = FALSE;
    SetConsoleCursorInfo(hOut, &curInfo);
    return 0;
}

//////////////////////////////////////////////////////////////////////////
// 主函数
int main()
{
    initRunEnv();
    midleDisplay("Press Anykey to Start Game!");
    _getch();
    // 清屏
    system("cls");
    // 等级
    int levels = 1;
    // 分数
    int score = 0;
    // 下落的行数
    int lines = 0;
    // 列数的计算
    int col = 0;

    while (true)
    {
        displayTitle(levels, score);
        
        char cOutputChar = initOutputChar(&col);

        while (TRUE)
        {
            levels = calcLevel(score);
            if (1 == checkFailed(&lines, &score))
            {
                break;
            }
            
            lines++;
            printSpace(col);
            printf("%c\b", cOutputChar);
            Sleep(BASE_LEVEL_SPEED / levels);
            printf("\b  \r\n");

            if (1 == checkInput(cOutputChar, &score, &lines, levels))
            {
                break;
            }
        }
    }
    return 0;
}

运行效果:
Alt 运行效果

如有错误,请提出指正!谢谢.

回复