summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2026-05-28 09:48:49 +0200
committerDominik Kaiser2026-05-28 09:48:49 +0200
commit827d5ad9c412791dbbb367bb09316e7ce29351e3 (patch)
treef3663e6f8b55bf7576fb4433fed9441c29be954f
downloadflappy-bird-827d5ad9c412791dbbb367bb09316e7ce29351e3.tar.gz
flappy-bird-827d5ad9c412791dbbb367bb09316e7ce29351e3.zip
Rust implementation
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock272
-rw-r--r--Cargo.toml7
-rw-r--r--src/main.rs109
4 files changed, 389 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..124914a
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,272 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "adler2"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
+
+[[package]]
+name = "allocator-api2"
+version = "0.2.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
+
+[[package]]
+name = "autocfg"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "bytemuck"
+version = "1.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
+
+[[package]]
+name = "byteorder"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+
+[[package]]
+name = "color_quant"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
+
+[[package]]
+name = "crc32fast"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "equivalent"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
+
+[[package]]
+name = "fdeflate"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
+dependencies = [
+ "simd-adler32",
+]
+
+[[package]]
+name = "flate2"
+version = "1.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
+dependencies = [
+ "crc32fast",
+ "miniz_oxide",
+]
+
+[[package]]
+name = "foldhash"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
+
+[[package]]
+name = "fontdue"
+version = "0.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e57e16b3fe8ff4364c0661fdaac543fb38b29ea9bc9c2f45612d90adf931d2b"
+dependencies = [
+ "hashbrown",
+ "ttf-parser",
+]
+
+[[package]]
+name = "glam"
+version = "0.27.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9"
+
+[[package]]
+name = "hashbrown"
+version = "0.15.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
+dependencies = [
+ "allocator-api2",
+ "equivalent",
+ "foldhash",
+]
+
+[[package]]
+name = "image"
+version = "0.24.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d"
+dependencies = [
+ "bytemuck",
+ "byteorder",
+ "color_quant",
+ "num-traits",
+ "png",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.182"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
+
+[[package]]
+name = "macroquad"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2befbae373456143ef55aa93a73594d080adfb111dc32ec96a1123a3e4ff4ae"
+dependencies = [
+ "fontdue",
+ "glam",
+ "image",
+ "macroquad_macro",
+ "miniquad",
+ "quad-rand",
+]
+
+[[package]]
+name = "macroquad_macro"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64b1d96218903768c1ce078b657c0d5965465c95a60d2682fd97443c9d2483dd"
+
+[[package]]
+name = "malloc_buf"
+version = "0.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "miniquad"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2fb3e758e46dbc45716a8a49ca9edc54b15bcca826277e80b1f690708f67f9e3"
+dependencies = [
+ "libc",
+ "ndk-sys",
+ "objc-rs",
+ "winapi",
+]
+
+[[package]]
+name = "miniz_oxide"
+version = "0.8.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
+dependencies = [
+ "adler2",
+ "simd-adler32",
+]
+
+[[package]]
+name = "my_game"
+version = "0.1.0"
+dependencies = [
+ "macroquad",
+]
+
+[[package]]
+name = "ndk-sys"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121"
+
+[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "objc-rs"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64a1e7069a2525126bf12a9f1f7916835fafade384fb27cabf698e745e2a1eb8"
+dependencies = [
+ "malloc_buf",
+]
+
+[[package]]
+name = "png"
+version = "0.17.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526"
+dependencies = [
+ "bitflags",
+ "crc32fast",
+ "fdeflate",
+ "flate2",
+ "miniz_oxide",
+]
+
+[[package]]
+name = "quad-rand"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a651516ddc9168ebd67b24afd085a718be02f8858fe406591b013d101ce2f40"
+
+[[package]]
+name = "simd-adler32"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
+
+[[package]]
+name = "ttf-parser"
+version = "0.21.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c591d83f69777866b9126b24c6dd9a18351f177e49d625920d19f989fd31cf8"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..61b00cb
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "my_game"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+macroquad = "0.4.14"
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..513a830
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,109 @@
+use macroquad::prelude::*;
+
+const SCREEN_WIDTH: f32 = 500.0;
+const SCREEN_HEIGHT: f32 = 800.0;
+const PLAYER_SIZE: f32 = 50.0;
+const GRAVITY: f32 = 1.2;
+const JUMP_HEIGHT: f32 = 20.0;
+const BAR_GAP: f32 = 150.0;
+const BAR_SPEED: f32 = 2.0;
+const BAR_WIDTH: f32 = 50.0;
+
+struct Player {
+ x: f32,
+ y: f32,
+ velocity_y: f32,
+}
+
+impl Player {
+ fn draw(&self) {
+ draw_rectangle(self.x, self.y, PLAYER_SIZE, PLAYER_SIZE, GOLD);
+ }
+
+ fn is_colliding_with_bar(&self, bar: &Bar) -> bool {
+ if self.x + PLAYER_SIZE < bar.x
+ || self.x > bar.x + BAR_WIDTH {
+ return false;
+ }
+ if self.y + PLAYER_SIZE < bar.y + BAR_GAP && self.y > bar.y - BAR_GAP {
+ return false;
+ }
+ true
+ }
+}
+
+struct Bar {
+ x: f32,
+ y: f32,
+ velocity_x: f32,
+}
+
+impl Bar {
+ fn draw(&self) {
+ draw_rectangle(self.x, 0.0, BAR_WIDTH, self.y - BAR_GAP, BLACK);
+ draw_rectangle(self.x, self.y + BAR_GAP, BAR_WIDTH, SCREEN_HEIGHT, BLACK);
+ }
+}
+
+#[macroquad::main("FlappyBird")]
+async fn main() {
+ loop {
+ loop {
+ request_new_screen_size(SCREEN_WIDTH, SCREEN_HEIGHT);
+ clear_background(BLUE);
+ if is_key_pressed(KeyCode::Enter) {
+ break;
+ }
+ next_frame().await
+ }
+
+ let score = play().await;
+ println!("{}", score);
+ }
+}
+
+async fn play() -> i32 {
+ let x = 100.0;
+ let y = (SCREEN_HEIGHT - PLAYER_SIZE) / 2.0;
+ let mut player = Player {x, y, velocity_y: 0.0};
+
+ let bar_y = macroquad::rand::gen_range(2.0 * BAR_GAP, SCREEN_HEIGHT - (2.0 * BAR_GAP));
+ let mut bar = Bar {x: SCREEN_WIDTH, y: bar_y, velocity_x: BAR_SPEED};
+
+ let mut score = 0;
+
+ loop {
+ clear_background(BLUE);
+
+ player.velocity_y += GRAVITY;
+
+ if is_key_pressed(KeyCode::Space) {
+ player.velocity_y = -JUMP_HEIGHT;
+ }
+
+ if player.y < 0.0 || player.y > SCREEN_HEIGHT - PLAYER_SIZE {
+ break;
+ }
+ player.y += player.velocity_y;
+
+ bar.x -= bar.velocity_x;
+
+ if bar.x < -BAR_WIDTH {
+ bar.x = SCREEN_WIDTH;
+ bar.y = (2.0 * BAR_GAP) +
+ (macroquad::rand::rand() as f32 % (SCREEN_HEIGHT - (4.0 * BAR_GAP)));
+ score += 1;
+ }
+
+ if player.is_colliding_with_bar(&bar) {
+ break;
+ }
+
+ player.draw();
+ bar.draw();
+ draw_text(&score.to_string(), SCREEN_WIDTH / 2.0 - 25.0, 50.0, 60.0, WHITE);
+
+ next_frame().await
+ }
+ score
+} \ No newline at end of file