C语言打字游戏(无错误警告版)
1.设计过程
之前在设计打字游戏项目设计之前曾在CSDN和百度上找了许多,但是最后运行都有问题,最后通过小组分配函数功能设计了一个简单的打字游戏程序
2.核心算法
①读取字符函数getch()
②退出游戏函数fun_Esc()
③实现游戏开始函数fun-Play()
④程序暂停函数delay()
⑤检查键盘输入函数kbhit()
⑥随机产生数字函数srand()
⑦随机产生数函数rand()
具体不多做赘述,源代码里面都有对应的注释。
3.运行过程
4.话不多说上代码
#include<stdio.h> //标明头文件
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>void delay(int sec)
{time_t start_time, cur_time;//声明变量time(&start_time);do {time(&cur_time);} while ((cur_time - start_time) < sec);
}void gotoxy(int x, int y)
{COORD c;c.X = x - 1;c.Y = y - 1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}const char* kw[] = { "Q W E R T Y U I O P [ ]","A S D F G H J K L ; '","Z X C V B N M , . / " };
long AllCounter = 0, RightCounter = 0, WrongCounter = 0;void main()
{int i = 0, j = 0; //声明函数 int fun_Esc(); //声明退出游戏函数int getch(); //读取字符函数int fun_Play(); //声明调用游戏函数void delay(); //运行的程序暂停函数int kbhit(); //检查是否有键盘输入函数void srand(); //随机产生数字函数int rand(); //随机产生数函数system("cls"); //清屏功能gotoxy(18, 1); //定位光标printf("%s\n", kw[0]); //输出字母 gotoxy(20, 3); //输出游戏窗口下界printf("%s\n", kw[1]);gotoxy(22, 5);printf("%s\n", kw[2]);gotoxy(11, 25);for (i = 0; i < 60; i++){printf("=");} //输出游戏窗口下界gotoxy(1, 1);printf("AllCh: %ld\nRight: %ld\nWrong: %ld", AllCounter, RightCounter, WrongCounter); //输出游戏数据gotoxy(50, 1);printf("Press Esc to exit"); //输出游戏提示信息gotoxy(50, 2);printf("Enter to pause");gotoxy(26, 12);printf("* * * * * * * * * * * * * * ");gotoxy(26, 13);printf("* Press any key to start! *");gotoxy(26, 14);printf("* * * * * * * * * * * * * * ");gotoxy(51, 13);if (getch() == 27) //ESC对应的ASCII码是27{if (fun_Esc() == 1) //推出调用判断函数{system("cls"); //清屏exit(0); //退出程序}}gotoxy(23, 12);printf(" ");gotoxy(23, 13);printf(" ");gotoxy(23, 14);printf(" ");while (1){fun_Play();}
} //主函数截止 //开始设计游戏界面
int fun_Play() //调用游戏函数
{int x, y, i, j; //定义整形变量 unsigned int Timer; //无符号变量char ch; //字符型变量char cur;int fun_Esc();time_t t; //time_t实际上是long型,从一个时间点(一般是1970年1月1日0时0分0秒)到当前的秒数srand((unsigned)time(&t)); //随机生成一个正数gotoxy(26, 12);//gotoxy函数用于屏幕输出,功能是将光标移动到屏幕指定位置。在屏幕左上角被定义为光标的原点(0,0) printf(" ");gotoxy(26, 13);printf(" ");gotoxy(26, 14);printf(" ");y = 6; //对y进行赋值Timer = 100; //对Timer进行赋值i = rand() % 3; //i等于一个从0到2的随机数j = rand() % (9 - i); //j等于一个从0到(9-i)的随机数ch = kw[i][j * 4]; //定义一个二维字符串数组x = 18 + i * 2 + j * 4;while (y <= 24) //while循环里面有两个大if语句{if (kbhit()) //函数名:kbhit()功能及返回值,检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。(第一个大if语句){cur = getch();if (cur == ch || cur == ch + 32)//巧妙运用if语句 {ch = '*'; Timer = 100;}else if (cur == 27){if (fun_Esc() == 1){system("cls");exit(0);}}else if (cur == '\r'){gotoxy(x, y - 1);printf(" ");gotoxy(26, 12);printf("* * * * * * * * * * * * * * *");gotoxy(26, 13);printf("* Press any key to continue *");gotoxy(26, 14);printf("* * * * * * * * * * * * * * * ");getch();gotoxy(28, 13);printf(" ");}else{WrongCounter++;}}if (y > 6) //第二个大if语句{gotoxy(x, y - 1);printf(" ");}gotoxy(x, y);printf("%c", ch);gotoxy(1, 1);printf("AllCh: %ld\nRight: %ld\nWrong: %ld", AllCounter, RightCounter, WrongCounter);Sleep(Timer);y++; //while循环语句结束}AllCounter++;if (ch == '*'){RightCounter++;}return 0;
}int fun_Esc() //退出游戏函数
{int key = '#';gotoxy(26, 12); //游戏光标(下同)printf("* * * * * * * * * * * * * * *");gotoxy(26, 13);printf(" Are you sure to exit? (Y/N) "); //输出 “是否退出游戏”gotoxy(26, 14);printf("* * * * * * * * * * * * * * *");gotoxy(51, 13);while (key != 'Y' && key != 'y' && key != 'N' && key != 'n') //确定是否退出游戏{key = getch(); //输入一个字母直接读取if (key == 'Y' || key == 'y'){return 1;}if (key == 'N' || key == 'n'){gotoxy(24, 12);printf(" ");gotoxy(24, 13);printf(" ");gotoxy(24, 14);printf(" ");return 0;}}return 0;
}
5.创作不易,点个赞,加个关注呗吧
其中用到了图形库graphics.h 在网络课程中学到的. #include #include #include #include #include IMAGE backimg; IMAGE bami ... 打字游戏程序设计汇报 第二章 打字游戏程序设计内容要求和设计思想 2.1 设计内容要求 要求设计的程序需包括:在游戏开始后,按回车键进入打字游戏:按ESC键返回主界面:按空格暂停:按大写字母E程序结束 ... 打字游戏程序设计报告 第二章 打字游戏程序设计内容要求和设计思想 2.1 设计内容要求 要求设计的程序需包括:在游戏开始后,按回车键进入打字游戏:按ESC键返回主界面:按空格暂停:按大写字母E程序结束 ... 前言 经过学习数组.函数.循环语句.选择语句等C语言的一些基础知识后,我想借助编写扫雷小游戏来对所学知识进行一个巩固.游戏只会实现一些基本的功能,展开.标记雷.取消雷等不实现(还不会). 1.游戏编写 ... #include #include #include #include #include "ConsoleOut.h" #pragma comment(lib,"Cons ... #include #include#include#include#include#include using namespacestd;#define KLETTER_COUNT 10 #defin ... 今天练习了一个C语言小程序,控制台版的简易"金山打字"小游戏. 效果图: 代码: #include #include #include #include void printfSp ... #include <stdio.h> #include<string.h> #include <unistd.h> //头文件 /*char mygetch() / ... C++学习之-C语言实现打字游戏 作者介绍 学习的主要内容 作者介绍 张伟伟,男,西安工程大学电子信息学院,2019级硕士研究生,张宏伟人工智能课题组. 研究方向:机器视觉与人工智能. 电子邮件:24 ...C语言打字游戏(无错误警告版)相关推荐
最新文章
热门文章