关于#C语言#的问题,如何解决?

C语言盒子接球小游戏解释代码

问题相关代码,请勿粘贴截图

int main()
{
// 初始化
initgraph(640, 480);// 这个函数用于初始化绘图窗口,初始化画布,
srand(time(NULL));//srand函数是随机数发生器的初始化函数,它需要提供一个种子,这个种子会对应一个随机数,如果使用相同的种子后面的rand()函数会出现一样的随机数。
BeginBatchDraw();//这个函数用于开始批量绘图。执行后,任何绘图操作都将暂时不输出到绘图窗口上,直到执行 FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出
setlinecolor(GREEN); //这个函数用于设置当前设备画线颜色。
setfillcolor(WHITE);//setfillcolor 这个函数用于设置当前设备填充颜色。

menu();

Ball ball[NUM];
int dx, i, c, score = 0;
bool flag = true;

for (i = 0; i < NUM; i++)
{
    ballRandom(ball, i);
}

int t = GetTickCount();
char strScore[10], str[] = "你的分数:";

// 游戏主循环
while (flag)
{
    dx = 0;
    // 显示得分
    char strScore[10];
    itoa(score, strScore, 10);//itoa 取整数输入值,并将其转换为相应进制数字的字符串。itoa(i ,num ,10 );
    outtextxy(570, 210, strScore);//打印得分

    // 画球,并计算得分
    calculateScore(ball, score);

    // 画盒子
    fillrectangle(box_x, box_y, box_x + 80, box_y + 60);//这个函数用于画有边框的填充矩形。
    FlushBatchDraw();

    // 获取用户控制命令
    c = GetCommand();
    if (c & CMD_LEFT)    dx = -10;
    if (c & CMD_RIGHT)    dx = 10;
    if (c & CMD_QUIT)    flag = false;
    if (!Time(t)) flag = false;

    // 延时
    Sleep(20);

    // 擦除游戏区
    clearrectangle(0, 0, 448, 480);//这个函数用于清空矩形区域。

    // 计算球的新坐标
    for (i = 0; i < NUM; i++)
    {
        ball[i].y += ball[i].v;
    }

    // 移动盒子
    box_x += dx;
    if (box_x < 0)   box_x = 0;
    if (box_x > 368) box_x = 368;
}

// 清空键盘缓冲区
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));

// 输出游戏结果
itoa(score, strScore, 10);//itoa 取整数输入值,并将其转换为相应进制数字的字符串。
outtextxy(222, 240, strcat(str, strScore)); //这个函数用于在指定位置输出字符串。
outtextxy(220, 300, "按任意键退出");//这个函数用于在指定位置输出字符串。
EndBatchDraw();//这个函数用于结束批量绘制,并执行未完成的绘制任务

// 按任意键退出
getch();
closegraph();//这个函数用于关闭绘图窗口。

return 0;

}

上次也是你问的,你干脆把整个代码发给我,我给你解释了吧



