【问题标题】:How to make a simple menu with C++ and Raylib?如何使用 C++ 和 Raylib 制作简单的菜单?
【发布时间】:2021-06-08 12:21:29
【问题描述】:

我是一名新手程序员,我正在尝试在 C++ 中使用 Raylib 库。

但我无法使用简单的开始菜单。我一直在尝试调用 void 函数,使用 switch 和简单的 if 语句......我如何使用 switch 或 if 语句在 raylib 中创建一个简单的菜单而不关闭并打开程序的新窗口?我猜在 While 循环中的某个地方?



#include "raylib.h"




int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {

       BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawText("Congrats! You created your firstwindow!", 190, 200, 50, LIGHTGRAY);
        EndDrawing();
        
        if(IsKeyPressed(KEY_Q)) DrawText("New thing here", 200, 210, 60, GREEN);
        if(IsKeyPressed(KEY_W)) DrawText("New thing here number two", 200, 210, 60, BLACK);

        

           
            
        }
        //----------------------------------------------------------------------------------
   
    CloseWindow();   
    return 0;


   }

我一直在尝试使用 Break、Pause 和 Goto 的东西,如何在不关闭窗口的情况下结束 While 循环,我需要更改 while 循环的语句吗?

【问题讨论】:

    标签: c++ menu raylib


    【解决方案1】:

    我不确定我是否理解您的问题...您想在不关闭窗口的情况下退出主游戏循环吗?这实际上是不可能的,因为你在那里渲染了所有的窗口内容。对我来说,您似乎对语言本身缺乏一些基本的了解。 例如,您的 if 不会按照您想要的方式工作,因为它只会闪烁文本一秒钟而不是一直显示它。

    布尔显示文本 = 假; 尽管(...) { if(IsKeyPressed(KEY_Q)) showText = !showText; // 如果你再次按 Q 文本将 消失 if(showText) DrawText(...); }

    可能不是最干净的方式,但它确实有效。希望我的回答对你有所帮助。 如果您不确定可以查看Raylib Examples

    【讨论】:

      【解决方案2】:

      如果我对问题的理解正确,您希望在游戏启动前有一个菜单。您可以在同一个窗口中实现这一点,而不是尝试启动一个新窗口。

      现在,确切的解决方案将根据您制作的游戏类型而有所不同,但我能想到的最简单的解决方案是使用 if 语句来检查我们是否在 while 循环的菜单中。这可能看起来像这样。

          bool isInMenu = true
          // Main game loop
          while (!WindowShouldClose())    // Detect window close button or ESC key
          {
              // Update here
              if(isInMenu)
              {
                  if(IsKeyPressed(KEY_Q)) isInMenu = false;
              }
              else
              {
                  if(IsKeyPressed(KEY_W)) isInMenu = true;
              }
      
              // Draw here
              BeginDrawing();
              if(isInMenu)
              {
                  ClearBackground(RAYWHITE);
                  DrawText("This is the menu", 190, 200, 50, LIGHTGRAY);
              }
              else
              {
                  ClearBackground(RAYWHITE);
                  DrawText("Congrats! You created your firstwindow!", 190, 200, 50, LIGHTGRAY);
              }
              
              EndDrawing();
      
          }
          //------------------------------------------------------------------------------
         
      

      【讨论】:

        猜你喜欢
        • 2021-09-12
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        • 1970-01-01
        • 2021-01-23
        • 1970-01-01
        • 2021-11-07
        • 1970-01-01
        相关资源
        最近更新 更多