鱼C论坛

 找回密码
 立即注册
查看: 527|回复: 8

[已解决]c变量问题求助

[复制链接]
发表于 2023-6-30 11:04:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问下面的代码中有什么问题,level的值应该随循环而增加,但打印调试信息时level值一直为2.
#include "ExBall.h"
#include <stdlib.h>

void initBall(struct ExBall* pBall, int level)
{
    pBall->x = 200+rand() % 401; // 在 0 - 400 范围内生成随机 x 坐标
    pBall->y = rand() % 20 + 300; // 在 300 - 320 范围内生成随机 y 坐标
    pBall->r = 15 - 2.5 * level; // 根据关卡等级调整球的大小
    pBall->dx = 5 + 2 * level; // 根据关卡等级调整球的速度
    pBall->dy = -5 - 2 * level; // 根据关卡等级调整球的速度
    static int levelValue = 1;
    pBall->level = levelValue; // 设置球的关卡等级
    pBall->color = WHITE;
    levelValue = level; // 更新静态变量的值
}

void drawBall(const struct ExBall* pBall)
{
    setfillcolor(pBall->color);
    fillcircle(pBall->x, pBall->y, pBall->r);
}

#include "ExGame.h"
#include "ExTimer.h"
#include <graphics.h>
#include "ExBall.h"

static void keyDown(struct ExGame* exgame, HWND hwnd);
static int hitBricks(struct ExGame* exgame);
static int hitBoard(struct ExGame* exgame);
static void moveBall(struct ExGame* exgame);
static int gameOver(struct ExGame* exgame);
static int gameWin(struct ExGame* exgame, HWND hwnd);
int ExGame::initialLevel = 1;
void gameInit(struct ExGame* exgame)
{
    initMap(&exgame->map);
    initBoard(&exgame->board);
    exgame->score = 0;
    exgame->level = ExGame::initialLevel; // 将初始关卡等级赋值给 level
    initBall(&exgame->ball, exgame->level);
}
int gameLoop(struct ExGame* exgame)
{
    HWND hwnd = initgraph(WIDTH, HEIGHT);
    IMAGE bk;
    loadimage(&bk, "./w.jpg", WIDTH, HEIGHT);
    BeginBatchDraw();

    while (1)
    {
        putimage(0, 0, &bk);
        drawMap(exgame->map);
        drawBoard(exgame->board);
        drawBall(&exgame->ball);
        char scoreStr[20];
        sprintf_s(scoreStr, "Score: %d", exgame->score);
        outtextxy(10, HEIGHT - 30, scoreStr);

        keyDown(exgame, hwnd); // 添加hwnd参数
        moveBall(exgame);

        if (gameOver(exgame))
        {
            int choice = MessageBoxA(hwnd, "你输了 再玩一遍吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION);
            if (choice == IDNO) // Exit
            {
                closegraph();
                return 0;
            }
            else if (choice == IDYES) // Play Again
            {
                gameInit(exgame);
            }
        }
        else if (gameWin(exgame, hwnd))
        {
            char levelStr[20];
            sprintf_s(levelStr, "Level: %d", exgame->level);
            outtextxy(10, HEIGHT - 60, levelStr);
            MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
            exgame->level++; // 进入下一关,更新关卡等级
            printf("Current level: %d\n", exgame->level);
            initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球
            gameInit(exgame);
        }

        FlushBatchDraw();
        Sleep(20);
    }

    EndBatchDraw();
    closegraph();
    return 0;
}

static void keyDown(struct ExGame* exgame, HWND hwnd) // 添加hwnd参数
{
    if ((GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)) && exgame->board.x > 0)
    {
        exgame->board.x -= exgame->board.speed;
    }
    if ((GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)) && exgame->board.x + exgame->board.w < WIDTH)
    {
        exgame->board.x += exgame->board.speed;
    }
    if (GetAsyncKeyState('9')) // 后门,按下数字9进入下一关
    {
        exgame->level++; // 更新关卡等级
        printf("Current level: %d\n", exgame->level);
        initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球
        gameInit(exgame);
    }
}

static int hitBricks(struct ExGame* exgame)
{
    int j = exgame->ball.x / BRICKS_W;
    int i = (exgame->ball.y - exgame->ball.r) / BRICKS_H;
    if (i < ROW && j < COLS && exgame->map.arr[i][j] != 0)
    {
        exgame->map.arr[i][j] = 0;
        exgame->score += 10;
        return 1;
    }
    return 0;
}

