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

added restart, pause and quit keybinds #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ pub fn draw_snake_head(ctx: &Context, g: &mut G2d, c: Color, pos: &Position, dir
}
}

pub fn draw_fruit(ctx: &Context, g: &mut G2d, c: Color, pos: &Position) {}

// Makes the screen red after the game is over
pub fn draw_overlay(ctx: &Context, g: &mut G2d, c: Color, size: (u32, u32)) {
rectangle(
c,
Expand Down
32 changes: 7 additions & 25 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,13 @@ impl Game {
self.paused = false;
}

pub fn pause(&mut self) {
self.paused = true;
pub fn toggle_game_state(&mut self) {
self.paused = !self.paused;
}

// pub fn toggle_game_state(&mut self) {
// if self.paused {
// self.start();
// } else {
// self.pause();
// }
// }

pub fn draw(&self, ctx: Context, g: &mut G2d) {
draw_block(&ctx, g, colors::FRUIT, &self.fruit);
self.snake.draw(&ctx, g);
// draw_text(&ctx, g, colors::SCORE, self.score.to_string());

if self.over {
draw_overlay(&ctx, g, colors::OVERLAY, self.size)
Expand All @@ -75,13 +66,6 @@ impl Game {
pub fn update(&mut self, delta_time: f64) {
self.waiting_time += delta_time;

// if self.over {
// if self.waiting_time > RESTART_TIME {
// self.restart();
// }
// return;
// }

if self.waiting_time > fps_in_ms(FPS) && !self.over && !self.paused {
// self.check_colision() use snake.get_head_pos;
self.waiting_time = 0.0;
Expand All @@ -104,17 +88,15 @@ impl Game {
pub fn key_down(&mut self, key: keyboard::Key) {
use keyboard::Key;

// match key {
// Key::R => self.over = false, // temp solution -> replace current game state trough new one
// Key::Space => self.toggle_game_state(),
// _ => self.start(),
// }

match key {
Key::A | Key::Left => self.snake.set_dir(Direction::Left),
Key::W | Key::Up => self.snake.set_dir(Direction::Up),
Key::D | Key::Right => self.snake.set_dir(Direction::Right),
Key::S | Key::Down => self.snake.set_dir(Direction::Down),
Key::R => *self = Game::new(self.size.0, self.size.1), // restart
Key::Escape => self.toggle_game_state(),
Key::Space => self.paused = false,
Key::Q => std::process::exit(0),
_ => {}
}
}
Expand All @@ -124,7 +106,7 @@ impl Game {
}

fn calc_score(&mut self) {
self.score = (self.snake.get_len() * 10) as u32
self.score = (self.snake.get_len()) as u32
}

// IMPORTANT!! -
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() {
.for_folder("assets")
.unwrap();
let ref font = assets.join("retro-gaming.ttf");
let factory = window.factory.clone();
let _factory = window.factory.clone();
let mut glyphs = Glyphs::new(font, TextureContext {
factory: window.factory.clone(),
encoder: window.factory.create_command_buffer().into(),
Expand All @@ -55,17 +55,21 @@ fn main() {

window.draw_2d(&event, |ctx, g, _| {
clear(colors::BACKGROUND, g);

// Render the current score in the top-left corner
text::Text::new_color(colors::SCORE, 20)
.draw(
main.get_score().to_string().as_ref(),
format!("{}", main.get_score()).as_str(),
&mut glyphs,
&ctx.draw_state,
ctx.transform.trans(0.0, 20.0),
ctx.transform.trans(10.0, 30.0),
g,
)
.unwrap();

main.draw(ctx, g);
});


event.update(|arg| {
main.update(arg.dt);
Expand Down
9 changes: 0 additions & 9 deletions src/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ impl Snake {
}
}

// pub fn dir(&self) -> Direction {
// self.direction
// }

// pub fn r#move(&mut self) {}

// pub fn grow(&mut self, x: u32, y: u32) {
// self.tail.push_back(Position { x, y })
// }
pub fn update(&mut self, width: u32, height: u32) {
if self.tail.len() > 0 {
self.tail.push_front(self.head.clone());
Expand Down