admin管理员组文章数量:1568309
2023年12月12日发(作者:)
C++打字游戏
很早之前C++写打字游戏:
运行效果:
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#pragma comment(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 3 alias bkmusic", NULL, 0, NULL);
mciSendString("play bkmusic repeat", NULL, 0, NULL);
srand((unsigned int)time(NULL));
initgraph(WIDTH+50,HEIGHT);
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') {
[i++] = target;
}else {
int flagERror = 0;
//
输入的与随机的一样消失
for (i = 0; i < 3; i++) {
if (strcmp(, words[i].str) == 0) {
initTarget(words,i);
right++;
flagERror = 1;
}
}
if (flagERror == 0) {
error++;
}
i = 0;
userkey.x = 320;
memset(,0,20);
}
}
outtextxy(userkey.x,userkey.y,);
drawScore();
FlushBatchDraw();
Sleep(100);
}
getchar();
closegraph();
return 0;
}
这个程序是要自己提前下载好图形库easyx库的,然后里面有一些资源音乐之类的,可以自己加,这个可以当个大一下的课程设计,哈哈哈
版权声明:本文标题:C++打字游戏 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1702313555a500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论