Write, Run & Share raylib code online using OneCompiler's raylib online compiler for free. It's one of the robust, feature-rich online compilers for raylib, running on raylib 5.5 with a C toolchain. The game window your code creates is streamed live into the browser — no compiler install, no linker flags, no SDL/GLFW to set up. The editor shows sample boilerplate code when you choose language as raylib and start coding.
raylib is a simple, easy-to-use C library for game programming, started in 2013 by Ramon Santamaria. The whole API is a flat list of around 500 functions — InitWindow, BeginDrawing, DrawCircle, IsKeyDown — with no classes, no inheritance, no event callbacks. That's the point: raylib aims to be the friendliest possible way to write a game in C, the way QBasic and the early game-programming books were friendly. It's used everywhere from university courses to indie game jams to embedded demo scenes, and it works on Windows, macOS, Linux, Android, iOS, Raspberry Pi, and the web.
Every raylib program follows the same shape: open a window, loop until the user closes it, and pair every BeginDrawing() with an EndDrawing().
#include "raylib.h"
int main(void) {
InitWindow(640, 360, "My Game");
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Hello, raylib!", 20, 20, 24, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
raylib gives you primitives for rectangles, circles, lines, triangles, polygons, and pixels. Colours are Color structs — RED, BLUE, RAYWHITE, etc. are predefined, or build your own with (Color){ r, g, b, a }.
#include "raylib.h"
int main(void) {
InitWindow(400, 300, "Shapes");
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangle(40, 40, 120, 80, BLUE);
DrawCircle(280, 80, 40, RED);
DrawLine(40, 180, 360, 180, BLACK);
DrawTriangle((Vector2){200, 220}, (Vector2){160, 280}, (Vector2){240, 280}, GREEN);
EndDrawing();
}
CloseWindow();
return 0;
}
Update positions each frame, draw, then call EndDrawing(). Use GetFrameTime() (delta seconds) so motion is consistent regardless of frame rate.
#include "raylib.h"
int main(void) {
const int W = 640, H = 360;
InitWindow(W, H, "Bouncing ball");
SetTargetFPS(60);
Vector2 pos = { W / 2.0f, H / 2.0f };
Vector2 vel = { 220.0f, 170.0f };
const float r = 24.0f;
while (!WindowShouldClose()) {
float dt = GetFrameTime();
pos.x += vel.x * dt;
pos.y += vel.y * dt;
if (pos.x - r < 0 || pos.x + r > W) vel.x = -vel.x;
if (pos.y - r < 0 || pos.y + r > H) vel.y = -vel.y;
BeginDrawing();
ClearBackground((Color){20, 24, 40, 255});
DrawCircleV(pos, r, (Color){233, 30, 99, 255});
DrawText("Hello, raylib!", 20, 20, 24, RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}
IsKeyDown is polled every frame for held keys — great for movement. IsKeyPressed fires once per press — use it for one-shot actions like jumping or firing.
#include "raylib.h"
int main(void) {
InitWindow(600, 400, "Keyboard");
SetTargetFPS(60);
Rectangle player = { 280, 180, 40, 40 };
float speed = 240.0f;
while (!WindowShouldClose()) {
float dt = GetFrameTime();
if (IsKeyDown(KEY_LEFT)) player.x -= speed * dt;
if (IsKeyDown(KEY_RIGHT)) player.x += speed * dt;
if (IsKeyDown(KEY_UP)) player.y -= speed * dt;
if (IsKeyDown(KEY_DOWN)) player.y += speed * dt;
if (IsKeyPressed(KEY_SPACE)) TraceLog(LOG_INFO, "Jump!");
BeginDrawing();
ClearBackground(BLACK);
DrawRectangleRec(player, GREEN);
EndDrawing();
}
CloseWindow();
return 0;
}
GetMousePosition() gives you a Vector2; IsMouseButtonPressed / Down / Released cover the buttons.
#include "raylib.h"
int main(void) {
InitWindow(600, 400, "Mouse");
SetTargetFPS(60);
Vector2 dots[256];
int count = 0;
while (!WindowShouldClose()) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && count < 256) {
dots[count++] = GetMousePosition();
}
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < count; i++) DrawCircleV(dots[i], 8, RED);
DrawText("Click to drop dots", 20, 20, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
DrawText uses the default raylib font. For custom fonts, load a .ttf with LoadFont and pass it to DrawTextEx.
#include "raylib.h"
int main(void) {
InitWindow(500, 200, "Text");
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Default font", 20, 40, 30, DARKGRAY);
DrawText("Centered subtitle", 20, 90, 16, GRAY);
const char *title = "BIG";
int w = MeasureText(title, 60);
DrawText(title, (500 - w) / 2, 130, 60, MAROON);
EndDrawing();
}
CloseWindow();
return 0;
}
raylib ships with helpers for axis-aligned rectangles, circles, and points — CheckCollisionRecs, CheckCollisionCircles, CheckCollisionPointRec. Good enough for most 2D games.
#include "raylib.h"
int main(void) {
InitWindow(600, 400, "Collisions");
SetTargetFPS(60);
Rectangle player = { 100, 180, 40, 40 };
Rectangle wall = { 300, 160, 80, 80 };
while (!WindowShouldClose()) {
float dt = GetFrameTime();
if (IsKeyDown(KEY_LEFT)) player.x -= 180 * dt;
if (IsKeyDown(KEY_RIGHT)) player.x += 180 * dt;
if (IsKeyDown(KEY_UP)) player.y -= 180 * dt;
if (IsKeyDown(KEY_DOWN)) player.y += 180 * dt;
bool hit = CheckCollisionRecs(player, wall);
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangleRec(wall, hit ? RED : DARKGRAY);
DrawRectangleRec(player, hit ? GOLD : GREEN);
DrawText(hit ? "Collision!" : "Move with arrows", 20, 20, 20, BLACK);
EndDrawing();
}
CloseWindow();
return 0;
}
Most of what's interesting about raylib is in this kind of small, complete example — a working game in under 50 lines.
#include "raylib.h"
int main(void) {
const int W = 600, H = 360;
InitWindow(W, H, "Pong");
SetTargetFPS(60);
Rectangle paddle = { 20, H / 2 - 40, 12, 80 };
Vector2 ball = { W / 2, H / 2 };
Vector2 bv = { 260, 190 };
while (!WindowShouldClose()) {
float dt = GetFrameTime();
if (IsKeyDown(KEY_UP)) paddle.y -= 380 * dt;
if (IsKeyDown(KEY_DOWN)) paddle.y += 380 * dt;
ball.x += bv.x * dt;
ball.y += bv.y * dt;
if (ball.y < 8 || ball.y > H - 8) bv.y = -bv.y;
if (ball.x > W - 8) bv.x = -bv.x;
if (CheckCollisionCircleRec(ball, 8, paddle)) bv.x = -bv.x;
if (ball.x < 0) { ball.x = W / 2; ball.y = H / 2; }
BeginDrawing();
ClearBackground(BLACK);
DrawRectangleRec(paddle, RAYWHITE);
DrawCircleV(ball, 8, RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}