```c
#include <graphics.h>//EasyX图形库
#include <conio.h>//一个控制台输入输出库函数
#include <time.h>//C语言时间控制库函数
#include <stdio.h>//C语言标准库函数

// 定义常量
#define NUM 10
#define    CMD_LEFT        1
#define    CMD_RIGHT        2
#define    CMD_QUIT        4

int box_x = 10;
int box_y = 420;

// 定义球的结构体
struct Ball
{
    int x, y, v;
};

// 获取用户控制
int GetCommand()
{
    int c = 0;
    if (GetAsyncKeyState(VK_LEFT) & 0x8000)        c |= CMD_LEFT;//检测左按键
    if (GetAsyncKeyState(VK_RIGHT) & 0x8000)    c |= CMD_RIGHT;//检测右按键
    if (GetAsyncKeyState(VK_ESCAPE) & 0x8000)    c |= CMD_QUIT;//检测退出

    return c;
}

// 倒计时
int Time(int t)
{
    char strsec[10];
    int sec = 20 - (GetTickCount() - t) / 1000;
    itoa(sec, strsec, 10);
    outtextxy(570, 110, "      ");
    outtextxy(570, 110, strcat(strsec, "s"));
    return sec;
}

// 介绍
void menu()
{
    line(449, 0, 449, 480);
    char runTime[] = "游戏倒计时     : ",
        receiveBallNum[] = "接到的球的数量:", copyRight[] = "制作:倪俨凡,杨政旭",
        finishWorkDate[] = "完成日期:2022年6月24日",
        introductiona[] = "按方向键控制盒子移动接住", introductionb[] = "小球,倒计时为0时游戏结束";

    settextcolor(GREEN);
    outtextxy(450, 10, introductiona);
    outtextxy(450, 30, introductionb);
    outtextxy(450, 110, runTime);
    outtextxy(450, 210, receiveBallNum);
    outtextxy(450, 310, copyRight);
    outtextxy(450, 410, finishWorkDate);
}

// 产生随机球
void ballRandom(Ball ball[], int i)
{
    ball[i].x = 16 + 45 * i;//X轴(横向)球出现的位置
    ball[i].y = 8 + rand() % 32;//Y轴(纵向)球出现的位置
    ball[i].v = 1 + rand() % 5;//球的速度
}

// 画球,并计算得分
void calculateScore(Ball ball[], int& score)
{
    for (int i = 0; i < NUM; i++)   // i 从0到NUM-1循环
    {
        fillcircle(ball[i].x, ball[i].y, 8);    //画一个以(ball[i].x, ball[i].y)为圆心,8位半径的圆
        if (ball[i].y >= 472)   //如果y的坐标大于472,
        {
            ballRandom(ball, i);    //随机更改球的位置
            continue;   //不进行后面的语句,进入下一次循环
        }
        if (box_x + 8 <= ball[i].x && ball[i].x <= box_x + 72 && ball[i].y >= 412)//如果球的横坐标x符合  box_x+8 <= ball[i].x <= box_x+72且纵坐标ball[i].y>=412
        {
            score++;    //得分+1
            ballRandom(ball, i);//随机更改球的位置
        }
    }
}

// 主函数
int main()
{
    // 初始化
    initgraph(640, 480);// 这个函数用于初始化绘图窗口,初始化画布,
    srand(time(NULL));//srand函数是随机数发生器的初始化函数,它需要提供一个种子,这个种子会对应一个随机数,如果使用相同的种子后面的rand()函数会出现一样的随机数。
    BeginBatchDraw();//这个函数用于开始批量绘图。执行后,任何绘图操作都将暂时不输出到绘图窗口上,直到执行 FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出
    setlinecolor(GREEN); //这个函数用于设置当前设备画线颜色。
    setfillcolor(WHITE);//setfillcolor 这个函数用于设置当前设备填充颜色。

    menu();

    Ball ball[NUM];
    int dx, i, c, score = 0;
    bool flag = true;

    for (i = 0; i < NUM; i++)
    {
        ballRandom(ball, i);
    }

    int t = GetTickCount();
    char strScore[10], str[] = "你的分数:";

    // 游戏主循环
    while (flag)
    {
        dx = 0;
        // 显示得分
        char strScore[10];
        itoa(score, strScore, 10);//itoa 取整数输入值,并将其转换为相应进制数字的字符串。itoa(i ,num ,10 );
        outtextxy(570, 210, strScore);//打印得分

        // 画球,并计算得分
        calculateScore(ball, score);

        // 画盒子
        fillrectangle(box_x, box_y, box_x + 80, box_y + 60);//这个函数用于画有边框的填充矩形。
        FlushBatchDraw();

        // 获取用户控制命令
        c = GetCommand();
        if (c & CMD_LEFT)    dx = -10;
        if (c & CMD_RIGHT)    dx = 10;
        if (c & CMD_QUIT)    flag = false;
        if (!Time(t)) flag = false;

        // 延时
        Sleep(20);

        // 擦除游戏区
        clearrectangle(0, 0, 448, 480);//这个函数用于清空矩形区域。

        // 计算球的新坐标
        for (i = 0; i < NUM; i++)
        {
            ball[i].y += ball[i].v;
        }

        // 移动盒子
        box_x += dx;
        if (box_x < 0)   box_x = 0;
        if (box_x > 368) box_x = 368;
    }

    // 清空键盘缓冲区
    FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
    
    // 输出游戏结果
    itoa(score, strScore, 10);//itoa 取整数输入值,并将其转换为相应进制数字的字符串。
    outtextxy(222, 240, strcat(str, strScore)); //这个函数用于在指定位置输出字符串。
    outtextxy(220, 300, "按任意键退出");//这个函数用于在指定位置输出字符串。
    EndBatchDraw();//这个函数用于结束批量绘制,并执行未完成的绘制任务

    // 按任意键退出
    getch();
    closegraph();//这个函数用于关闭绘图窗口。

    return 0;
}

```