From 8da0a514a0d8d2685dc416685c6990e0f08ce400 Mon Sep 17 00:00:00 2001 From: sourovislam33 Date: Tue, 5 Aug 2025 00:18:34 +0600 Subject: [PATCH] Created Working with Raylib in WebAssembly (markdown) --- Working-with-Raylib-in-WebAssembly.md | 140 ++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Working-with-Raylib-in-WebAssembly.md diff --git a/Working-with-Raylib-in-WebAssembly.md b/Working-with-Raylib-in-WebAssembly.md new file mode 100644 index 0000000..7b145c9 --- /dev/null +++ b/Working-with-Raylib-in-WebAssembly.md @@ -0,0 +1,140 @@ +#include "raylib.h" +#include + +#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; +}