Build and run the included Raylib example game - a complete tutorial
The Raylib Container includes a complete example game in user_code/my_game.c. This walkthrough explains the code, demonstrates how to compile and run it, and shows how to customize it for your own projects.
Here’s the full my_game.c file included in the container:
/********************************************************************************************* raylib [core] example - Basic window** Example complexity rating: [★☆☆☆] 1/4** Welcome to raylib!** To test examples, just press F6 and execute 'raylib_compile_execute' script* Note that compiled executable is placed in the same folder as .c file** To test the examples on Web, press F6 and execute 'raylib_compile_execute_web' script* Web version of the program is generated in the same folder as .c file** You can find all basic examples on C:\raylib\raylib\examples folder or* raylib official webpage: www.raylib.com** Enjoy using raylib. :)** Example originally created with raylib 1.0, last time updated with raylib 1.0** Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,* BSD-like license that allows static linking with closed source software** Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)*********************************************************************************************/#include "raylib.h"//------------------------------------------------------------------------------------// Program main entry point//------------------------------------------------------------------------------------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 { // Update //---------------------------------------------------------------------------------- // TODO: Update your variables here //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0;}
This line includes the Raylib library header, giving you access to all Raylib functions, types, and constants. Every Raylib program must include this header.
while (!WindowShouldClose()){ // Update // TODO: Update your variables here // Draw BeginDrawing(); ClearBackground(RAYWHITE); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); EndDrawing();}
This is the heart of every Raylib game. The loop continues until the user closes the window or presses ESC.
Game Loop API Calls
WindowShouldClose()
Returns true if the user pressed ESC or clicked the window close button
If compilation succeeds, you’ll return to the shell prompt with no errors.
3
Run the compiled executable
Execute your game:
./my_game
A window should appear displaying the congratulatory message!
4
Test the game
Try the following:
Move the window around your screen
Press ESC to close the game
Click the window’s close button (X)
The game should exit cleanly and return you to the terminal.
If the window doesn’t appear, verify your graphics setup is working by running xeyes. If xeyes doesn’t work, check that you ran xhost +local:docker on your host system.
Draw multiple text elements at different positions:
BeginDrawing(); ClearBackground(RAYWHITE); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); DrawText("Press ESC to exit", 290, 250, 20, GRAY); DrawText("Made with Raylib", 310, 400, 16, DARKGRAY);EndDrawing();
int textX = 0; // Add this before the game loopwhile (!WindowShouldClose()){ // Update textX += 2; // Move 2 pixels right each frame if (textX > 800) textX = -400; // Wrap around // Draw BeginDrawing(); ClearBackground(RAYWHITE); DrawText("Moving text!", textX, 200, 20, LIGHTGRAY); EndDrawing();}
BeginDrawing(); ClearBackground(RAYWHITE); // Draw a circle DrawCircle(400, 225, 50, RED); // Draw a rectangle DrawRectangle(300, 150, 200, 100, BLUE); // Draw a line DrawLine(0, 0, 800, 450, GREEN); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);EndDrawing();
After making any changes to the source code, remember to recompile with the GCC command before running again. Your old executable won’t reflect the changes until you rebuild.
while (!WindowShouldClose()){ // Update: Modify game state, handle input, update physics // (Empty in this example) // Draw: Render everything to the screen BeginDrawing(); // All drawing code here EndDrawing();}
This separation keeps your code organized and ensures rendering happens at the right time.
Now that you understand the basic structure, here are next steps for creating your own games:
Add Input Handling
Check for key presses with IsKeyPressed(KEY_SPACE) to add player controls
Load Textures
Use LoadTexture("image.png") and DrawTexture() to add graphics
Play Sounds
Initialize audio with InitAudioDevice() and play sounds with PlaySound()
Detect Collisions
Use CheckCollisionRecs() and CheckCollisionCircles() for game physics
When loading external assets (textures, sounds, fonts), make sure the file paths are correct relative to where you run the executable. Use the assets/ subdirectory for organization.
The Raylib community is very active. If you get stuck, check the official examples, join the Discord server, or browse the GitHub discussions for help.