static int hitBoard(struct ExGame* exgame)
{
    if (exgame->ball.y + exgame->ball.r >= exgame->board.y && exgame->ball.y - exgame->ball.r <= exgame->board.y + exgame->board.h)
    {
        if (exgame->ball.x + exgame->ball.r >= exgame->board.x && exgame->ball.x - exgame->ball.r <= exgame->board.x + exgame->board.w)
        {
            return 1;
        }
    }
    return 0;
}

static void moveBall(struct ExGame* exgame)
{
    if (exgame->ball.x - exgame->ball.r <= 0 || exgame->ball.x + exgame->ball.r >= WIDTH)
    {
        exgame->ball.dx = -exgame->ball.dx;
    }
    if (exgame->ball.y - exgame->ball.r <= 0 || hitBoard(exgame) || hitBricks(exgame))
    {
        exgame->ball.dy = -exgame->ball.dy;
    }
    exgame->ball.x += exgame->ball.dx;
    exgame->ball.y += exgame->ball.dy;
}

static int gameOver(struct ExGame* exgame)
{
    if (exgame->ball.y + exgame->ball.r >= exgame->board.y + exgame->board.h)
    {
        return 1;
    }
    return 0;
}

static int gameWin(struct ExGame* exgame, HWND hwnd)
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (exgame->map.arr[i][j] != 0)
            {
                return 0;
            }
        }
    }

    char levelStr[20];
    sprintf_s(levelStr, "Level: %d", exgame->level);
    outtextxy(10, HEIGHT - 60, levelStr);
    MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);

    exgame->level++; // 进入下一关,更新关卡等级
    printf("Current level: %d\n", exgame->level);
    initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球的位置、大小和速度
    gameInit(exgame);

    return 1;
}
最佳答案
2023-6-30 14:52:39

根据代码的片段分析,以下是可能存在的问题:

  • 缺少头文件引用:代码中使用了graphics.h等头文件,但没有看到相应的头文件引用语句。请确保在运行代码之前正确引用所需的头文件。
  • main函数缺失:这是一个代码片段,缺少main函数的定义。请确保在该文件或其他文件中包含适当的main函数。
  • 使用了未定义的结构体和函数:在代码中引用了ExGame、ExTimer、ExBall等结构体和相关函数,但是没有提供相应的定义。请确保这些结构体和函数的定义在代码中的其他位置或其他相关文件中。
  • 初始化图形库错误:在gameLoop函数中调用了initgraph函数初始化图形库,但是没有检查初始化是否成功。请确保调用成功后再进行图形绘制操作。
  • 图形绘制顺序有误:在gameLoop函数中,先绘制背景图像,然后在其上绘制地图、挡板和球等元素,最后执行FlushBatchDraw函数刷新绘图缓冲区。正确的绘制顺序应该是先绘制地图、挡板和球等元素,然后再绘制背景图像,最后刷新绘图缓冲区。


请注意,以上只是根据代码片段进行的初步分析,可能存在其他问题。

