简单的c语言实现打字小游戏

运行效果
在这里插入图片描述
账号密码都是123
输入账号密码后回车
在这里插入图片描述
进入游戏

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include <conio.h>
#include <time.h>
#include <Windows.h>

#define _CRT_SECURE_NO_WARNINGS 

using namespace std;
int now_count=0;//当前得分
#define  KLETTER_COUNT    50
#define  KSPEED    50

char last_key;
int now_key=0;//当前的key值
//定义子弹或者字符
typedef struct
{
    int  x; //定义坐标
    int  y;
    char ch;//定义字符
    int isDisplay; //是否显示 0 不显示 否则显示
    int life; //定义声明周期
}LETTER;

//字母初始化
LETTER  letter[KLETTER_COUNT];

void init_letter(void)
{
    srand((unsigned)time(NULL));
    for (int i = 0; i < KLETTER_COUNT; i++)
    {
        letter[i].ch = rand() % 26 + 'A';
        letter[i].x = rand() % 80;
        //printf("letter[%d].x = %d.\n",i,letter[i].x);
        letter[i].y = i * (-2);
        //printf("letter[%d].y= %d.\n",i,letter[i].y);
        letter[i].life = 1;
    }
}


//子弹初始化
LETTER bullet;
void init_bullet(int x)
{
    bullet.ch = 30;
    bullet.x = x;
    bullet.y = 25;
    bullet.life = 1;
}

//坐标切换
void  gotoxy(int x, int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, pos);
}

//绘制字母
void draw_letter(char ch, int x, int y)
{
    gotoxy(x, y);
    printf("%c", ch);
}
//绘制得分
void draw_count(int count, int x, int y)
{
    gotoxy(x, y);
    printf("当前得分:%d", count);
}

void draw_count_key(int key, int x, int y)
{
    gotoxy(x, y);
    printf("当前key:%d", key);
}
//判断字母是否显示
void isLetterDisplay()
{
    for (int i = 0; i < KLETTER_COUNT; i++)
    {
        if (0 == letter[i].life)
        {
            letter[i].isDisplay = 0;
        }
        else if (letter[i].y < 0 || letter[i].y>24)
        {
            letter[i].isDisplay = 0;
        }
        else
        {
            letter[i].isDisplay = 1;
        }
    }
}

//让所有的字母下降
void  letterMov()
{
    isLetterDisplay(); //先更新所有字母的状态 
    for (int i = 0; i < KLETTER_COUNT; i++) //所有的字母状态更新一遍
    {
        if (letter[i].y < 25)
        {
            if (letter[i].isDisplay)
            {
                draw_letter(' ', letter[i].x, letter[i].y);
                draw_letter(letter[i].ch, letter[i].x, letter[i].y + 1);
                //显示当前key
                if (last_key == ' ') {
                    last_key = letter[i].ch;
                    char key_s[10];
                    //  sprintf(key_s, "%c", letter[i].ch);
                    key_s[0] = 't';
                    key_s[1] = 'i';
                    key_s[2] = 't';
                    key_s[3] = 'l';
                    key_s[4] = 'e';
                    key_s[5] = ' ';
                    key_s[6] = letter[i].ch;
                    key_s[7] = '\0';

                    //   system(key_s);
                    system(key_s);
                }
                else {
                    if ( letter[i].ch== last_key ) {
               
                    }
                    else {
                        char key_s[10];
                        //  sprintf(key_s, "%c", letter[i].ch);
                        key_s[0] = 't';
                        key_s[1] = 'i';
                        key_s[2] = 't';
                        key_s[3] = 'l';
                        key_s[4] = 'e';
                        key_s[5] = ' ';
                        key_s[6] = ((letter[i].ch));
                        key_s[7] = '\0';
                        now_key = (letter[i].ch);
                        draw_count_key(now_key, 2, 2);

                        system(key_s);
                        last_key = letter[i].ch;
                    }//更新

                }
                

            }
            else
            {
                draw_letter(' ', letter[i].x, letter[i].y);
            }
            letter[i].y++;
        }
        else
        {
            draw_letter(' ', letter[i].x, letter[i].y);
        }
    }
}

