打字游戏-第一个小项目

来源:互联网 发布:网站源码网 编辑:程序博客网 时间:2024/04/26 13:41

话不多说,直接上代码

#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <windows.h>#include <time.h>int levels = 1, score = 0, lines = 0, num = 0, col = 0;void EnterSpace(int num){    for (int i=0;i<num;i++)    {        printf(" ");    }}void EnterLine(int num){    for (int i = 0; i < num; i++)    {        printf("\r\n");    }}void EnterUnline(int num){    for (int i = 0; i < num; i++)    {        printf("_");    }    EnterLine(1);}// 游戏的初始化--画出打字的简易界面void initialize(int score){    levels = score / 50;    system("cls");    EnterSpace(35);    printf("levels:%d\t ", levels);    printf("\tscore:%d\n", score);    EnterSpace(35);    printf("1---Pause\t");    printf("\t0---Exit\r\n");    EnterUnline(100);    //EnterLine(16);    //EnterUnline(100);}// 处理按键int keydown(char c){           if (c == 'A' + num)        {            lines = 0;            score += 10;            return 1;        }        else if (c == '1')        {            getchar();            return 0;        }        else if (c == '0')        {            exit(0);            return 0;        }}void BeforeStart(){    EnterLine(15);    EnterSpace(40);    printf("press anykey to start");    getchar();}// 游戏结束void End(){    system("cls");    EnterLine(15);    EnterSpace(40);    printf("sorry,you faile!");    getch();    exit(0);}void Speed(int score){    if (score <= 50)        Sleep(1000);    else if (score <= 100)        Sleep(500);    else        Sleep(200);}int main(){    BeforeStart();    srand(time(NULL));    while (true)    {        initialize(score);        col = rand() % 100;        num = rand() % 26;        while (true)        {            lines++;            if (lines > 15)            {                lines = 0;                score -= 10;                if (score < -50)                {                    End();                }                break;            }            EnterSpace(col);            printf("%c", 'A'+num);            Speed(score);            printf("\b \n");            if (kbhit())            {                char c = getch();                int book=keydown(c);                if (book==1)                    break;                  }        }    }    return 0;}

这是效果图
开始界面
运行界面

0 0
原创粉丝点击