Add unicode snake printing
Removed a lot of unused and commented code
This commit is contained in:
+18
-3
@@ -5,6 +5,7 @@ use std::collections::LinkedList;
|
||||
use std::ops::Sub;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
#[derive(Debug)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
@@ -39,6 +40,20 @@ impl Direction {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
// impl std::fmt::Debug for Direction {
|
||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
// // write!(f,)
|
||||
// write!(
|
||||
// f,
|
||||
// "{}",
|
||||
// match *self {
|
||||
// Self::Up => (),
|
||||
// _ => (),
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
pub enum CellType {
|
||||
Food,
|
||||
@@ -89,10 +104,10 @@ impl Cell {
|
||||
}
|
||||
pub fn is_adjacent(&self, other: &Cell) -> Option<Direction> {
|
||||
match *self - *other {
|
||||
(0, 1) => Some(Direction::Left),
|
||||
(0, 1) => Some(Direction::Right),
|
||||
(1, 0) => Some(Direction::Down),
|
||||
(-1, 0) => Some(Direction::Right),
|
||||
(0, -1) => Some(Direction::Up),
|
||||
(-1, 0) => Some(Direction::Up),
|
||||
(0, -1) => Some(Direction::Left),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+50
-49
@@ -10,8 +10,6 @@ pub fn game_window(mlines: i32, mcols: i32, vmargin: i32, hmargin: i32) -> WINDO
|
||||
let (starty, startx): (i32, i32);
|
||||
lines = mlines - vmargin * 2;
|
||||
cols = mcols - hmargin * 2;
|
||||
// starty = (mlines - lines) / 2;
|
||||
// startx = (mcols - cols) / 2;
|
||||
starty = vmargin;
|
||||
startx = hmargin;
|
||||
game_win = newwin(lines, cols, starty, startx);
|
||||
@@ -41,54 +39,67 @@ pub fn draw_snake(snake: &Snake, game_win: WINDOW) {
|
||||
// So I'll need to know the last and next cell of the snake to draw the current snake_cell
|
||||
// For some reason the snake goes invisible after the first food
|
||||
prev = snake_iter.next().unwrap(); // currently this should be head. On initial run this should be the only snake_cell
|
||||
mvwaddstr(game_win, prev.posyx().0, prev.posyx().1, &format!("H"));
|
||||
mvwaddstr(
|
||||
game_win,
|
||||
prev.posyx().0,
|
||||
prev.posyx().1,
|
||||
&format!("{}", std::char::from_u32(0x0298).unwrap_or('O')),
|
||||
);
|
||||
let _current = snake_iter.next();
|
||||
// current = match _current {
|
||||
// Some(cell) => cell,
|
||||
// None => return,
|
||||
// };
|
||||
if _current.is_some() {
|
||||
current = _current.unwrap();
|
||||
} else {
|
||||
return;
|
||||
} // the match is equivalent to this block of code
|
||||
// this should be none in the initial run
|
||||
// as of now this will panic as soon as the game starts
|
||||
// I need to match this with Some(Cell) and None and if
|
||||
// none then just straight exit from the function
|
||||
|
||||
// The head is never in current so the head is not being printed as of now
|
||||
current = match _current {
|
||||
Some(cell) => cell,
|
||||
None => return,
|
||||
};
|
||||
for next in snake_iter {
|
||||
// O(n) the whole snake is redrawn every single tick
|
||||
let (snake_l, snake_c): (i32, i32) = current.posyx();
|
||||
// mvwaddstr(game_win, snake_l, snake_c, "o");
|
||||
let snake_char = match (
|
||||
let snake_char: u32 = match (
|
||||
prev.is_adjacent(current).unwrap(),
|
||||
next.is_adjacent(current).unwrap(),
|
||||
) {
|
||||
(Direction::Up, Direction::Down) | (Direction::Down, Direction::Up) => "║", //186, //boxdraw double vertical line ║ code 186
|
||||
(Direction::Up, Direction::Left) | (Direction::Left, Direction::Left) => "╝", //188, // ╝
|
||||
(Direction::Up, Direction::Right) | (Direction::Right, Direction::Up) => "╚", //200, // ╚
|
||||
(Direction::Down, Direction::Left) | (Direction::Left, Direction::Down) => "╗", //187, // ╗
|
||||
(Direction::Down, Direction::Right) | (Direction::Right, Direction::Down) => "╔", //,201, // ╔
|
||||
(Direction::Left, Direction::Right) | (Direction::Right, Direction::Left) => "═", //205 ═
|
||||
_ => " ",
|
||||
(Direction::Up, Direction::Down) | (Direction::Down, Direction::Up) => 0x2551, //"║"
|
||||
(Direction::Up, Direction::Left) | (Direction::Left, Direction::Up) => 0x255d, //"╝"
|
||||
(Direction::Up, Direction::Right) | (Direction::Right, Direction::Up) => 0x255a, //"╚"
|
||||
(Direction::Down, Direction::Left) | (Direction::Left, Direction::Down) => 0x2557, // "╗"
|
||||
(Direction::Down, Direction::Right) | (Direction::Right, Direction::Down) => 0x2554, //"╔"
|
||||
(Direction::Left, Direction::Right) | (Direction::Right, Direction::Left) => 0x2550, //"═"
|
||||
_ => 0x20,
|
||||
};
|
||||
mvwaddstr(
|
||||
game_win, snake_l, snake_c, // &format!("{}", snake_char as char),
|
||||
// &format!("{}", snake_char),
|
||||
"o",
|
||||
game_win,
|
||||
snake_l,
|
||||
snake_c,
|
||||
&format!("{}", std::char::from_u32(snake_char).unwrap_or('o')),
|
||||
);
|
||||
prev = current;
|
||||
current = next;
|
||||
}
|
||||
mvwaddstr(game_win, current.posyx().0, current.posyx().1, "t");
|
||||
|
||||
mvwaddstr(
|
||||
game_win,
|
||||
current.posyx().0,
|
||||
current.posyx().1,
|
||||
&format!(
|
||||
"{}",
|
||||
std::char::from_u32(match current.is_adjacent(prev).unwrap() {
|
||||
Direction::Up | Direction::Down => 0x2551,
|
||||
Direction::Left | Direction::Right => 0x2550,
|
||||
})
|
||||
.unwrap_or('o')
|
||||
),
|
||||
);
|
||||
wrefresh(game_win);
|
||||
}
|
||||
|
||||
pub fn draw_board(board: &Board, game_win: WINDOW) {
|
||||
let (food_l, food_c): (i32, i32) = board.food_posyx();
|
||||
mvwaddstr(game_win, food_l, food_c, "F");
|
||||
mvwaddstr(
|
||||
game_win,
|
||||
food_l,
|
||||
food_c,
|
||||
&format!("{}", std::char::from_u32(0x0298).unwrap_or('F')),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn _log(snake: &Snake, board: &Board) {
|
||||
@@ -96,26 +107,16 @@ pub fn _log(snake: &Snake, board: &Board) {
|
||||
let (bfl, bfc): (i32, i32) = board.food_posyx();
|
||||
mvwaddstr(stdscr(), 0, 0, &format!("snake:head: {} {} ", shl, shc));
|
||||
mvwaddstr(stdscr(), 1, 0, &format!("board:food: {} {} ", bfl, bfc));
|
||||
// mvwaddstr(
|
||||
// stdscr(),
|
||||
// 2,
|
||||
// 0,
|
||||
// &format!(
|
||||
// "board:maxlines {} maxcols {}",
|
||||
// board.maxlines, board.maxcols
|
||||
// ),
|
||||
// );
|
||||
|
||||
wmove(stdscr(), 2, 0);
|
||||
for snake_cell in snake.iter() {
|
||||
let (scl, scc): (i32, i32) = snake_cell.posyx();
|
||||
waddstr(stdscr(), &format!("cell: {} {} ", scl, scc));
|
||||
waddstr(
|
||||
stdscr(),
|
||||
&format!(
|
||||
"<cell y:{} x:{}>",
|
||||
snake_cell.posyx().0,
|
||||
snake_cell.posyx().1
|
||||
),
|
||||
);
|
||||
}
|
||||
mvwaddstr(
|
||||
stdscr(),
|
||||
3,
|
||||
0,
|
||||
&format!("snake_size {}", snake.iter().size_hint().0),
|
||||
);
|
||||
wrefresh(stdscr());
|
||||
}
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ pub fn start() {
|
||||
loop {
|
||||
frontend::draw_snake(&snake, game_win); // always draw snake before board because the snake will clear the game win
|
||||
frontend::draw_board(&board, game_win);
|
||||
frontend::_log(&snake, &board);
|
||||
// frontend::_log(&snake, &board);
|
||||
if board.check_collision(&snake) {
|
||||
// Add stuff here to show the score and
|
||||
// how You lose screen
|
||||
|
||||
+3
-5
@@ -5,11 +5,13 @@ mod menu;
|
||||
// use game::{Cell, Snake};
|
||||
// use ncurses::*;
|
||||
use ncurses::{
|
||||
curs_set, endwin, getmaxyx, initscr, keypad, noecho, raw, refresh, stdscr, CURSOR_VISIBILITY,
|
||||
curs_set, endwin, getmaxyx, initscr, keypad, noecho, raw, refresh, setlocale, stdscr,
|
||||
LcCategory, CURSOR_VISIBILITY,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
// let (lines, cols): (i32, i32) = (0, 0);
|
||||
setlocale(LcCategory::all, "");
|
||||
initscr();
|
||||
raw();
|
||||
keypad(stdscr(), true);
|
||||
@@ -17,10 +19,6 @@ fn main() {
|
||||
noecho();
|
||||
let (mut mlines, mut mcols): (i32, i32) = (0, 0);
|
||||
getmaxyx(stdscr(), &mut mlines, &mut mcols);
|
||||
// let (mlines, mcols) = match getmaxyx(stdscr()) {
|
||||
// Ok(size) => (size.lines, size.columns),
|
||||
// Err(e) => panic!(e),
|
||||
// };
|
||||
if (mlines < 20) || (mcols < 35) {
|
||||
refresh();
|
||||
endwin();
|
||||
|
||||
Reference in New Issue
Block a user