//子弹向上
void bulletMov()
{
    if (bullet.life == 1)
    {
        draw_letter(' ', bullet.x, bullet.y);
        draw_letter(bullet.ch, bullet.x, bullet.y - 1);
        bullet.y--;
    }
}

//动画
void  flash_ip()
{
    for (int j = 0; j < 20; j++)//总共打印20次
    {
        for (int i = 0; i < 10; i++) //全部打印一遍,每次打印
        {
            if (letter[i].y >= 0)
            {
                gotoxy(letter[i].x, letter[i].y);//首先跳转到对应的位置
                printf(" ");// 打印该字符
                gotoxy(letter[i].x, letter[i].y + 1);//首先跳转到对应的位置
                printf("%c", letter[i].ch);// 打印该字符
            }
            else
            {
                //打印空白行
                gotoxy(letter[i].x, letter[i].y);//首先跳转到对应的位置
                printf(" ");// 打印该字符
            }
            letter[i].y++;
        }
        Sleep(500);//显示0.5s

    }
}
void start_game(void)
{
   
    printf("倒计时2s开始游戏\n");
    Sleep(2000);
    system("CLS"); //清屏、
    draw_count(now_count, 1, 1);//绘制分数
    last_key = ' ';

    // system("title 688");
    int i = 0;
    int flag = 0;
    int score = 0;
    init_letter(); //初始化 字母
    char        user_l = ' ';

    while (letter[KLETTER_COUNT - 1].y < 24)
    {
        letterMov(); //字母下降一行
        if (_kbhit())  //判断是否按键按下
        {
            user_l = _getch() - 32;;//'A'-'a';
            fflush(stdin); //清除键盘缓冲曲
            for (i = 0; i < KLETTER_COUNT; i++) //遍历数组  寻找按下的按键是否和目前显示的字母有重一样的
            {
                if (user_l == letter[i].ch && letter[i].life == 1 && letter[i].isDisplay == 1)
                {
                    now_count += 1;

                    draw_count(now_count, 1, 1);//绘制分数

                    init_bullet(letter[i].x);//例化子弹 //说明找到了
                    flag = 1;
                    break;
                }
            }
            while (bullet.y > 0 && flag == 1) //说明找到了以后才将子弹发射出去
            {

                bulletMov();
                if (bullet.y == letter[i].y)//当子弹打到字母的时候
                {
                    bullet.life = 0; //销毁子弹
                    letter[i].life = 0; //销毁字母
                    draw_letter(' ', letter[i].x, letter[i].y);
                    flag = 0;
                    break; //跳出循环
                }
                Sleep(50);
            }
        }
        Sleep(300);
    }



    system("CLS"); //清屏、
    //统计分数
    for (i = 0; i < KSPEED; i++)
    {
        if (letter[i].life == 1)        score++;
    }

    printf("一共%d个字母,你削掉了%d个字母,剩余%d个字母.\n", KSPEED, now_count, KSPEED - now_count);
    //打印

    getchar();
    system("pause");
}
void login(void)
{
    char str1[10];
    char str2[10];

    char str123[10];
    char key_s[20];

    strcpy_s(key_s, "title game");
    system(key_s);

    strcpy_s(str123, "123");
    while (true) {
        printf("username:");
        scanf_s("%s", str1, 10);

        printf("passward:");
        scanf_s("%s", str2, 10);
        //printf("%s\n", str);
        if ((strcmp(str1,str123) == 0)&& (strcmp(str2, str123) == 0) ){ return; }
    }

}
int main(void)
{
    char str2[10];
    login();//登录需要输入123
    system("CLS"); //清屏、
    start_game();
    //return 0;
    scanf_s("%s", str2, 10);
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值