main.rs 7.92 KB
Newer Older
Astro's avatar
Astro committed
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
#![no_std]
#![no_main]

use core::fmt::Write;
use l0dable::*;

pub const BALL_RADIUS: u16 = 3;
pub const PADDLE_SPACE: u16 = 24;
pub const BLOCK_W: u16 = 8;
pub const BLOCK_H: u16 = 4;
pub const BLOCKS_X: u16 = Display::W / BLOCK_W;
pub const BLOCKS_Y: u16 = (Display::H - PADDLE_SPACE) / BLOCK_H;

pub struct Blocks {
    blocks: [[Option<Color>; BLOCKS_X as usize]; BLOCKS_Y as usize],
}

impl Blocks {
    pub fn new() -> Self {
        Blocks { blocks: [[None; BLOCKS_X as usize]; BLOCKS_Y as usize], }
    }

    pub fn generate() -> Self {
        let mut result = Self::new();
        let mut count = 0;
        for line in result.blocks.iter_mut() {
            for block in line.iter_mut() {
Astro's avatar
Astro committed
28
                let mut buf = [0, 0, 0, 0];
Astro's avatar
Astro committed
29
30
                trng::read(&mut buf);
                if buf[0] > 0xBF {
Astro's avatar
Astro committed
31
32
33
34
35
                    *block = Some(Color::rgb8(
                       0x80 | buf[1],
                       0x80 | buf[2],
                       0x80 | buf[3],
                    ));
Astro's avatar
Astro committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
                    count += 1;
                }
            }
        }
        writeln!(UART, "Generated {} blocks\r", count).unwrap();
        result
    }

    pub fn collides(&mut self, x: u16, y: u16) -> bool {
        let col = (x / BLOCK_W) as usize;
        let line = (y / BLOCK_H) as usize;
        if line >= BLOCKS_Y.into() || col >= BLOCKS_X.into() {
            return false;
        }
        match self.blocks[line][col] {
            Some(_) => {
                self.blocks[line][col] = None;
                true
            }
            None => false,
        }
    }
Astro's avatar
Astro committed
58
59
60
61

    fn is_finished(&self) -> bool {
        self.blocks.iter().all(|line| line.iter().all(|block| block.is_none()))
    }
Astro's avatar
Astro committed
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

const PADDLE_HEIGHT: u16 = 4;
const PADDLE_SPEED: u16 = 4;

#[derive(Clone, Copy, Debug)]
enum Direction {
    /// Up Right
    UR,
    /// Up Left
    DR,
    /// Down Left
    DL,
    /// Down Right
    UL,
}

#[derive(Clone, Copy, Debug)]
enum Bounce {
    Vertical,
    Horizontal,
}

impl Direction {
    fn motion(&self, x: &mut u16, y: &mut u16) {
        use Direction::*;
        match self {
            UR => {
                *x += 1;
                *y -= 1;
            }
            DR => {
                *x += 1;
                *y += 1;
            }
            DL => {
                *x -= 1;
                *y += 1;
            }
            UL => {
                *x -= 1;
                *y -= 1;
            }
        }
    }

