-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
61 lines (55 loc) · 1.68 KB
/
main.rs
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
mod draw;
extern crate tetris_core;
use draw::{draw_block, to_coord};
use piston_window::types::Color as PistonColor;
use piston_window::*;
use rand::Rng;
use tetris_core::{Action, Game, Randomizer, Size};
const BACK_COLOR: PistonColor = [0.5, 0.5, 0.5, 1.0];
struct Rand;
impl Randomizer for Rand {
fn random_between(&self, lower: i32, higher: i32) -> i32 {
let mut rng = rand::thread_rng();
return rng.gen_range(lower..higher);
}
}
fn main() {
let game_size = Size {
height: 20,
width: 10,
};
let mut window: PistonWindow = WindowSettings::new(
"Tetris",
[
to_coord(game_size.width as i32),
to_coord(game_size.height as i32),
],
)
.exit_on_esc(true)
.build()
.unwrap();
let rand = Rand {};
let mut game = Game::new(&game_size, Box::new(rand));
while let Some(event) = window.next() {
if let Some(Button::Keyboard(key)) = event.press_args() {
if game.is_game_over() {
game = Game::new(&game_size, Box::new(Rand {}));
}
match key {
Key::Left => game.perform(Action::MoveLeft),
Key::Right => game.perform(Action::MoveRight),
Key::Space => game.perform(Action::Rotate),
Key::Down => game.perform(Action::MoveDown),
_ => continue,
}
}
window.draw_2d(&event, |ctx, g2d, _| {
clear(BACK_COLOR, g2d);
let game_blocks = game.draw();
for block in game_blocks {
draw_block(block, &ctx, g2d);
}
});
event.update(|arg| game.update(arg.dt));
}
}