Initial commit I think

Initial commit with just menu and sort of pause menu only
This commit is contained in:
Uttarayan Mondal
2021-01-05 03:40:51 +05:30
commit 4cc7dbce92
5 changed files with 258 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
extern crate rand;
enum CellType {
Snake,
Food,
Empty,
}
struct Cell {
line: i32,
col: i32,
ctype: CellType,
}
impl Default for Cell {
fn default() -> Self {
Self {
line: -1,
col: -1,
ctype: CellType::Empty,
}
}
}
enum Direction {
Up,
Down,
Left,
Right,
}
struct Snake {
length: i32,
}
struct SnakeCell {
pub line: i32,
pub col: i32,
}
impl SnakeCell {
fn next(&self, direction: Direction) -> SnakeCell {
let (dl, dc) = match direction {
Direction::Up => (-1, 0),
Direction::Down => (1, 0),
Direction::Left => (0, -1),
Direction::Right => (0, 1),
};
SnakeCell {
line: self.line + dl,
col: self.col + dc,
// ctype: self.ctype,
}
}
}
+26
View File
@@ -0,0 +1,26 @@
// use ncurses::*;
use ncurses::{box_, delwin, keypad, newwin, wborder, wrefresh, WINDOW};
pub fn game_window(mlines: i32, mcols: i32, vmargin: i32, hmargin: i32) -> WINDOW {
let game_win: WINDOW;
let (lines, cols): (i32, i32);
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);
box_(game_win, 0, 0);
keypad(game_win, true);
wrefresh(game_win);
game_win
}
pub fn destroy_window(win: WINDOW) {
// wmove(win, 0, 0);
// wclrtobot(win);
wborder(win, 32, 32, 32, 32, 32, 32, 32, 32); // 32 is the ascii code for whitespace. this replaces all the window borders with whitespace
wrefresh(win); // refresh to remove the borders
delwin(win); // delete the window
}
+37
View File
@@ -0,0 +1,37 @@
mod backend;
mod frontend;
use crate::menu;
// use ncurses::*;
use ncurses::{getmaxyx, stdscr, wgetch, wrefresh, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_UP, WINDOW};
use std::thread::sleep;
pub fn start() {
let (mut mlines, mut mcols): (i32, i32) = (0, 0);
let game_win: WINDOW;
let mut ch: i32;
// let mut choice: i8;
getmaxyx(stdscr(), &mut mlines, &mut mcols);
game_win = frontend::game_window(mlines, mcols, 5, 10);
loop {
ch = wgetch(game_win);
match ch {
KEY_UP => (),
KEY_DOWN => (),
KEY_LEFT => (),
KEY_RIGHT => (),
112 => {
match menu::pause_menu_control() {
//112 is keycode for 'p'
0 => (), //resume
1 => (), //restart
2 => break, //exit
_ => (), //other charachters just in case
}
wrefresh(game_win);
}
27 => break,
_ => (),
}
sleep(std::time::Duration::new(0, 10));
}
frontend::destroy_window(game_win);
}