78 lines
1.7 KiB
Rust
78 lines
1.7 KiB
Rust
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
use rand::Rng;
|
|
|
|
use crate::Direction::{Down, Left, Right, Up};
|
|
use crate::movables::{Movable, Ufo};
|
|
|
|
mod movables;
|
|
mod terminal;
|
|
|
|
const WIDTH: u16 = 160;
|
|
const HEIGHT: u16 = 40;
|
|
const HEADER_ROW: u16 = 0;
|
|
const FOOTER_ROW: u16 = HEIGHT - 1;
|
|
const TANK_ROW: u16 = FOOTER_ROW - 1;
|
|
const MIN_UFO_ROW: u16 = 1;
|
|
const MAX_UFO_ROW: u16 = TANK_ROW - 5;
|
|
const DELAY: Duration = Duration::from_millis(10);
|
|
const UFO_STR: &str = "<=000=>";
|
|
const TANK_STR: &str = "⊆≡≣🠭≣≡⊇";
|
|
|
|
|
|
fn main() {
|
|
let terminal = terminal::setup();
|
|
const MAX_UFOS: u8 = 100;
|
|
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(Direction::Left);
|
|
x.draw();
|
|
}
|
|
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);
|
|
}
|
|
// for _ in 0..N_UFOS {
|
|
// let mut ufo = Ufo::create();
|
|
// while ufo.is_on_screen() {
|
|
// ufo.mov(Direction::Left);
|
|
// ufo.draw();
|
|
// thread::sleep(DELAY);
|
|
// }
|
|
// }
|
|
|
|
terminal::restore(terminal);
|
|
}
|
|
|
|
fn tank_at(col: u16) {
|
|
terminal::print_str_at(col, TANK_ROW, TANK_STR);
|
|
}
|
|
|
|
pub enum Direction {
|
|
Left,
|
|
Right,
|
|
Up,
|
|
Down,
|
|
}
|
|
|
|
impl Direction {
|
|
fn invert(&self) -> Direction {
|
|
match self {
|
|
Left => Right,
|
|
Right => Left,
|
|
Up => Down,
|
|
Down => Up
|
|
}
|
|
}
|
|
}
|