commit 4cc7dbce92def7cbed5db04ac952f392f9d1f3aa Author: Uttarayan Mondal Date: Tue Jan 5 03:40:51 2021 +0530 Initial commit I think Initial commit with just menu and sort of pause menu only diff --git a/src/game/backend.rs b/src/game/backend.rs new file mode 100644 index 0000000..a37bf4c --- /dev/null +++ b/src/game/backend.rs @@ -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, + } + } +} diff --git a/src/game/frontend.rs b/src/game/frontend.rs new file mode 100644 index 0000000..0897fb1 --- /dev/null +++ b/src/game/frontend.rs @@ -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 +} diff --git a/src/game/mod.rs b/src/game/mod.rs new file mode 100644 index 0000000..13bccfb --- /dev/null +++ b/src/game/mod.rs @@ -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); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a94ca23 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,24 @@ +extern crate ncurses; +mod game; +mod menu; +// use game::{Cell, Snake}; +// use ncurses::*; +use ncurses::{curs_set, endwin, initscr, keypad, noecho, raw, refresh, stdscr, CURSOR_VISIBILITY}; + +fn main() { + // let (lines, cols): (i32, i32) = (0, 0); + initscr(); + raw(); + keypad(stdscr(), true); + curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE); + noecho(); + loop { + match menu::main_menu_control() { + 0 => game::start(), + 1 => game::start(), + _ => break, + } + } + refresh(); + endwin(); +} diff --git a/src/menu.rs b/src/menu.rs new file mode 100644 index 0000000..e49ee24 --- /dev/null +++ b/src/menu.rs @@ -0,0 +1,118 @@ +use ncurses::*; +fn menu_window(mlines: i32, mcols: i32, lines: i32, cols: i32) -> WINDOW { + let menu_win: WINDOW; + let (starty, startx): (i32, i32); + starty = (mlines - lines) / 2; + startx = (mcols - cols) / 2; + menu_win = newwin(lines, cols, starty, startx); + box_(menu_win, 0, 0); // add default border + wrefresh(menu_win); // refreshes the new window to draw it + keypad(menu_win, true); // to use arrow keys + menu_win +} +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 +} +pub fn pause_menu_control() -> i8 { + let mut ch: i32 = 0; + let choice: i8; + let (mut lines, mut cols): (i32, i32) = (0, 0); + let menu_win: WINDOW; + let menu_items = vec!["Resume", "Restart", "Exit"]; + let menu_desc = vec!["Resume the game", "Restart the game", "Exit Game"]; + let mut menu_highlight: usize = 0; + + getmaxyx(stdscr(), &mut lines, &mut cols); + menu_win = menu_window(lines, cols, 10, 20); + loop { + for (menu_item_num, menu_item) in menu_items.iter().enumerate() { + if menu_item_num == menu_highlight { + wattron(menu_win, A_REVERSE()); + } + mvwaddstr(menu_win, menu_item_num as i32 + 1, 1, &menu_item); + wattroff(menu_win, A_REVERSE()); + wrefresh(menu_win); + } + mvwaddstr(menu_win, 7, 1, " "); + mvwaddnstr(menu_win, 7, 1, &menu_desc[menu_highlight], 18); + mvwaddstr(menu_win, 8, 1, &format!("{}", ch)); + ch = wgetch(menu_win); + match ch { + KEY_UP | 107 => { + if menu_highlight > 0 { + menu_highlight -= 1 + } + } + KEY_DOWN | 106 => { + if menu_highlight < 2 { + menu_highlight += 1 + } + } + + 10 => { + choice = menu_highlight as i8; + break; + } + 27 => { + choice = 2; + break; + } + _ => (), + } + } + destroy_window(menu_win); + choice +} +pub fn main_menu_control() -> i8 { + let mut ch: i32 = 0; + let choice: i8; + let (mut lines, mut cols): (i32, i32) = (0, 0); + let menu_win: WINDOW; + let menu_items = vec!["Start", "High Score", "Exit"]; + let menu_desc = vec!["Start the game", "Check Highscrore", "Exit Game"]; + let mut menu_highlight: usize = 0; + + getmaxyx(stdscr(), &mut lines, &mut cols); + menu_win = menu_window(lines, cols, 10, 20); + loop { + for (menu_item_num, menu_item) in menu_items.iter().enumerate() { + if menu_item_num == menu_highlight { + wattron(menu_win, A_REVERSE()); + } + mvwaddstr(menu_win, menu_item_num as i32 + 1, 1, &menu_item); + wattroff(menu_win, A_REVERSE()); + wrefresh(menu_win); + } + mvwaddstr(menu_win, 7, 1, " "); + mvwaddnstr(menu_win, 7, 1, &menu_desc[menu_highlight], 18); + mvwaddstr(menu_win, 8, 1, &format!("{}", ch)); + ch = wgetch(menu_win); + match ch { + KEY_UP | 107 => { + if menu_highlight > 0 { + menu_highlight -= 1 + } + } + KEY_DOWN | 106 => { + if menu_highlight < 2 { + menu_highlight += 1 + } + } + 10 => { + choice = menu_highlight as i8; + break; + } + 27 => { + choice = 2; + break; + } + _ => (), + } + } + destroy_window(menu_win); + choice +}