An Option?

Just playing.
Does this seem a viable option for testing the movement bounds ??

I’d like to split some of this functionality out of main() , but I haven’t learnt how yet in C++

#include "raylib.h"

int main()
{
  // window dimensions : braced initialization
  int width{ 350 };
  int height{ 200 };
  int textHeight{ 30 };

  InitWindow(width, height, "Phreds's Play Window");
  int circle_x{ 175 };
  int circle_y{ 100 };
  float circle_radius{ 25 };

  int move_distance{ 5 };
  int min_x{ (int)circle_radius };
  int max_x{ width - (int)circle_radius };
  int min_y{ (int)circle_radius };
  int max_y{ height - (int)circle_radius };

  SetTargetFPS(60);
  while (!WindowShouldClose())
  {
    BeginDrawing();
    ClearBackground(GRAY);

    DrawCircle(circle_x, circle_y, circle_radius, BLUE);

    // multiple keys accepted each frame for MOVEMENT.
    if (IsKeyDown(KEY_D) || (IsKeyDown(KEY_RIGHT)))
    {
      circle_x += move_distance;
    }
    if (IsKeyDown(KEY_A) || (IsKeyDown(KEY_LEFT)))
    {
      circle_x -= move_distance;
    }
    if (IsKeyDown(KEY_W) || (IsKeyDown(KEY_UP)))
    {
      circle_y -= move_distance;
    }
    if (IsKeyDown(KEY_S) || (IsKeyDown(KEY_DOWN)))
    {
      circle_y += move_distance;
    }

    circle_x = circle_x < min_x   ? min_x
               : circle_x > max_x ? max_x
                                  : circle_x;
    circle_y = circle_y < min_y   ? min_y
               : circle_y > max_y ? max_y
                                  : circle_y;

    DrawText("Waiting . . . !", 40, 125, textHeight, RED);

    EndDrawing();
  }

  return 0;
}

I just play around with your question.

You can split moving ifology to function for example:
void MovingLogic(int& circle_x, int& circle_y, int move_distance)

& - make that you can change value for e.g. circle_x inside function and changed value is visible outside the function.

In c# is like ref int circle_x ← you’re passing reference of value.

but to make code is working you file stucture should be:

void MoveLogic()
{
}
int main()
{
}

Jarek has it right, you can add additional functions and use them inside of the main function. Just make sure the function is declared before using it.

Privacy & Terms