b717d306c3
When you want to work with the 'alternate screen' you can call the following functions: terminal::from_screen etc. Which will give you an instance to the back of the module you are calling it in. So instead of: let color = color(Screen::default()); let cursor = cursor(Screen::default()); let input = input(Screen::default()); let terminal = terminal(Screen::default()); You can do: let color = color(); let cursor = cursor(); let input = input(); let terminal = terminal();
80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
use super::variables::{Position, Size, Direction };
|
|
use super::snake::Snake;
|
|
|
|
use crossterm::{Crossterm, Screen};
|
|
use crossterm::cursor::from_screen;
|
|
use crossterm::style::{ObjectStyle, StyledObject, Color, style};
|
|
|
|
use rand::distributions::{IndependentSample, Range};
|
|
|
|
use std::hash::Hash;
|
|
use std::collections::HashMap;
|
|
use std::fmt::Display;
|
|
use std::ops::Index;
|
|
|
|
use rand;
|
|
|
|
pub struct Map
|
|
{
|
|
pub size: Size,
|
|
pub foot_pos: Position,
|
|
}
|
|
|
|
impl Map
|
|
{
|
|
pub fn new(size: Size) -> Self
|
|
{
|
|
Map { size: size, foot_pos: Position::new(0,0) }
|
|
}
|
|
|
|
// render the map on the screen.
|
|
pub fn render_map(&mut self, screen: &Screen, free_positions: &mut HashMap<String, Position>)
|
|
{
|
|
let crossterm = Crossterm::from_screen(screen);
|
|
let mut cursor = crossterm.cursor();
|
|
let mut terminal = crossterm.terminal();
|
|
|
|
for y in 0..self.size.height
|
|
{
|
|
for x in 0..self.size.height
|
|
{
|
|
if (y == 0 || y == self.size.height - 1) || (x == 0 || x == self.size.width - 1)
|
|
{
|
|
cursor.goto(x as u16, y as u16);
|
|
terminal.write("█")
|
|
}else {
|
|
free_positions.insert(format!("{},{}",x,y), Position::new(x,y));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn is_out_of_bounds(&self, new_pos: Position) -> bool
|
|
{
|
|
if (new_pos.x == 0 || new_pos.x == self.size.width) || (new_pos.y == 0 || new_pos.y == self.size.height)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
pub fn is_food_eaten(&self, snake_head: Position) -> bool
|
|
{
|
|
snake_head.x == self.foot_pos.x && snake_head.y == self.foot_pos.y
|
|
}
|
|
|
|
pub fn spawn_food(&mut self, free_positions: &HashMap<String, Position>, screen: &Screen)
|
|
{
|
|
let index = Range::new(0, free_positions.len()).ind_sample(&mut rand::thread_rng());
|
|
self.foot_pos = free_positions.values().skip(index).next().unwrap().clone();
|
|
self.draw_food(screen);
|
|
}
|
|
|
|
fn draw_food(&self, screen: &Screen)
|
|
{
|
|
from_screen(screen).goto(self.foot_pos.x as u16, self.foot_pos.y as u16);
|
|
style("$").with(Color::Green).paint(screen);
|
|
screen.stdout.flush();
|
|
}
|
|
} |