-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Working with Raylib in WebAssembly
sourovislam33 edited this page Aug 4, 2025
·
1 revision
#include "raylib.h" #include <stdio.h>
#define MAX_OBJECTS 5 #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 500
//Game Object typedef struct GameObject { Rectangle rect; Vector2 position; float width; bool passed; } GameObject;
int main() { // Window Setup InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Flappy Bird No Graphics or Sound"); SetTargetFPS(60);
int score = 0;
int highScore = 0;
bool gameOver = false;
float birdX = 130;
float birdY = 8;
float birdVelocity = 0;
const float gravity = 0.4f;
const float jumpStrength = -5.8f;
const float birdSize = 32;
// Initialize object positions
GameObject objects[MAX_OBJECTS];
int objectSpacing = 300;
int startX = 800;
float objectSpeed = 3.5f;
for (int i = 0; i < MAX_OBJECTS; i++) {
objects[i].position = (Vector2){ startX + i * objectSpacing, GetRandomValue(200, SCREEN_HEIGHT - 50) };
objects[i].rect = (Rectangle){ objects[i].position.x, objects[i].position.y, 40, 40 };
objects[i].width = 40;
objects[i].passed = false;
}
// Colors
Color LightBlue = {208, 226, 237, 255};
Color Green = {50, 123, 66, 255};
// Game Loop
while (!WindowShouldClose()) {
// Game logic
if (!gameOver) {
if (IsKeyPressed(KEY_SPACE)) {
birdVelocity = jumpStrength;
}
birdVelocity += gravity;
birdY += birdVelocity;
for (int i = 0; i < MAX_OBJECTS; i++) {
objects[i].position.x -= objectSpeed;
objects[i].rect.x = objects[i].position.x;
if (objects[i].position.x + objects[i].width < 0) {
float farthestX = 0;
for (int j = 0; j < MAX_OBJECTS; j++) {
if (j != i && objects[j].position.x > farthestX) {
farthestX = objects[j].position.x;
}
}
objects[i].position.x = farthestX + objectSpacing;
objects[i].position.y = GetRandomValue(200, SCREEN_HEIGHT - 50);
objects[i].rect.x = objects[i].position.x;
objects[i].rect.y = objects[i].position.y;
objects[i].passed = false;
}
if (!objects[i].passed && birdX > objects[i].position.x + objects[i].width) {
score++;
objects[i].passed = true;
}
Rectangle birdRect = { birdX, birdY, birdSize, birdSize };
if (CheckCollisionRecs(birdRect, objects[i].rect)) {
gameOver = true;
}
}
if (birdY < 0 || birdY + birdSize > SCREEN_HEIGHT - 13) {
gameOver = true;
}
} else {
if (IsKeyPressed(KEY_ENTER)) {
// Restart
birdY = 8;
birdVelocity = 0;
score = 0;
gameOver = false;
for (int i = 0; i < MAX_OBJECTS; i++) {
objects[i].position = (Vector2){ startX + i * objectSpacing, GetRandomValue(200, SCREEN_HEIGHT - 50) };
objects[i].rect = (Rectangle){ objects[i].position.x, objects[i].position.y, 40, 40 };
objects[i].passed = false;
}
}
}
if (score > highScore) highScore = score;
// Drawing
BeginDrawing();
ClearBackground(LightBlue);
// Ground
DrawRectangle(0, SCREEN_HEIGHT - 11, SCREEN_WIDTH, 16, Green);
// Draw Bird
DrawRectangle(birdX, birdY, birdSize, birdSize, RED);
// Draw Objects
for (int i = 0; i < MAX_OBJECTS; i++) {
DrawRectangleRec(objects[i].rect, DARKGRAY);
}
// Text
DrawText("Flappy Bird", 10, 7, 24, BLACK);
DrawText(TextFormat("SCORE: %d", score), 10, 40, 20, DARKBLUE);
DrawText(TextFormat("HIGH SCORE: %d", highScore), 10, 70, 20, MAROON);
if (gameOver) {
DrawText("GAME OVER!", SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 40, 30, RED);
DrawText("Press [ENTER] to Restart", SCREEN_WIDTH / 2 - 120, SCREEN_HEIGHT / 2, 20, DARKGRAY);
}
EndDrawing();
}
CloseWindow();
return 0;
}
www.raylib.com | itch.io | GitHub | Discord | YouTube
- Architecture
- Syntax analysis
- Data structures
- Enumerated types
- External dependencies
- GLFW dependency
- libc dependency
- Platforms and graphics
- Input system
- Default shader
- Custom shaders
- Coding conventions
- Integration with other libs
- Working on Windows
- Working on macOS
- Working on GNU Linux
- Working on Chrome OS
- Working on FreeBSD
- Working on Raspberry Pi
- Working for Android
- Working for Web (HTML5)
- Working on exaequOS Web Computer
- Creating Discord Activities
- Working anywhere with CMake
- CMake Build Options
- raylib templates: Get started easily
- How To: Quick C/C++ Setup in Visual Studio 2022, GCC or MinGW
- How To: C# Visual Studio Setup
- How To: VSCode
- How To: Eclipse
- How To: Sublime Text
- How To: Code::Blocks