    fn bounce(&mut self, bounce: Bounce) {
        use Direction::*;
        use Bounce::*;
        match (*self, bounce) {
            (UR, Vertical) => *self = UL,
            (UL, Vertical) => *self = UR,
            (DR, Vertical) => *self = DL,
            (DL, Vertical) => *self = DR,
            (UR, Horizontal) => *self = DR,
            (UL, Horizontal) => *self = DL,
            (DR, Horizontal) => *self = UR,
            (DL, Horizontal) => *self = UL,
        }
    }
}


fn game() -> u32 {
    let start_time = time();
    let display = Display::open();

    let mut running = true;
    let mut score = 0;
    let mut paddle = Display::W / 2;
    let paddle_size = 10;
    let mut ball_x = paddle;
    let mut ball_y = Display::H - PADDLE_HEIGHT - BALL_RADIUS;
    let mut ball_direction = Direction::UR;
    let mut blocks = Blocks::generate();
    while running {
        let input = Buttons::read();
        if input.left_bottom() {
            paddle -= PADDLE_SPEED;
            paddle = paddle.max(paddle_size);
        }
        if input.right_bottom() {
            paddle += PADDLE_SPEED;
            paddle = paddle.min(Display::W - paddle_size)
        }
        if input.left_top() {
            exit(0);
        }
Astro's avatar
Astro committed
150
        let mut check_finish = false;
Astro's avatar
Astro committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        let speed_steps = 1 + (time() - start_time) / 10;
        for _ in 0..speed_steps {
            ball_direction.motion(&mut ball_x, &mut ball_y);

            if ball_x <= BALL_RADIUS || ball_x >= Display::W - BALL_RADIUS {
                // Bounce on left/right border
                ball_direction.bounce(Bounce::Vertical);
            }
            if ball_y <= BALL_RADIUS {
                // Bounce on top border
                ball_direction.bounce(Bounce::Horizontal);
            }
            if ball_y == Display::H - PADDLE_HEIGHT - BALL_RADIUS &&
                ball_x >= paddle - paddle_size &&
                ball_x <= paddle + paddle_size {
                    // Bounce on paddle
                    ball_direction.bounce(Bounce::Horizontal);
                }
            if ball_y >= Display::H - BALL_RADIUS {
                // Game over
                running = false;
            }
            if blocks.collides(ball_x - BALL_RADIUS, ball_y) ||
                blocks.collides(ball_x + BALL_RADIUS, ball_y) {
                    ball_direction.bounce(Bounce::Vertical);
                    score += 100;
                    // paddle_size += 2;
Astro's avatar
Astro committed
178
                    check_finish = true;
Astro's avatar
Astro committed
179
180
181
182
183
184
185
                    vibra::vibrate(60);
                }
            if blocks.collides(ball_x, ball_y - BALL_RADIUS) ||
                blocks.collides(ball_x, ball_y + BALL_RADIUS) {
                    ball_direction.bounce(Bounce::Horizontal);
                    score += 60;
                    // paddle_size += 1;
Astro's avatar
Astro committed
186
                    check_finish = true;
Astro's avatar
Astro committed
187
188
189
                    vibra::vibrate(40);
                }
        }
Astro's avatar
Astro committed
190
191
192
        if check_finish && blocks.is_finished() {
            return score + 1000;
        }
Astro's avatar
Astro committed
193
194
195
196
197
198
        
        display.clear(Color::black());
        // Paddle
        display.rect(
            paddle - paddle_size, Display::H - PADDLE_HEIGHT,
            paddle + paddle_size, Display::H - 1,
Astro's avatar
Astro committed
199
            Color::rgb8(0x7f, 0xff, 0x7f), FillStyle::Filled, 1
Astro's avatar
Astro committed
200
201
202
203
        );
        // Ball
        display.circ(
            ball_x, ball_y, BALL_RADIUS,
Astro's avatar
Astro committed
204
            Color::rgb8(0x7f, 0x7f, 0xff), FillStyle::Filled, 1
Astro's avatar
Astro committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        );
        // Blocks
        for (lineno, line) in blocks.blocks.iter_mut().enumerate() {
            let lineno = lineno as u16;
            for (colno, block) in line.iter_mut().enumerate() {
                block.map(|color| {
                    let colno = colno as u16;
                    display.rect(
                        colno * BLOCK_W,
                        lineno * BLOCK_H,
                        (colno + 1) * BLOCK_W - 1,
                        (lineno + 1) * BLOCK_H - 1,
                        color, FillStyle::Filled, 1
                    );
                });
            }
        }
        display.update();
    }

    score
}

fn title_screen() {
    let display = Display::open();
    display.clear(Color::red());
    display.print(30, 15, b"Rkanoid\0", Color::white(), Color::red());
    display.print(30, 30, b"in Rust\0", Color::white(), Color::red());
    display.print(20, 45, b"by Astro\0", Color::white(), Color::red());
    display.print(Display::W - 2 * Display::FONT_W, Display::H - Display::FONT_H, b"Go\0", Color::yellow(), Color::blue());
    display.update();
    while !Buttons::read().right_bottom() {}
}

fn game_over(score: u32) -> bool {
    let display = Display::open();
    display.clear(Color::red());
    display.print(30, 0, b"Rkanoid\0", Color::white(), Color::red());
    display.print(0, 25, b"Game over!\0", Color::white(), Color::red());
    let mut score_str = [0u8; 32];
    write!(FmtBuffer::new(&mut score_str), "Score: {}\0", score).unwrap();
    display.print(0, 60, &score_str, Color::white(), Color::red());
    display.print(0, 0, b"Q\0", Color::yellow(), Color::blue());
    display.print(Display::W - Display::FONT_W, Display::H - 2 * Display::FONT_H, b"S\0", Color::yellow(), Color::blue());
    display.update();
    loop {
        let buttons = Buttons::read();
        if buttons.left_top() {
            return false
        }
        if buttons.right_top() {
            return true
        }
    }
}

main!(main);
fn main() {
    title_screen();
    
    let mut quit = false;
    while !quit {
        let score = game();
        let again = game_over(score);
        quit = !again;
    }
}