c++打字游戏在图形界面中如何输出计时时间与打字的正确率?自己做了一个打字游戏但无法实现时间与正确率的正确数字,因此想要来询问。以下为要求:
1.游戏开始时,开始计时,
2.在时间超过60秒后停止。
3.统计用户的正确率,给出该局游戏的得分。
以下是源代码,正确率与时间希望写在draw函数上,谢谢!
```c++
#include<iostream>
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#define WIDTH 600
#define HEIGHT 400
#define MAXLETTER 10
#define SPEED 2
struct Letter
{
int x;
int y;
char ch;
};
IMAGE MM;
Letter letter[MAXLETTER];
int score = 0;int n = 1;
// 初始化字母
void SetLetter()
{
for (int i = 0; i < MAXLETTER; i++)
{
letter[i].x = rand() % (WIDTH - 50) + 25;
letter[i].y = rand() % HEIGHT - HEIGHT;
letter[i].ch = rand() % 26 + 'A';
n++;
}
}
// 绘制屏幕
void Draw()
{
putimage(0, 0, &MM);
for (int i = 0; i < MAXLETTER; i++)
{
settextstyle(50, 20, "HandelGothic BT");
setbkmode(TRANSPARENT);
outtextxy(letter[i].x, letter[i].y, letter[i].ch);
}
//书写内容:1.正确个数
char text[20];
sprintf_s(text, "Score: %d", score);
settextstyle(50, 20, "Arial");
outtextxy(20, 20, text);
//2,时间
clock_t start, end;//定义clock_t变量
start = clock(); //
end = clock(); //结束时间
settextstyle(50, 20, "HandelGothic BT");
int m = int(end - start) / CLOCKS_PER_SEC;
char time1[20];
sprintf_s(time1, "time:", m);
outtextxy(500,300,time1);
//3,正确率:
settextstyle(50, 20, "HandelGothic BT");
char correct2[20];
sprintf_s(correct2, "正确率: %d", score / n);
outtextxy(100,300,correct2);
}
// 移动字母
void MoveLetter()
{
for (int i = 0; i < MAXLETTER; i++)
{
letter[i].y += SPEED;
if (letter[i].y >= HEIGHT)
{
score--;
letter[i].x = rand() % (WIDTH - 50) + 25;
letter[i].y = rand() % HEIGHT - HEIGHT;
letter[i].ch = rand() % 26 + 'A';
}
}
}
// 用户输入
void GetKey()
{
if (_kbhit())
{
char input = _getch();
input = input - 'a' + 'A';
for (int i = 0; i < MAXLETTER; i++)
{
if (input == letter[i].ch)
{
score++;
letter[i].x = rand() % (WIDTH - 50) + 25;
letter[i].y = rand() % HEIGHT - HEIGHT;
letter[i].ch = rand() % 26 + 'A';
}
}
}
}
int main()
{
initgraph(WIDTH, HEIGHT);
srand(time(NULL));
loadimage(&MM, "mm.jpg", WIDTH, HEIGHT);
SetLetter();
BeginBatchDraw();
while (true)
{
Draw();
MoveLetter();
GetKey();
FlushBatchDraw();
Sleep(10);
}
EndBatchDraw();
closegraph();
return 0;
}
```