根据代码片段的问题,以下是修改后的代码建议:

  1. #include "ExGame.h"
  2. #include "ExTimer.h"
  3. #include <graphics.h>
  4. #include "ExBall.h"

  5. static void keyDown(struct ExGame* exgame, HWND hwnd);
  6. static int hitBricks(struct ExGame* exgame);
  7. static int hitBoard(struct ExGame* exgame);
  8. static void moveBall(struct ExGame* exgame);
  9. static int gameOver(struct ExGame* exgame);
  10. static int gameWin(struct ExGame* exgame, HWND hwnd);

  11. void gameInit(struct ExGame* exgame)
  12. {
  13.     initMap(&exgame->map);
  14.     initBoard(&exgame->board);
  15.     exgame->score = 0;
  16.     exgame->level = ExGame::initialLevel;
  17.     initBall(&exgame->ball, exgame->level);
  18. }

  19. int gameLoop(struct ExGame* exgame)
  20. {
  21.     HWND hwnd = initgraph(WIDTH, HEIGHT);
  22.     if (hwnd == NULL) {
  23.         printf("图形库初始化失败\n");
  24.         return -1;
  25.     }
  26.    
  27.     IMAGE bk;
  28.     loadimage(&bk, "./w.jpg", WIDTH, HEIGHT);
  29.     BeginBatchDraw();

  30.     while (1)
  31.     {
  32.         putimage(0, 0, &bk);
  33.         drawMap(exgame->map);
  34.         drawBoard(exgame->board);
  35.         drawBall(&exgame->ball);
  36.         char scoreStr[20];
  37.         sprintf_s(scoreStr, "Score: %d", exgame->score);
  38.         outtextxy(10, HEIGHT - 30, scoreStr);

  39.         keyDown(exgame, hwnd);
  40.         moveBall(exgame);

  41.         if (gameOver(exgame))
  42.         {
  43.             int choice = MessageBoxA(hwnd, "你输了 再玩一遍吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION);
  44.             if (choice == IDNO)
  45.             {
  46.                 closegraph();
  47.                 return 0;
  48.             }
  49.             else if (choice == IDYES)
  50.             {
  51.                 gameInit(exgame);
  52.             }
  53.         }
  54.         else if (gameWin(exgame, hwnd))
  55.         {
  56.             char levelStr[20];
  57.             sprintf_s(levelStr, "Level: %d", exgame->level);
  58.             outtextxy(10, HEIGHT - 60, levelStr);
  59.             MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  60.             exgame->level++;
  61.             printf("Current level: %d\n", exgame->level);
  62.             initBall(&exgame->ball, exgame->level);
  63.             gameInit(exgame);
  64.         }

  65.         FlushBatchDraw();
  66.         Sleep(20);
  67.     }

  68.     EndBatchDraw();
  69.     closegraph();
  70.     return 0;
  71. }

  72. static void keyDown(struct ExGame* exgame, HWND hwnd)
  73. {
  74.     if ((GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)) && exgame->board.x > 0)
  75.     {
  76.         exgame->board.x -= exgame->board.speed;
  77.     }
  78.     if ((GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)) && exgame->board.x + exgame->board.w < WIDTH)
  79.     {
  80.         exgame->board.x += exgame->board.speed;
  81.     }
  82.     if (GetAsyncKeyState('9'))
  83.     {
  84.         exgame->level++;
  85.         printf("Current level: %d\n", exgame->level);
  86.         initBall(&exgame->ball, exgame->level);
  87.         gameInit(exgame);
  88.     }
  89. }

  90. static int hitBricks(struct ExGame* exgame)
  91. {
  92.     int j = exgame->ball.x / BRICKS_W;
  93.     int i = (exgame->ball.y - exgame->ball.r) / BRICKS_H;
  94.     if (i < ROW && j < COLS && exgame->map.arr[i][j] != 0)
  95.     {
  96.         exgame->map.arr[i][j] = 0;
  97.         exgame->score += 10;
  98.         return 1;
  99.     }
  100.     return 0;
  101. }

  102. static int hitBoard(struct ExGame* exgame)
  103. {
  104.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y && exgame->ball.y - exgame->ball.r <= exgame->board.y + exgame->board.h)
  105.     {
  106.         if (exgame->ball.x + exgame->ball.r >= exgame->board.x && exgame->ball.x - exgame->ball.r <= exgame->board.x + exgame->board.w)
  107.         {
  108.             return 1;
  109.         }
  110.     }
  111.     return 0;
  112. }

  113. static void moveBall(struct ExGame* exgame)
  114. {
  115.     if (exgame->ball.x - exgame->ball.r <= 0 || exgame->ball.x + exgame->ball.r >= WIDTH)
  116.     {
  117.         exgame->ball.dx = -exgame->ball.dx;
  118.     }
  119.     if (exgame->ball.y - exgame->ball.r <= 0 || hitBoard(exgame) || hitBricks(exgame))
  120.     {
  121.         exgame->ball.dy = -exgame->ball.dy;
  122.     }
  123.     exgame->ball.x += exgame->ball.dx;
  124.     exgame->ball.y += exgame->ball.dy;
  125. }

  126. static int gameOver(struct ExGame* exgame)
  127. {
  128.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y + exgame->board.h)
  129.     {
  130.         return 1;
  131.     }
  132.     return 0;
  133. }

  134. static int gameWin(struct ExGame* exgame, HWND hwnd)
  135. {
  136.     for (int i = 0; i < ROW; i++)
  137.     {
  138.         for (int j = 0; j < COLS; j++)
  139.         {
  140.             if (exgame->map.arr[i][j] != 0)
  141.             {
  142.                 return 0;
  143.             }
  144.         }
  145.     }
  146.     char levelStr[20];
  147.     sprintf_s(levelStr, "Level: %d", exgame->level);
  148.     outtextxy(10, HEIGHT - 60, levelStr);
  149.     MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  150.     exgame->level++;
  151.     printf("Current level: %d\n", exgame->level);
  152.     gameInit(exgame);

  153.     return 1;
  154. }

  155. void initBall(struct ExBall* pBall, int level)
  156. {
  157.     pBall->x = 200 + rand() % 401;
  158.     pBall->y = rand() % 20 + 300;
  159.     pBall->r = 15 - 2.5 * level;
  160.     pBall->dx = 5 + 2 * level;
  161.     pBall->dy = -5 - 2 * level;
  162.     pBall->level = level;
  163.     pBall->color = WHITE;
  164. }

  165. void drawBall(const struct ExBall* pBall)
  166. {
  167.     setfillcolor(pBall->color);
  168.     fillcircle(pBall->x, pBall->y, pBall->r);
  169. }
