summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 190d3e3ba6305d7a1d1aeac14c3902b4062e2771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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() {
    'game: loop {
        loop {
            request_new_screen_size(SCREEN_WIDTH, SCREEN_HEIGHT);
            clear_background(BLUE);
            if is_key_pressed(KeyCode::Space) {
                break;
            }
            if is_key_pressed(KeyCode::Escape) {
                break 'game;
            }
            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(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;
        }

        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 = macroquad::rand::gen_range(1.5 * BAR_GAP, SCREEN_HEIGHT - (1.5 * 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
}