0
Follow
0
View

c++ typing game display time and accuracy.

cys23_2012 注册会员
2023-02-27 09:08

This answer quotes ChatGPT

Reply me if you have any questions
You can add timer and correct rate to Draw() function. Also, be careful to stop the game after 60 seconds.

Here is the modified code:


#include
#include 
#include 
#include 
#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;
clock_t start_time = clock(); // 记录开始时间
// 初始化字母
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';
    }
}
 
// 绘制屏幕
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);
    }
    // 绘制得分
    char text[20];
    sprintf_s(text, "Score: %d", score);
    settextstyle(50, 20, "Arial");
    outtextxy(20, 20, text);
    
    // 绘制时间
    clock_t current_time = clock(); // 获取当前时间
    int elapsed_time = (int)(current_time - start_time) / CLOCKS_PER_SEC;
    if (elapsed_time > 60) {
        // 时间超过60秒时停止游戏
        outtextxy(500, 300, "Time: 60");
        outtextxy(100, 300, "Correct Rate: 100%");
        return;
    }
    char time1[20];
    sprintf_s(time1, "Time: %d", elapsed_time);
    settextstyle(50, 20, "HandelGothic BT");
    outtextxy(500,300,time1);
    
    // 绘制正确率
    settextstyle(50, 20, "HandelGothic BT");
    char correct2[20];
    int correct_count = score / n;
    int correct_rate = correct_count * 100;
    sprintf_s(correct2, "Correct Rate: %d%%", correct_rate);
    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';
        }
    }
}



doubleniu 注册会员
2023-02-27 09:08

Reference to GPT and their own ideas, the following is modified code, to achieve the timing time and typing correct rate statistics:

#include
#include 
#include 
#include 
#include 
#include 
#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 total = 0;
time_t start_time;
 
// 初始化字母
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';
    }
}
 
// 绘制屏幕
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);
    }
 
    // 绘制得分
    char score_text[20];
    sprintf_s(score_text, "Score: %d", score);
    settextstyle(50, 20, "Arial");
    outtextxy(20, 20, score_text);
    
    // 绘制时间
    time_t end_time = time(NULL);
    double elapsed_time = difftime(end_time, start_time);
    if (elapsed_time > 60)
    {
        elapsed_time = 60;
    }
    char time_text[20];
    sprintf_s(time_text, "Time: %.0f", elapsed_time);
    settextstyle(50, 20, "Arial");
    outtextxy(500, 20, time_text);
 
    // 绘制正确率
    double accuracy = total == 0 ? 0 : (double)score / total * 100;
    char accuracy_text[20];
    sprintf_s(accuracy_text, "Accuracy: %.0f%%", accuracy);
    settextstyle(50, 20, "Arial");
    outtextxy(20, 300, accuracy_text);
}
 
// 移动字母
void MoveLetter()
{
    for (int i = 0; i < MAXLETTER; i++)
    {
        letter[i].y += SPEED;
        if (letter[i].y >= HEIGHT)
        {
            total++;
            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 = toupper(input);
        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();
clock_t start = clock(); // 记录游戏开始时间
BeginBatchDraw();
while (true)
{
    Draw();
    MoveLetter();
    GetKey();
    FlushBatchDraw();
    Sleep(10);
    clock_t end = clock(); // 记录当前时间
    double timeElapsed = (double)(end - start) / CLOCKS_PER_SEC;
    if (timeElapsed >= 60.0) // 如果游戏时间超过60秒,跳出循环
        break;
}
EndBatchDraw();
closegraph();

// 计算正确率
int correctCount = score;
int totalCount = n - 1;
double accuracy = (double)correctCount / totalCount * 100;

// 输出游戏结果
printf("游戏结束!\n");
printf("得分: %d\n", score);
printf("时间: %.2lf秒\n", timeElapsed);
printf("正确率: %.2lf%%\n", accuracy);

return 0;
}


About the Author

Question Info

Publish Time
2023-02-27 09:08
Update Time
2023-02-27 09:08

Related Question

如何正确处理一个衍生的child_process的stdio ?

Tomcat的普通加载器不能引用WEB-INF/lib中的jar吗?

Troubleshooting startup script on GCP using key:value & directing to bucket storage file

访问ip,只有apache页面的,是web没开还是必须要域名访问?

spring-JDBC-Templent 处理1对多结果集

这个数学问题用c语言怎么写

程序设计将两个两位数的正整数a,b合并形成一个整数放在c中

Oracle合并未按预期运行

MSYS2: UCRT 64和x64的区别是什么?

c++,这几个空填什么呀