Skip to content

Commit e95ce0a

Browse files
authored
Raylib post (#181)
* add mini-game * update * update ci * just use presets * update cmake
1 parent 9f413f5 commit e95ce0a

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
project(runner_game)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
find_package(raylib)
6+
7+
add_executable(runner_game main.cpp)
8+
target_link_libraries(runner_game raylib)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example using raylib to create a game
2+
3+
Example for using [raylib](https://www.raylib.com/) to create a simple runner game.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import platform
2+
from test.examples_tools import run
3+
4+
print("raylib example")
5+
6+
run("conan install . --build=missing")
7+
8+
if platform.system() == "Windows":
9+
run("cmake --preset conan-default")
10+
run("cmake --build --preset conan-release")
11+
else:
12+
run("cmake --preset conan-release")
13+
run("cmake --build --preset conan-release")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from conan import ConanFile
2+
from conan.tools.cmake import cmake_layout, CMakeToolchain
3+
4+
class ConanApplication(ConanFile):
5+
package_type = "application"
6+
settings = "os", "compiler", "build_type", "arch"
7+
generators = "CMakeDeps", "CMakeToolchain"
8+
9+
def layout(self):
10+
cmake_layout(self)
11+
12+
def requirements(self):
13+
self.requires("raylib/5.5")
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "raylib.h"
2+
#include <vector>
3+
4+
int main() {
5+
// --- Initialization ---
6+
const int screenW = 800;
7+
const int screenH = 450;
8+
InitWindow(screenW, screenH, "Jump to Survive!");
9+
10+
// --- Player Setup ---
11+
Rectangle player = { 100, screenH - 80, 40, 60 };
12+
float vy = 0;
13+
const float gravity = 1000.0f;
14+
const float jumpImpulse = -450.0f;
15+
16+
// --- Ground Definition ---
17+
const int groundY = screenH - 20;
18+
19+
// --- Obstacle Management ---
20+
std::vector<Rectangle> obstacles;
21+
float spawnTimer = 0.0f;
22+
float spawnInterval = 1.2f;
23+
const float obstacleSpeed = 300.0f;
24+
25+
const float minSpawnInterval = 0.8f;
26+
const float maxSpawnInterval = 1.6f;
27+
28+
const int minObsWidth = 40;
29+
const int maxObsWidth = 120;
30+
31+
// --- Game State Variables ---
32+
int score = 0;
33+
bool gameOver = false;
34+
35+
SetTargetFPS(60);
36+
37+
while (!WindowShouldClose()) {
38+
float dt = GetFrameTime();
39+
40+
if (!gameOver) {
41+
// Jump logic
42+
if (IsKeyPressed(KEY_SPACE) && player.y + player.height >= groundY) {
43+
vy = jumpImpulse;
44+
}
45+
vy += gravity * dt;
46+
player.y += vy * dt;
47+
if (player.y + player.height > groundY) {
48+
player.y = groundY - player.height;
49+
vy = 0;
50+
}
51+
52+
// Spawn obstacles with random width & interval
53+
spawnTimer += dt;
54+
if (spawnTimer >= spawnInterval) {
55+
spawnTimer = 0.0f;
56+
// recalc next interval
57+
spawnInterval = GetRandomValue(int(minSpawnInterval*100), int(maxSpawnInterval*100)) / 100.0f;
58+
// random width
59+
int w = GetRandomValue(minObsWidth, maxObsWidth);
60+
obstacles.push_back({ float(screenW), float(groundY - 40), float(w), 40.0f });
61+
}
62+
63+
// Move & collide obstacles
64+
for (int i = 0; i < (int)obstacles.size(); i++) {
65+
obstacles[i].x -= obstacleSpeed * dt;
66+
if (CheckCollisionRecs(player, obstacles[i])) {
67+
gameOver = true;
68+
}
69+
}
70+
// Remove off-screen & score
71+
if (!obstacles.empty() && obstacles.front().x + obstacles.front().width < 0) {
72+
obstacles.erase(obstacles.begin());
73+
score++;
74+
}
75+
}
76+
else {
77+
if (IsKeyPressed(KEY_R)) {
78+
// reset everything
79+
player.y = screenH - 80;
80+
vy = 0;
81+
obstacles.clear();
82+
spawnTimer = 0.0f;
83+
spawnInterval = 1.2f;
84+
score = 0;
85+
gameOver = false;
86+
}
87+
}
88+
89+
// --- Drawing ---
90+
BeginDrawing();
91+
ClearBackground(RAYWHITE);
92+
93+
DrawRectangle(0, groundY, screenW, 20, DARKGRAY);
94+
DrawRectangleRec(player, BLUE);
95+
for (auto &obs : obstacles) DrawRectangleRec(obs, RED);
96+
97+
DrawText(TextFormat("Score: %d", score), 10, 10, 20, BLACK);
98+
99+
if (gameOver) {
100+
DrawText("GAME OVER! Press R to restart", 200, screenH/2 - 20, 20, MAROON);
101+
}
102+
103+
EndDrawing();
104+
}
105+
106+
CloseWindow();
107+
return 0;
108+
}

0 commit comments

Comments
 (0)