summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominik Kaiser2026-05-28 12:13:26 +0200
committerDominik Kaiser2026-05-28 12:13:26 +0200
commitb630408e81bc4edadbcf29ea57638c2641e021c1 (patch)
treea1acec3b04d955ff4ac42bc3684355969b375a74 /src
parent827d5ad9c412791dbbb367bb09316e7ce29351e3 (diff)
downloadflappy-bird-b630408e81bc4edadbcf29ea57638c2641e021c1.tar.gz
flappy-bird-b630408e81bc4edadbcf29ea57638c2641e021c1.zip
Refactor and improverust-impl
Diffstat (limited to 'src')
-rw-r--r--src/main.rs89
1 files changed, 52 insertions, 37 deletions
diff --git a/src/main.rs b/src/main.rs
index 513a830..190d3e3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,53 +10,55 @@ const BAR_SPEED: f32 = 2.0;
const BAR_WIDTH: f32 = 50.0;
struct Player {
- x: f32,
- y: f32,
- velocity_y: f32,
+ 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;
+ fn draw(&self) {
+ draw_rectangle(self.x, self.y, PLAYER_SIZE, PLAYER_SIZE, GOLD);
}
- if self.y + PLAYER_SIZE < bar.y + BAR_GAP && self.y > bar.y - BAR_GAP {
- return false;
+
+ 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
}
- true
- }
}
struct Bar {
- x: f32,
- y: f32,
- velocity_x: f32,
+ 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);
- }
+ 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 {
+ 'game: loop {
loop {
request_new_screen_size(SCREEN_WIDTH, SCREEN_HEIGHT);
clear_background(BLUE);
- if is_key_pressed(KeyCode::Enter) {
+ if is_key_pressed(KeyCode::Space) {
break;
}
- next_frame().await
+ if is_key_pressed(KeyCode::Escape) {
+ break 'game;
+ }
+ next_frame().await;
}
-
+
let score = play().await;
println!("{}", score);
}
@@ -65,24 +67,32 @@ async fn main() {
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 player = Player {
+ x,
+ y,
+ velocity_y: 0.0,
+ };
+
+ let bar_y = macroquad::rand::gen_range(1.5 * BAR_GAP, SCREEN_HEIGHT - (1.5 * 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;
+ player.velocity_y = -JUMP_HEIGHT;
}
if player.y < 0.0 || player.y > SCREEN_HEIGHT - PLAYER_SIZE {
- break;
+ break;
}
player.y += player.velocity_y;
@@ -90,8 +100,7 @@ async fn play() -> i32 {
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)));
+ bar.y = macroquad::rand::gen_range(1.5 * BAR_GAP, SCREEN_HEIGHT - (1.5 * BAR_GAP));
score += 1;
}
@@ -101,9 +110,15 @@ async fn play() -> i32 {
player.draw();
bar.draw();
- draw_text(&score.to_string(), SCREEN_WIDTH / 2.0 - 25.0, 50.0, 60.0, WHITE);
+ 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
+}