Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable speed on long press #4

Merged
merged 8 commits into from
Oct 4, 2023
42 changes: 27 additions & 15 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ncurses::{mvprintw, attroff, attron, A_BOLD, COLOR_PAIR, addstr };
use crate::snake::Snake;
use crate::utils::{Direction, Position};
use ncurses::{addstr, attroff, attron, mvprintw, A_BOLD, COLOR_PAIR};
use rand::Rng;
use crate::snake::{Snake};
use crate::utils::{ Position, Direction };

pub struct Game {
username: String,
Expand All @@ -19,22 +19,22 @@ struct Food {
impl Food {
fn new(value: i32) -> Self {
Food {
value: value,
value,
icon: "$".to_string(),
pos: Position::new(12, 10),
}
}

fn display(&self) -> () {
fn display(&self) {
attron(COLOR_PAIR(1) | A_BOLD());
mvprintw(self.pos.posy, self.pos.posx, self.icon.as_str());
attroff(COLOR_PAIR(1) | A_BOLD());
}

fn relocate(&mut self, width: i32, height: i32) -> () {
fn relocate(&mut self, width: i32, height: i32) {
let mut rng = rand::thread_rng();
self.pos.posy = rng.gen_range(1..=height-2);
self.pos.posx = rng.gen_range(1..=width-2);
self.pos.posy = rng.gen_range(1..=height - 2);
self.pos.posx = rng.gen_range(1..=width - 2);
}
}

Expand All @@ -53,7 +53,7 @@ impl Game {
}
}

pub fn update_score(&mut self, width: i32, height: i32) -> () {
pub fn update_score(&mut self, width: i32, height: i32) {
if let Some(head) = self.snake.snake.front_mut() {
if head.pos == self.food.pos {
self.score += self.food.value;
Expand All @@ -67,28 +67,40 @@ impl Game {
match ch {
119 | 259 => match self.snake.direction {
Direction::BOTTOM => {}
_ => self.snake.direction = Direction::TOP,
_ => {
self.snake.direction = Direction::TOP;
self.snake.speed = 4;
}
}, // w
97 | 260 => match self.snake.direction {
Direction::RIGHT => {}
_ => self.snake.direction = Direction::LEFT,
_ => {
self.snake.direction = Direction::LEFT;
self.snake.speed = 4;
}
}, // A
115 | 258 => match self.snake.direction {
Direction::TOP => {}
_ => self.snake.direction = Direction::BOTTOM,
_ => {
self.snake.direction = Direction::BOTTOM;
self.snake.speed = 4;
}
}, //s
100 | 261 => match self.snake.direction {
Direction::LEFT => {}
_ => self.snake.direction = Direction::RIGHT,
_ => {
self.snake.direction = Direction::RIGHT;
self.snake.speed = 4;
}
}, // D
_ => {}
}
}

pub fn display(&mut self) -> () {
pub fn display(&mut self) {
let data = format!("USER : {} | SCORE : {}", self.username, self.score);
addstr(&data);
self.snake.display();
self.food.display();
}
}
}
60 changes: 37 additions & 23 deletions src/game_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub struct GameWindow {
pub window_height: i32,
}

impl Default for GameWindow {
fn default() -> Self {
Self::new()
}
}

impl GameWindow {
pub fn new() -> Self {
initscr();
Expand Down Expand Up @@ -38,7 +44,13 @@ impl GameWindow {
endwin();
}

pub fn pause_menu(&mut self, height: i32, width: i32, title: String, is_gameover : bool) -> Action {
pub fn pause_menu(
&mut self,
height: i32,
width: i32,
title: String,
is_gameover: bool,
) -> Action {
let mut component: Component =
Component::new(height, width, self.window_height, self.window_width);

Expand All @@ -62,14 +74,15 @@ impl GameWindow {
napms(100);
match getch() {
ERR => {}
27 => {
27 => {
component.del();
return Action::QUIT
return Action::QUIT;
}
x => {
if let Err(n) = component.handle_input(x) {
return n;
}
}
x => match component.handle_input(x) {
Err(n) => return n,
_ => {}
},
}
}
}
Expand All @@ -92,20 +105,21 @@ impl GameWindow {
napms(100);
match getch() {
ERR => {}
27 => {
27 => {
component.del();
return Action::QUIT
return Action::QUIT;
}
x => {
if let Err(n) = component.handle_input(x) {
return n;
}
}
x => match component.handle_input(x) {
Err(n) => return n,
_ => {}
},
}
}
}

pub fn get_name(&mut self, height : i32, width: i32) -> String{
let title: String = String::from("You Name?");
pub fn get_name(&mut self, height: i32, width: i32) -> String {
let title: String = String::from("You Name?");
let mut component: Component =
Component::new(height, width, self.window_height, self.window_width);

Expand All @@ -118,16 +132,18 @@ impl GameWindow {
component.display();
component.refresh();
napms(100);
match getch(){
ERR => {},
match getch() {
ERR => {}
10 | 27 => break,
c => {
if c == 263 && name.len() > 0 {
if c == 263 && !name.is_empty() {
name.truncate(name.len() - 1);
}
else if ( c >= 48 && c <= 57) || ( c >= 65 && c <= 90 ) || ( c >= 97 && c <= 122 ){
} else if (48..=57).contains(&c)
|| (65..=90).contains(&c)
|| (97..=122).contains(&c)
{
let ch = char::from_u32(c as u32).unwrap();
name = format!("{}{}",name, ch );
name = format!("{}{}", name, ch);
}
}
};
Expand All @@ -136,6 +152,4 @@ impl GameWindow {
component.del();
name
}

}

27 changes: 14 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use ncurses::*;
use viper::game::Game;
use viper::game_window::{*};
use viper::game_window::*;
use viper::window_component::Action;

fn main() {
timeout(0);

let mut window = GameWindow::new();
'new_game: loop {
match window.start_menu(6, 30 ){
Action::QUIT => break 'new_game,
_ => {}
if let Action::QUIT = window.start_menu(6, 30) {
break 'new_game;
}
let name: String = window.get_name(6, 30);
let mut game: Game = Game::new(
name,
window.window_width,
window.window_height,
);
let mut game: Game = Game::new(name, window.window_width, window.window_height);
'curr_game: loop {
let mut is_gameover : bool = false;
let mut is_gameover: bool = false;
'game: loop {
clear();
game.display();
Expand All @@ -31,21 +28,25 @@ fn main() {
break 'game;
}
};
napms(100);
match getch() {
ERR => {}
ERR => {
game.snake.speed = 1;
}
27 => break 'game,
n => game.control_snake(n),
}
let speed = 100 / game.snake.speed;
napms(speed);
}
let msg: String = format!("Score : {}", game.score);
match window.pause_menu(6, 30, msg, is_gameover) {
Action::QUIT => break 'new_game,
Action::RESTART => break 'curr_game,
Action::RESUME => {},
Action::RESUME => {}
_ => {}
}
}
}
window.exit();
}

31 changes: 16 additions & 15 deletions src/snake.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ncurses::{attroff, attron, mvprintw, A_BOLD};
use std::collections::HashSet;
use std::collections::VecDeque;
use ncurses::{mvprintw, attroff, attron, A_BOLD };

use crate::utils::{Position, Direction};
use crate::utils::{Direction, Position};

#[derive(Clone, Copy, Debug)]
enum Part {
Expand All @@ -20,6 +20,7 @@ pub struct SnakeBodyPart {
pub struct Snake {
pub snake: VecDeque<SnakeBodyPart>,
pub direction: Direction,
pub speed: i32,
texture: (String, String, String),
snake_hash: HashSet<i32>,
}
Expand All @@ -28,22 +29,20 @@ impl SnakeBodyPart {
fn new(position: Position, part: Part) -> Self {
SnakeBodyPart {
pos: position,
part: part,
part,
}
}

fn display(&self, texture: (String, String, String)) -> () {
let ch: String;
match self.part {
Part::HEAD => ch = texture.0.clone(),
Part::BODY => ch = texture.1.clone(),
Part::TAIL => ch = texture.2.clone(),
}
fn display(&self, texture: (String, String, String)) {
let ch: String = match self.part {
Part::HEAD => texture.0.clone(),
Part::BODY => texture.1.clone(),
Part::TAIL => texture.2.clone(),
};
mvprintw(self.pos.posy, self.pos.posx, ch.as_str());
}
}


impl Snake {
pub fn new(position: Position, texture: (String, String, String)) -> Self {
let p1 = SnakeBodyPart::new(position, Part::HEAD);
Expand All @@ -52,10 +51,11 @@ impl Snake {
let hash_set: HashSet<i32> = HashSet::from([p1.pos.hash(), p2.pos.hash(), p3.pos.hash()]);
let snake: VecDeque<SnakeBodyPart> = VecDeque::from([p1, p2, p3]);
Snake {
snake: snake,
snake,
direction: Direction::RIGHT,
texture: texture,
texture,
snake_hash: hash_set,
speed: 1,
}
}

Expand All @@ -73,10 +73,11 @@ impl Snake {
}
self.snake.push_front(p);
}

Ok(())
}

pub fn extend_back(&mut self) -> () {
pub fn extend_back(&mut self) {
if let Some(curr_tail) = self.snake.back_mut() {
curr_tail.part = Part::BODY;
let p: SnakeBodyPart =
Expand Down Expand Up @@ -106,7 +107,7 @@ impl Snake {
Ok(())
}

pub fn display(&self) -> () {
pub fn display(&self) {
attron(A_BOLD());
for part in self.snake.iter() {
part.display(self.texture.clone());
Expand Down
Loading