复制代码

这是根据代码片段进行的修改建议,但仍然需要确保整个代码的完整性和正确性。如果问题仍然存在,请尽可能提供完整的代码,以便进行更准确的分析和修复。
1d958a38c42bc4033426dfdf51d0b0a.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-30 11:16:25 | 显示全部楼层
我注意到 initBall 函数中关于 levelValue 的使用可能导致了这个问题。

  1. static int levelValue = 1;
  2. pBall->level = levelValue; // 设置球的关卡等级
  3. levelValue = level; // 更新静态变量的值
复制代码


在这段代码中,您首先将 levelValue 的值赋给了 pBall->level ,然后才更新 levelValue 的值。因此,在第一次调用 initBall 时,无论参数 level 的值是多少, pBall->level 始终被设置为1,因为这是 levelValue 的初始值。随后的调用则总是使用上一次调用时的 level 值。

要解决这个问题,你应该首先更新 levelValue 的值,然后再将其赋给 pBall->level 。更改后的代码应如下所示:

  1. static int levelValue = 1;
  2. levelValue = level; // 更新静态变量的值
  3. pBall->level = levelValue; // 设置球的关卡等级
复制代码


然而,这个修正前提是 levelValue 这个静态变量的存在有必要。从代码来看, levelValue 和参数 level 始终保持一致,这使得我不明白为什么要使用这个静态变量。如果 levelValue 并无特殊用途,建议直接使用 level 参数,将 pBall->level 设置为 level :

  1. pBall->level = level; // 设置球的关卡等级
复制代码


此外,您在游戏胜利后调用了 initBall ,然后又调用了 gameInit ,注意到 gameInit 函数中也有调用 initBall ,所以实际上在游戏胜利后您连续两次初始化了球。这可能不是您想要的,您可能需要删除 gameWin 函数中的 initBall 调用,只在 gameInit 中调用它。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-30 11:29:13 | 显示全部楼层
isdkz 发表于 2023-6-30 11:16
我注意到 initBall 函数中关于 levelValue 的使用可能导致了这个问题。

你好,我根据您的建议修改了代码,取消了levelvalue。gameWin 函数中的 initBall 调用已被删除,只在 gameInit 函数中调用它,static int gameWin(struct ExGame* exgame, HWND hwnd)
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (exgame->map.arr[j] != 0)
            {
                return 0;
            }
        }
    }
    char levelStr[20];
    sprintf_s(levelStr, "Level: %d", exgame->level);
    outtextxy(10, HEIGHT - 60, levelStr);
    MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
    exgame->level++; // 进入下一关,更新关卡等级
    printf("Current level: %d\n", exgame->level);
    gameInit(exgame);

    return 1;
}void gameInit(struct ExGame* exgame)
{
    initMap(&exgame->map);
    initBoard(&exgame->board);
    exgame->score = 0;
    exgame->level = ExGame::initialLevel; // 将初始关卡等级赋值给 level
    initBall(&exgame->ball, exgame->level);
}

