Skip to content

Commit

Permalink
feat(externalized collision logic): brought outside
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskw1999 committed Jun 16, 2024
1 parent 0d8c010 commit 04da240
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 280 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
crossterm = "0.27.0"
ratatui = "0.26.3"
rand = "0.8"
collision_detection = { git = "https://github.com/kriskw1999/collision-detector" }
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

## Preview


https://github.com/kriskw1999/ratatui-snake/assets/71312948/027db3fa-c295-47ad-969d-39018b2cbeca


## Description

This is a simple snake game made designed using Ratatui as TUI library and Rust as programming language.
Expand All @@ -30,7 +28,7 @@ The project is divided in 3 main modules:

- `game`: Contains the game logic
- `main`: Contains the TUI rendering logic and the main loop
- `coord`: contains the `Coord` struct that represents a 2D coordinate
- `collision_detection`: is an external lib that contains the collision detection logic

## Contributing

Expand Down
201 changes: 0 additions & 201 deletions src/collision.rs

This file was deleted.

66 changes: 0 additions & 66 deletions src/coord.rs

This file was deleted.

4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use collision::check_collisions;
use collision_detection::check_collisions;
use crossterm::{
event::{self, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
Expand All @@ -17,8 +17,6 @@ use snake::Snake;
use std::io::{stdout, Result};
use walls::Walls;

mod collision;
mod coord;
mod game;
mod letters;
mod point;
Expand Down
10 changes: 6 additions & 4 deletions src/point.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use collision_detection::{coord::Coord, Collidable};
use ratatui::{
style::Color,
widgets::canvas::{Line, Shape},
};

use crate::{collision::Collidable, coord::Coord};

pub struct Point {
pub coord: Coord,
pub max_x: f64,
Expand All @@ -22,8 +21,11 @@ impl Point {
}
}

pub fn get_random_coord(max_x: f64, max_y: f64) -> Coord {
Coord::randomize(max_x - 1.0, max_y - 1.0)
pub fn get_random_coord(x_max: f64, y_max: f64) -> Coord {
let x = (rand::random::<f64>() * x_max - x_max / 2.0).round();
let y = (rand::random::<f64>() * y_max - y_max / 2.0).round();

Coord::new(x, y)
}

pub fn create_new_point(&mut self) {
Expand Down
3 changes: 2 additions & 1 deletion src/snake.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use collision_detection::{coord::Coord, Collidable};
use ratatui::{
style::Color,
widgets::canvas::{Line, Shape},
};

use crate::{collision::Collidable, coord::Coord, game::Direction};
use crate::game::Direction;

pub struct SnakeHead {
pub coord: Coord,
Expand Down
3 changes: 1 addition & 2 deletions src/walls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use collision_detection::{coord::Coord, Collidable};
use ratatui::{
style::Color,
widgets::canvas::{Line, Shape},
};

use crate::{collision::Collidable, coord::Coord};

pub struct Walls {
pub corners: [Coord; 4],
}
Expand Down

0 comments on commit 04da240

Please sign in to comment.