Programmed a circle to react to keypresses

#include <cstdio>
#include <string>

#include "raylib.h"

int main() {
    int height{1600};
    int width{1600};

    InitWindow(height, width, "Circle");

    int circle_x = height / 2;
    int circle_y = width / 2;
    int circle_radius = 50;
    int circle_min_radius = circle_radius;
    int circle_max_radius = height < width ? height / 2 : width / 2;
    int circle_radius_step = 50;
    Color circle_color = {0, 255, 0, 255};
    std::string prompt = "Key Pressed: ";
    std::string key = "None";

    SetTargetFPS(30);
    while (!WindowShouldClose()) {
        BeginDrawing();
            ClearBackground(RAYWHITE);
            key = "None";

            if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) {
                circle_x += circle_radius;
                key = "Right";
            }

            if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) {
                circle_x -= circle_radius;
                key = "Left";
            }

            if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) {
                circle_y -= circle_radius;
                key = "Up";
            }

            if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) {
                circle_y += circle_radius;
                key = "Down";
            }

            if (circle_x + circle_radius > height) {
                circle_x = height - circle_radius;
            }

            if (circle_y + circle_radius > width) {
                circle_y = width - circle_radius;
            }

            if (circle_x - circle_radius < 0) {
                circle_x = circle_radius;
            }

            if (circle_y - circle_radius < 0) {
                circle_y = circle_radius;
            }

            if (IsKeyDown(KEY_PAGE_UP)) {
                circle_radius += circle_radius_step;
                key = "PgUp";
            }

            if (IsKeyDown(KEY_PAGE_DOWN)) {
                circle_radius -= circle_radius_step;
                key = "PgDown";
            }

            if (circle_radius > circle_max_radius) {
                circle_radius = circle_max_radius;
            }

            if (circle_radius <= circle_min_radius) {
                circle_radius = circle_min_radius;
            }

            if (IsKeyDown(KEY_SPACE)) {
                circle_color.r = (circle_color.r + 10) % 255;
                circle_color.g = (circle_color.g + 20) % 255;
                circle_color.b = (circle_color.b + 30) % 255;
                key = "Space";
            }

            DrawCircle(circle_x, circle_y, circle_radius, circle_color);

            std::string debug_statement = prompt + key;
            DrawText(debug_statement.c_str(), height / 10, width / 10, 48, RED);
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

circle

1 Like

Made a simple game with this mechanic

circle_game

2 Likes

I like it. Reminds me of my first Pong game.

2 Likes

Impressive, unfortunately I was born a 15 years ago so I don’t really have the type of memories of pong like @VVruba does, however I have played some newer versions, though there really isn’t much resemblance to the classics, great work making a game with only code(no game engine), nicely done!

1 Like

I meant the first simple game I made a few years back when I was doing Java… I am getting old but I ain’t that kind of fossil yet.
giphy

1 Like

Oh, I’m stupid, if that’s the case then I to have built the game, my bad if I offended you.

Privacy & Terms