C++打字游戏

很早之前C++写打字游戏:
运行效果:
在这里插入图片描述
代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")

const int WIDTH = 640;
const int HEIGHT = 500;
//文字
struct TARGET {
	int x;
	int y;
	char *str;
};
//接受用户的值
struct USERKEY {
	int x;
	int y;
	char str[20];
}userkey = {320,470,""};
//数据
int right = 0;
int error = 0;
//指定位置输出整数
void outtextxy_int(int x,int y,char *format,int num) {
	char str[20] = "";
	sprintf(str,format,num);
	outtextxy(x,y,str);
}


//指定位置输出浮点数
void outtextxy_double(int x, int y, char *format, double num) {
	char str[20] = "";
	sprintf(str, format, num);
	outtextxy(x, y, str);
}
//布局
void divWindow() {
	line(WIDTH-100,0,WIDTH-100,HEIGHT-40);
	line(0, HEIGHT - 40,WIDTH+50,HEIGHT-40);
	line(WIDTH-100,130,WIDTH+50,130);
}
//初始化字符串
void initTarget(struct TARGET words[],int n) {
	static char str[22][10] = {"main","include","void","true","int","char","float","switch",
	"case","if","while","unsigned","bool","for","continue","struct","enum","long",
	"return","signed","else","break"};
	//随机
	words[n].str = str[rand() % 22];
	//0 1 2 循环队列不重复
	while (words[n].str == words[(n + 1) % 3].str ||
		words[n].str == words[(n + 2) % 3].str) {
		words[n].str = str[rand() % 22];
	}
	words[n].x = rand() % (WIDTH - 200);
	words[n].y = -20;
}
//分数
void drawScore() {
	settextcolor(LIGHTBLUE);
	settextstyle(25,0,"字魂24号-镇魂手书");
	//软件输出
	outtextxy(WIDTH-90,25,"打字游戏");
	//outtextxy(WIDTH - 90, 25+25, "程序员专属");
	//游戏状态
	outtextxy(WIDTH - 90,225,"正确数");
	outtextxy_int(WIDTH-90,225+25,"%d",right);

	outtextxy(WIDTH - 90,285, "错误数");
	outtextxy_int(WIDTH - 90, 285+ 25, "%d", error);

	outtextxy(WIDTH - 90,285+285-225, "正确率");
	if (right + error == 0) {
		outtextxy_double(WIDTH - 90, 285 +285+ 25 - 225, "%.2lf%%",0.00);
	}else {
		//c会取整
		double sum = right + error;
		outtextxy_double(WIDTH - 90, 285 +285+ 25 - 225, "%.2lf%%", right / sum * 100);
	}
	
}
int main() {

	mciSendString("open 1.mp3 alias bkmusic", NULL, 0, NULL);
	mciSendString("play bkmusic repeat", NULL, 0, NULL);

	srand((unsigned int)time(NULL));
	initgraph(WIDTH+50,HEIGHT);
	struct TARGET words[3];
	//产生掉落的字符串
	for (int n = 0; n < 3; n++) {
		initTarget(words,n);
		words[n].y = -15 - n * 30;
	}
	BeginBatchDraw();
	int i = 0;
	while (1) {
		cleardevice();
		divWindow();
		//碰线处理
		for (int n = 0; n < 3; n++) {
			words[n].y += 2;
			if (words[n].y >(HEIGHT - 40 - textheight(words[n].str))) {
				initTarget(words, n);
			}
		}
		for (int n = 0; n < 3; n++) {
			settextcolor(RED);
			outtextxy(words[n].x,words[n].y,words[n].str);
		}
		//用户处理 有按键返回非0
		if (_kbhit()) {
			//字符串变字符
			char target;
			if ((target = _getch()) != '\r') {
				userkey.str[i++] = target;
				
			}else {

				int flagERror = 0;
				//输入的与随机的一样消失
				for (i = 0; i < 3; i++) {
					if (strcmp(userkey.str, words[i].str) == 0) {
						initTarget(words,i);
						right++;
						flagERror = 1;
					}
				}
				if (flagERror == 0) {
					error++;
				}
				i = 0;
				userkey.x = 320;
				memset(userkey.str,0,20);
			}
		}
		outtextxy(userkey.x,userkey.y,userkey.str);
		drawScore();
		FlushBatchDraw();
		Sleep(100);
	}
	getchar();
	closegraph();
	return 0;
}

这个程序是要自己提前下载好图形库easyx库的,然后里面有一些资源音乐之类的,可以自己加,这个可以当个大一下的课程设计,哈哈哈

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值