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