int gameLoop(struct ExGame* exgame)
{
    HWND hwnd = initgraph(WIDTH, HEIGHT);
    IMAGE bk;
    loadimage(&bk, "./w.jpg", WIDTH, HEIGHT);
    BeginBatchDraw();

    while (1)
    {
        putimage(0, 0, &bk);
        drawMap(exgame->map);
        drawBoard(exgame->board);
        drawBall(&exgame->ball);
        char scoreStr[20];
        sprintf_s(scoreStr, "Score: %d", exgame->score);
        outtextxy(10, HEIGHT - 30, scoreStr);

        keyDown(exgame, hwnd); // 添加hwnd参数
        moveBall(exgame);

        if (gameOver(exgame))
        {
            int choice = MessageBoxA(hwnd, "你输了 再玩一遍吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION);
            if (choice == IDNO) // Exit
            {
                closegraph();
                return 0;
            }
            else if (choice == IDYES) // Play Again
            {
                gameInit(exgame);
            }
        }
        else if (gameWin(exgame, hwnd))
        {
            char levelStr[20];
            sprintf_s(levelStr, "Level: %d", exgame->level);
            outtextxy(10, HEIGHT - 60, levelStr);
            MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
            exgame->level++; // 进入下一关,更新关卡等级
            printf("Current level: %d\n", exgame->level);
            initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球
            gameInit(exgame);
        }

        FlushBatchDraw();
        Sleep(20);
    }

    EndBatchDraw();
    closegraph();
    return 0;
}但是level的打印值还是恒为2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-30 11:30:18 | 显示全部楼层
1493916656 发表于 2023-6-30 11:29
你好,我根据您的建议修改了代码,取消了levelvalue。gameWin 函数中的 initBall 调用已被删除,只在 gam ...

放在代码标签里
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-30 11:40:34 | 显示全部楼层

抱歉 能给出更详细的指导吗?我对这个不是很熟悉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-30 11:41:35 | 显示全部楼层
1493916656 发表于 2023-6-30 11:40
抱歉 能给出更详细的指导吗?我对这个不是很熟悉

https://fishc.com.cn/thread-128631-1-1.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-30 14:37:33 | 显示全部楼层
歌者文明清理员 发表于 2023-6-30 11:41
https://fishc.com.cn/thread-128631-1-1.html
  1. #include "ExGame.h"
  2. #include "ExTimer.h"
  3. #include <graphics.h>
  4. #include "ExBall.h"

  5. static void keyDown(struct ExGame* exgame, HWND hwnd);
  6. static int hitBricks(struct ExGame* exgame);
  7. static int hitBoard(struct ExGame* exgame);
  8. static void moveBall(struct ExGame* exgame);
  9. static int gameOver(struct ExGame* exgame);
  10. static int gameWin(struct ExGame* exgame, HWND hwnd);
  11. int ExGame::initialLevel = 1;
  12. void gameInit(struct ExGame* exgame)
  13. {
  14.     initMap(&exgame->map);
  15.     initBoard(&exgame->board);
  16.     exgame->score = 0;
  17.     exgame->level = ExGame::initialLevel; // 将初始关卡等级赋值给 level
  18.     initBall(&exgame->ball, exgame->level);
  19. }

  20. int gameLoop(struct ExGame* exgame)
  21. {
  22.     HWND hwnd = initgraph(WIDTH, HEIGHT);
  23.     IMAGE bk;
  24.     loadimage(&bk, "./w.jpg", WIDTH, HEIGHT);
  25.     BeginBatchDraw();

  26.     while (1)
  27.     {
  28.         putimage(0, 0, &bk);
  29.         drawMap(exgame->map);
  30.         drawBoard(exgame->board);
  31.         drawBall(&exgame->ball);
  32.         char scoreStr[20];
  33.         sprintf_s(scoreStr, "Score: %d", exgame->score);
  34.         outtextxy(10, HEIGHT - 30, scoreStr);

  35.         keyDown(exgame, hwnd); // 添加hwnd参数
  36.         moveBall(exgame);

  37.         if (gameOver(exgame))
  38.         {
  39.             int choice = MessageBoxA(hwnd, "你输了 再玩一遍吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION);
  40.             if (choice == IDNO) // Exit
  41.             {
  42.                 closegraph();
  43.                 return 0;
  44.             }
  45.             else if (choice == IDYES) // Play Again
  46.             {
  47.                 gameInit(exgame);
  48.             }
  49.         }
  50.         else if (gameWin(exgame, hwnd))
  51.         {
  52.             char levelStr[20];
  53.             sprintf_s(levelStr, "Level: %d", exgame->level);
  54.             outtextxy(10, HEIGHT - 60, levelStr);
  55.             MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  56.             exgame->level++; // 进入下一关,更新关卡等级
  57.             printf("Current level: %d\n", exgame->level);
  58.             initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球
  59.             gameInit(exgame);
  60.         }

  61.         FlushBatchDraw();
  62.         Sleep(20);
  63.     }

  64.     EndBatchDraw();
  65.     closegraph();
  66.     return 0;
  67. }

  68. static void keyDown(struct ExGame* exgame, HWND hwnd) // 添加hwnd参数
  69. {
  70.     if ((GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)) && exgame->board.x > 0)
  71.     {
  72.         exgame->board.x -= exgame->board.speed;
  73.     }
  74.     if ((GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)) && exgame->board.x + exgame->board.w < WIDTH)
  75.     {
  76.         exgame->board.x += exgame->board.speed;
  77.     }
  78.     if (GetAsyncKeyState('9')) // 后门,按下数字9进入下一关
  79.     {
  80.         exgame->level++; // 更新关卡等级
  81.         printf("Current level: %d\n", exgame->level);
  82.         initBall(&exgame->ball, exgame->level); // 根据新关卡等级重新初始化球
  83.         gameInit(exgame);
  84.     }
  85. }

  86. static int hitBricks(struct ExGame* exgame)
  87. {
  88.     int j = exgame->ball.x / BRICKS_W;
  89.     int i = (exgame->ball.y - exgame->ball.r) / BRICKS_H;
  90.     if (i < ROW && j < COLS && exgame->map.arr[i][j] != 0)
  91.     {
  92.         exgame->map.arr[i][j] = 0;
  93.         exgame->score += 10;
  94.         return 1;
  95.     }
  96.     return 0;
  97. }

  98. static int hitBoard(struct ExGame* exgame)
  99. {
  100.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y && exgame->ball.y - exgame->ball.r <= exgame->board.y + exgame->board.h)
  101.     {
  102.         if (exgame->ball.x + exgame->ball.r >= exgame->board.x && exgame->ball.x - exgame->ball.r <= exgame->board.x + exgame->board.w)
  103.         {
  104.             return 1;
  105.         }
  106.     }
  107.     return 0;
  108. }

  109. static void moveBall(struct ExGame* exgame)
  110. {
  111.     if (exgame->ball.x - exgame->ball.r <= 0 || exgame->ball.x + exgame->ball.r >= WIDTH)
  112.     {
  113.         exgame->ball.dx = -exgame->ball.dx;
  114.     }
  115.     if (exgame->ball.y - exgame->ball.r <= 0 || hitBoard(exgame) || hitBricks(exgame))
  116.     {
  117.         exgame->ball.dy = -exgame->ball.dy;
  118.     }
  119.     exgame->ball.x += exgame->ball.dx;
  120.     exgame->ball.y += exgame->ball.dy;
  121. }

  122. static int gameOver(struct ExGame* exgame)
  123. {
  124.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y + exgame->board.h)
  125.     {
  126.         return 1;
  127.     }
  128.     return 0;
  129. }

  130. static int gameWin(struct ExGame* exgame, HWND hwnd)
  131. {
  132.     for (int i = 0; i < ROW; i++)
  133.     {
  134.         for (int j = 0; j < COLS; j++)
  135.         {
  136.             if (exgame->map.arr[i][j] != 0)
  137.             {
  138.                 return 0;
  139.             }
  140.         }
  141.     }
  142.     char levelStr[20];
  143.     sprintf_s(levelStr, "Level: %d", exgame->level);
  144.     outtextxy(10, HEIGHT - 60, levelStr);
  145.     MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  146.     exgame->level++; // 进入下一关,更新关卡等级
  147.     printf("Current level: %d\n", exgame->level);
  148.     gameInit(exgame);

  149.     return 1;
  150. }

  151. #include "ExBall.h"
  152. #include <stdlib.h>

  153. void initBall(struct ExBall* pBall, int level)
  154. {
  155.     pBall->x = 200 + rand() % 401; // 在 0 - 400 范围内生成随机 x 坐标
  156.     pBall->y = rand() % 20 + 300; // 在 300 - 320 范围内生成随机 y 坐标
  157.     pBall->r = 15 - 2.5 * level; // 根据关卡等级调整球的大小
  158.     pBall->dx = 5 + 2 * level; // 根据关卡等级调整球的速度
  159.     pBall->dy = -5 - 2 * level; // 根据关卡等级调整球的速度
  160.     pBall->level = level; // 设置球的关卡等级
  161.     pBall->color = WHITE;
  162. }

  163. void drawBall(const struct ExBall* pBall)
  164. {
  165.     setfillcolor(pBall->color);
  166.     fillcircle(pBall->x, pBall->y, pBall->r);
  167. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-30 14:52:39 | 显示全部楼层    本楼为最佳答案   

根据代码的片段分析,以下是可能存在的问题:

  • 缺少头文件引用:代码中使用了graphics.h等头文件,但没有看到相应的头文件引用语句。请确保在运行代码之前正确引用所需的头文件。
  • main函数缺失:这是一个代码片段,缺少main函数的定义。请确保在该文件或其他文件中包含适当的main函数。
  • 使用了未定义的结构体和函数:在代码中引用了ExGame、ExTimer、ExBall等结构体和相关函数,但是没有提供相应的定义。请确保这些结构体和函数的定义在代码中的其他位置或其他相关文件中。
  • 初始化图形库错误:在gameLoop函数中调用了initgraph函数初始化图形库,但是没有检查初始化是否成功。请确保调用成功后再进行图形绘制操作。
  • 图形绘制顺序有误:在gameLoop函数中,先绘制背景图像,然后在其上绘制地图、挡板和球等元素,最后执行FlushBatchDraw函数刷新绘图缓冲区。正确的绘制顺序应该是先绘制地图、挡板和球等元素,然后再绘制背景图像,最后刷新绘图缓冲区。


请注意,以上只是根据代码片段进行的初步分析,可能存在其他问题。

根据代码片段的问题,以下是修改后的代码建议:

  1. #include "ExGame.h"
  2. #include "ExTimer.h"
  3. #include <graphics.h>
  4. #include "ExBall.h"

  5. static void keyDown(struct ExGame* exgame, HWND hwnd);
  6. static int hitBricks(struct ExGame* exgame);
  7. static int hitBoard(struct ExGame* exgame);
  8. static void moveBall(struct ExGame* exgame);
  9. static int gameOver(struct ExGame* exgame);
  10. static int gameWin(struct ExGame* exgame, HWND hwnd);

  11. void gameInit(struct ExGame* exgame)
  12. {
  13.     initMap(&exgame->map);
  14.     initBoard(&exgame->board);
  15.     exgame->score = 0;
  16.     exgame->level = ExGame::initialLevel;
  17.     initBall(&exgame->ball, exgame->level);
  18. }

  19. int gameLoop(struct ExGame* exgame)
  20. {
  21.     HWND hwnd = initgraph(WIDTH, HEIGHT);
  22.     if (hwnd == NULL) {
  23.         printf("图形库初始化失败\n");
  24.         return -1;
  25.     }
  26.    
  27.     IMAGE bk;
  28.     loadimage(&bk, "./w.jpg", WIDTH, HEIGHT);
  29.     BeginBatchDraw();

  30.     while (1)
  31.     {
  32.         putimage(0, 0, &bk);
  33.         drawMap(exgame->map);
  34.         drawBoard(exgame->board);
  35.         drawBall(&exgame->ball);
  36.         char scoreStr[20];
  37.         sprintf_s(scoreStr, "Score: %d", exgame->score);
  38.         outtextxy(10, HEIGHT - 30, scoreStr);

  39.         keyDown(exgame, hwnd);
  40.         moveBall(exgame);

  41.         if (gameOver(exgame))
  42.         {
  43.             int choice = MessageBoxA(hwnd, "你输了 再玩一遍吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION);
  44.             if (choice == IDNO)
  45.             {
  46.                 closegraph();
  47.                 return 0;
  48.             }
  49.             else if (choice == IDYES)
  50.             {
  51.                 gameInit(exgame);
  52.             }
  53.         }
  54.         else if (gameWin(exgame, hwnd))
  55.         {
  56.             char levelStr[20];
  57.             sprintf_s(levelStr, "Level: %d", exgame->level);
  58.             outtextxy(10, HEIGHT - 60, levelStr);
  59.             MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  60.             exgame->level++;
  61.             printf("Current level: %d\n", exgame->level);
  62.             initBall(&exgame->ball, exgame->level);
  63.             gameInit(exgame);
  64.         }

  65.         FlushBatchDraw();
  66.         Sleep(20);
  67.     }

  68.     EndBatchDraw();
  69.     closegraph();
  70.     return 0;
  71. }

  72. static void keyDown(struct ExGame* exgame, HWND hwnd)
  73. {
  74.     if ((GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)) && exgame->board.x > 0)
  75.     {
  76.         exgame->board.x -= exgame->board.speed;
  77.     }
  78.     if ((GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT)) && exgame->board.x + exgame->board.w < WIDTH)
  79.     {
  80.         exgame->board.x += exgame->board.speed;
  81.     }
  82.     if (GetAsyncKeyState('9'))
  83.     {
  84.         exgame->level++;
  85.         printf("Current level: %d\n", exgame->level);
  86.         initBall(&exgame->ball, exgame->level);
  87.         gameInit(exgame);
  88.     }
  89. }

  90. static int hitBricks(struct ExGame* exgame)
  91. {
  92.     int j = exgame->ball.x / BRICKS_W;
  93.     int i = (exgame->ball.y - exgame->ball.r) / BRICKS_H;
  94.     if (i < ROW && j < COLS && exgame->map.arr[i][j] != 0)
  95.     {
  96.         exgame->map.arr[i][j] = 0;
  97.         exgame->score += 10;
  98.         return 1;
  99.     }
  100.     return 0;
  101. }

  102. static int hitBoard(struct ExGame* exgame)
  103. {
  104.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y && exgame->ball.y - exgame->ball.r <= exgame->board.y + exgame->board.h)
  105.     {
  106.         if (exgame->ball.x + exgame->ball.r >= exgame->board.x && exgame->ball.x - exgame->ball.r <= exgame->board.x + exgame->board.w)
  107.         {
  108.             return 1;
  109.         }
  110.     }
  111.     return 0;
  112. }

  113. static void moveBall(struct ExGame* exgame)
  114. {
  115.     if (exgame->ball.x - exgame->ball.r <= 0 || exgame->ball.x + exgame->ball.r >= WIDTH)
  116.     {
  117.         exgame->ball.dx = -exgame->ball.dx;
  118.     }
  119.     if (exgame->ball.y - exgame->ball.r <= 0 || hitBoard(exgame) || hitBricks(exgame))
  120.     {
  121.         exgame->ball.dy = -exgame->ball.dy;
  122.     }
  123.     exgame->ball.x += exgame->ball.dx;
  124.     exgame->ball.y += exgame->ball.dy;
  125. }

  126. static int gameOver(struct ExGame* exgame)
  127. {
  128.     if (exgame->ball.y + exgame->ball.r >= exgame->board.y + exgame->board.h)
  129.     {
  130.         return 1;
  131.     }
  132.     return 0;
  133. }

  134. static int gameWin(struct ExGame* exgame, HWND hwnd)
  135. {
  136.     for (int i = 0; i < ROW; i++)
  137.     {
  138.         for (int j = 0; j < COLS; j++)
  139.         {
  140.             if (exgame->map.arr[i][j] != 0)
  141.             {
  142.                 return 0;
  143.             }
  144.         }
  145.     }
  146.     char levelStr[20];
  147.     sprintf_s(levelStr, "Level: %d", exgame->level);
  148.     outtextxy(10, HEIGHT - 60, levelStr);
  149.     MessageBox(hwnd, "你赢了", "游戏结束", MB_OK);
  150.     exgame->level++;
  151.     printf("Current level: %d\n", exgame->level);
  152.     gameInit(exgame);

  153.     return 1;
  154. }

  155. void initBall(struct ExBall* pBall, int level)
  156. {
  157.     pBall->x = 200 + rand() % 401;
  158.     pBall->y = rand() % 20 + 300;
  159.     pBall->r = 15 - 2.5 * level;
  160.     pBall->dx = 5 + 2 * level;
  161.     pBall->dy = -5 - 2 * level;
  162.     pBall->level = level;
  163.     pBall->color = WHITE;
  164. }

  165. void drawBall(const struct ExBall* pBall)
  166. {
  167.     setfillcolor(pBall->color);
  168.     fillcircle(pBall->x, pBall->y, pBall->r);
  169. }
复制代码

这是根据代码片段进行的修改建议,但仍然需要确保整个代码的完整性和正确性。如果问题仍然存在,请尽可能提供完整的代码,以便进行更准确的分析和修复。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-30 15:58:57 | 显示全部楼层
小号。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 16:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表