fix: des trucs
This commit is contained in:
41
src/main.rs
41
src/main.rs
@@ -20,7 +20,7 @@ enum Direction {
|
||||
}
|
||||
|
||||
fn read_inputs() -> Option<KeyCode> {
|
||||
if event::poll(Duration::from_millis(100)).unwrap() {
|
||||
if event::poll(Duration::from_millis(300)).unwrap() {
|
||||
if let Event::Key(KeyEvent { code, .. }) = event::read().unwrap() {
|
||||
return Some(code);
|
||||
}
|
||||
@@ -29,19 +29,21 @@ fn read_inputs() -> Option<KeyCode> {
|
||||
None
|
||||
}
|
||||
|
||||
fn draw_gui() {
|
||||
fn draw_gui(max_x: u16, max_y: u16) {
|
||||
let mut stdout = stdout();
|
||||
stdout.execute(cursor::MoveTo(0, 0)).unwrap();
|
||||
println!("SNAKE TUI");
|
||||
stdout.execute(cursor::MoveTo(0, 1)).unwrap();
|
||||
stdout.execute(SetForegroundColor(Color::White)).unwrap();
|
||||
println!("┌───────────────────────────────────┐");
|
||||
for i in 2..19 {
|
||||
println!("┌{}┐", "──".repeat(max_x as usize + 2));
|
||||
|
||||
for i in 2..max_y+3 {
|
||||
stdout.execute(cursor::MoveTo(0, i)).unwrap();
|
||||
println!("│ │")
|
||||
println!("│{}│", " ".repeat((max_x as usize+2) * 2))
|
||||
}
|
||||
stdout.execute(cursor::MoveTo(0, 19)).unwrap();
|
||||
println!("└───────────────────────────────────┘");
|
||||
|
||||
stdout.execute(cursor::MoveTo(0, max_y+3)).unwrap();
|
||||
println!("└{}┘", "──".repeat(max_x as usize + 2));
|
||||
|
||||
stdout.execute(ResetColor).unwrap();
|
||||
stdout.flush().unwrap();
|
||||
@@ -70,17 +72,17 @@ fn game_over() {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
const MAX_X : u16 = 16;
|
||||
const MAX_Y: u16 = 16;
|
||||
const MAX_X : u16 = 7;
|
||||
const MAX_Y: u16 = 7;
|
||||
|
||||
let mut head_x: u16 = 8;
|
||||
let mut head_y: u16 = 8;
|
||||
let mut head_x: u16 = 4;
|
||||
let mut head_y: u16 = 4;
|
||||
let mut body_length: usize = 0;
|
||||
let mut body: VecDeque<(u16,u16)> = Default::default();
|
||||
let mut direction: Direction = Direction::Up;
|
||||
|
||||
let mut apple_x: u16 = random_range(0..17);
|
||||
let mut apple_y: u16 = random_range(0..17);
|
||||
let mut apple_x: u16 = random_range(0..MAX_X+1);
|
||||
let mut apple_y: u16 = random_range(0..MAX_Y+1);
|
||||
|
||||
let mut stdout = stdout();
|
||||
terminal::enable_raw_mode().unwrap();
|
||||
@@ -111,21 +113,24 @@ fn main() {
|
||||
|
||||
|
||||
// CHECKS
|
||||
// if body_length == &MAX_X*&MAX_Y { win(); }
|
||||
if body_length == MAX_X as usize * MAX_Y as usize { win(); }
|
||||
if (head_x, head_y) == (apple_x, apple_y) {
|
||||
body_length += 1;
|
||||
|
||||
loop {
|
||||
apple_x = random_range(0..17);
|
||||
apple_y = random_range(0..17);
|
||||
apple_x = random_range(0..MAX_X+1);
|
||||
apple_y = random_range(0..MAX_Y+1);
|
||||
|
||||
if !body.contains(&(apple_x, apple_y)) { break; }
|
||||
if !body.contains(&(apple_x, apple_y)) &&
|
||||
(apple_x, apple_y) != (head_x, head_y) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DRAW
|
||||
stdout.execute(Clear(ClearType::All)).unwrap();
|
||||
draw_gui();
|
||||
draw_gui(MAX_X, MAX_Y);
|
||||
|
||||
// Apple
|
||||
stdout.execute(cursor::MoveTo((apple_x+1)*2, apple_y+2)).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user