rust-ufo/src/main.rs

51 lines
1.1 KiB
Rust

use std::thread;
use std::time::Duration;
use rand::Rng;
use crate::Direction::Left;
use crate::movable::Movable;
use crate::movable::tank::Tank;
use crate::movable::ufo::Ufo;
mod terminal;
mod movable;
const DELAY: Duration = Duration::from_millis(10);
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() {
let terminal = terminal::setup();
const MAX_UFOS: u8 = 100;
let mut tank = Tank::create();
let mut ufos = vec![Ufo::create(), Ufo::create(), Ufo::create(), Ufo::create(), Ufo::create()];
let mut n_ufos = ufos.len() as u8;
loop {
let iter_mut = ufos.iter_mut();
for x in iter_mut {
x.mov(Left);
}
if rand::thread_rng().gen_bool(0.1) && n_ufos < MAX_UFOS {
ufos.push(Ufo::create());
n_ufos += 1;
}
ufos.retain(|ufo| ufo.is_on_screen());
if ufos.is_empty() {
break;
}
thread::sleep(DELAY);
}
terminal::restore(terminal);
}
#[derive(PartialEq)]
pub enum Direction {
Left,
Right,
Up,
Down,
}