snake struct created

This commit is contained in:
JasterV 2020-11-04 00:50:39 +01:00
commit 46847365b1
6 changed files with 1449 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1308
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

15
Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "square"
version = "0.1.0"
authors = ["JasterV <victorcoder2@gmail.com>"]
edition = "2018"
[lib]
name="lib"
src = "src/lib.rs"
[dependencies]
piston = "0.52.0"
piston2d-graphics = "0.37.0"
pistoncore-glutin_window = "0.67.0"
piston2d-opengl_graphics = "0.74.0"

3
src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
mod snake;
pub use snake::{Snake, Direction};

62
src/main.rs Normal file
View file

@ -0,0 +1,62 @@
extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
// WINDOW CONTROLLER
use glutin_window::GlutinWindow;
// SHAPES, COLORS AND ALL THAT STUFF
use opengl_graphics::{GlGraphics, OpenGL};
// EVENTS, WE NEED A WINDOW CONTROLLER FOR THAT
use piston::event_loop::{EventSettings, Events};
// EVENT ARGUMENTS FOR RENDERING, DONT WORRY JUST USE IT
use piston::input::{
Button, ButtonEvent, ButtonState, RenderArgs, RenderEvent, UpdateArgs, UpdateEvent,
};
// WINDOW SETTINGS, TO CREATE THE WINDOW CONTROLLER
use piston::window::WindowSettings;
use lib::{Direction, Snake};
struct Game {
gl: GlGraphics,
snake: Snake,
}
impl Game {
fn render(&mut self, args: &RenderArgs) {}
fn update(&mut self, args: &UpdateArgs) {}
fn key_press(&mut self, key: Button) {}
}
fn main() {
let opengl = OpenGL::V3_1;
let mut window: GlutinWindow = WindowSettings::new("Snake Game", [400, 400])
.fullscreen(true)
.exit_on_esc(true)
.resizable(false)
.build()
.expect("Error building the window");
let mut game = Game {
gl: GlGraphics::new(opengl),
snake: Snake::new(0.0, 0.0),
};
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(arg) = e.render_args() {}
if let Some(arg) = e.update_args() {}
if let Some(arg) = e.button_args() {
if let ButtonState::Press = arg.state {
game.key_press(arg.button);
}
}
}
}

60
src/snake/mod.rs Normal file
View file

@ -0,0 +1,60 @@
use std::collections::LinkedList;
use std::iter::FromIterator;
#[derive(PartialEq, Clone)]
pub enum Direction {
Up,
Left,
Right,
Down,
}
pub struct Snake {
pub body: LinkedList<(f64, f64)>,
direction: Direction,
}
impl Snake {
pub const VELOCITY: f64 = 8.0;
pub const SIZE: f64 = 20.0;
pub fn new(x: f64, y: f64) -> Self {
let body: LinkedList<(f64, f64)> = LinkedList::from_iter(vec![(x, y)].into_iter());
Snake {
body,
direction: Direction::Right,
}
}
pub fn rotate(&mut self, direction: Direction) {
match direction {
Direction::Up if self.direction != Direction::Down => self.direction = direction,
Direction::Down if self.direction != Direction::Up => self.direction = direction,
Direction::Left if self.direction != Direction::Right => self.direction = direction,
Direction::Right if self.direction != Direction::Left => self.direction = direction,
_ => ()
}
}
pub fn move_forwards(&mut self) {
let mut head: (f64, f64) = self
.body
.front()
.expect("Cannot move an empty snake")
.clone();
match self.direction {
Direction::Up => head.1 -= Snake::VELOCITY,
Direction::Down => head.1 += Snake::VELOCITY,
Direction::Left => head.0 -= Snake::VELOCITY,
Direction::Right => head.0 += Snake::VELOCITY,
}
self.body.pop_back();
self.body.push_front(head);
}
pub fn add_square(&mut self) {
let back = self.body.back().unwrap();
let new_part = (back.0, back.1);
self.body.push_back(new_part);
}
}