栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++测手速小游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++测手速小游戏

        终于有空写点代码了(作业好多awa)

        今天带来的是一款非常有趣的测手速游戏,玩家可以测试自己的反应速度。

创作背景:

        MCYH的反应速度及其迟钝。

        为了锻炼自己,MCYH制作了一款测手速的小游戏,以达到提高反应速度的目的。

游戏玩法:

        cmd框一开始为绿色,4-9秒钟后会变成红色,这时玩家需要按下空格键,游戏会记录你的反应时间。反应时间反映着你的反应速度。

       

低配版代码:

#include
#include
#include
using namespace std;
#define INF 0x3f3f
int main()
{
	system("cls");
	srand(time(0));
	system("color 2F");
	printf("when it turns red, press 'space'nnn");
	Sleep(rand()%5000+4000);
	system("color 4F");
	int j=clock(),k;
	char s=_getch();
	printf("%d ms",clock()-j);
	Sleep(1000);
	return 0;
}

_____________________________________________________________________________

        emmmmm...是不是感觉少点什么?

        对啦!我们可以多测几次,取平均值,这样也可以避免偶然性,减少误差。

      

中配版代码:

#include
#include
#include
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int c_a=INF;
int main()
{
	system("cls");
	for(int i=1;i<=5;i++)
	  {
	  	system("cls");
		srand(time(0));
		system("color 2F");
		printf("when it turns red, press 'space'nnn");
		Sleep(rand()%5000+4000);
		bool cont=0;
		while(kbhit())//防止玩家“抢跑 ” 
		  {
		  	printf("You pressed too soon.");
		    char s=getch();
		    Sleep(1000);
		    cont=1;
		  }
		if (cont==0)
		{
			system("color 4F");
			int j=clock(),k;
			char s=_getch();
			printf("%d ms",clock()-j);
			a[i]=clock()-j;
			res+=a[i];
			Sleep(1000);
		}
		else
		  --i;//本次作废,重新来过 
	  }
	system("cls");
	res/=5;
	printf("Your average:%dmsn",res);
	for(int i=1;i<=5;i++)
	  {
	    printf("%d : %d msn",i,a[i]);
	    c_a=min(a[i],c_a);
	  }
	printf("You can press any key to quit the Game after 2 seconds.n");
	Sleep(2000);
	_getch();
	return 0;
}

_____________________________________________________________________________

        emmmmm...要是能存档就更好了。

高配版代码:

#include
#include
#include
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int best_res=INF,best_a=INF;
int c_a=INF;
void Read_File();//读档 
void Write_File();//存档 
int main()
{
	Read_File();
	for(int i=1;i<=5;i++)
	  {
	  	system("cls");
		srand(time(0));
		system("color 2F");
		printf("when it turns red, press 'space'nnn");
		Sleep(rand()%5000+4000);
		bool cont=0;
		while(kbhit())
		  {
		  	printf("You pressed too soon.");
		    char s=getch();
		    Sleep(1000);
		    cont=1;
		  }
		if (cont==0)
		{
			system("color 4F");
			int j=clock(),k;
			char s=_getch();
			printf("%d ms",clock()-j);
			a[i]=clock()-j;
			res+=a[i];
			Sleep(1000);
		}
		else
		  --i;
	  }
	system("cls");
	res/=5;
	printf("Your average:%dmsn",res);
	for(int i=1;i<=5;i++)
	  {
	    printf("%d : %d msn",i,a[i]);
	    c_a=min(a[i],c_a);
	  }
	printf("Your best average: %dmsn",best_res);
	printf("Your best score: %dmsn",best_a);
	if (res 

_____________________________________________________________________________

        最后再完善一下场景,把光标屏蔽掉,大功告成!

上成品:

       

#include
#include
#include
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int best_res=INF,best_a=INF;
int c_a=INF;

void Read_File();//读档 
void Write_File();//存档 
void hidden();//隐藏光标 
int main()
{
	Read_File();
	printf("Measuring hand speed Gamen");
	printf("Author : MCYH n");
	printf("Press any key to join the game.nn");
	printf("Game Rule:n");
	printf("The screen will be green first. When it turns red, you should press 'space'.n");
	printf("DO NOT press ahead!!!n");
	_getch();
	system("cls");
	for(int i=1;i<=5;i++)
	  {
	  	system("cls");
		srand(time(0));
		system("color 2F");
		printf("when it turns red, press 'space'nnn");
		Sleep(rand()%5000+4000);
		bool cont=0;
		while(kbhit())
		  {
		  	printf("You pressed too soon.");
		    char s=getch();
		    Sleep(1000);
		    cont=1;
		  }
		if (cont==0)
		{
			system("color 4F");
			int j=clock(),k;
			char s=_getch();
			printf("%d ms",clock()-j);
			a[i]=clock()-j;
			res+=a[i];
			Sleep(1000);
		}
		else
		  --i;
	  }
	system("cls");
	res/=5;
	printf("Your average:%dmsn",res);
	for(int i=1;i<=5;i++)
	  {
	    printf("%d : %d msn",i,a[i]);
	    c_a=min(a[i],c_a);
	  }
	printf("Your best average: %dmsn",best_res);
	printf("Your best score: %dmsn",best_a);
	if (res 

        好啦,以上就是这篇文章的全部内容,你的手速有多快呢?可以把你的最好成绩打在评论区哟。觉得好玩的可以点一个赞,感谢您的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033098.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号