打字游戏-第一个小项目

话不多说,直接上代码

#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;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值