Small Cleanup

This commit is contained in:
Marco Thomas
2020-09-30 09:32:55 +02:00
parent ac5ebc2752
commit f4e3cbb618
2 changed files with 68 additions and 69 deletions

View File

@@ -2,5 +2,7 @@
Yet another snake game \o/ Yet another snake game \o/
Used to learn some rust.
## Dependencies ## Dependencies
Written using the [piston Crate](https://github.com/PistonDevelopers/piston). Written using the [piston Crate](https://github.com/PistonDevelopers/piston).

View File

@@ -1,27 +1,80 @@
extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
use piston::window::WindowSettings; use piston::window::WindowSettings;
use piston::input::*; use piston::input::*;
use piston::event_loop::*; use piston::event_loop::*;
use glutin_window::GlutinWindow; use glutin_window::GlutinWindow;
use opengl_graphics::{GlGraphics, OpenGL}; use opengl_graphics::{GlGraphics, OpenGL};
const PIXEL_SIZE: i32 = 20; pub const PIXEL_SIZE: i32 = 20;
const FIELD_SIZE_X: i32 = 30; pub const FIELD_SIZE_X: i32 = 30;
const FIELD_SIZE_Y: i32 = 30; pub const FIELD_SIZE_Y: i32 = 30;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
enum Direction { pub enum Direction {
Up, Up,
Down, Down,
Left, Left,
Right, Right,
} }
struct Game { pub struct Snake {
pos_x: i32,
pos_y: i32,
direction: Direction,
}
impl Snake {
pub fn render(&self, gl: &mut GlGraphics, args: &RenderArgs) {
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
let square = graphics::rectangle::square(
(self.pos_x * PIXEL_SIZE) as f64,
(self.pos_y * PIXEL_SIZE) as f64,
PIXEL_SIZE as f64);
gl.draw(args.viewport(), |c, gl| {
let transform = c.transform;
graphics::rectangle(GREEN, square, transform, gl);
});
}
pub fn update(&mut self) {
match self.direction {
Direction::Up => self.pos_y -=1,
Direction::Down => self.pos_y +=1,
Direction::Left => self.pos_x -=1,
Direction::Right => self.pos_x +=1,
}
if self.pos_x > FIELD_SIZE_X {
self.pos_x = 0;
} else if self.pos_x < 0 {
self.pos_x = FIELD_SIZE_X;
}
if self.pos_y > FIELD_SIZE_Y {
self.pos_y = 0;
} else if self.pos_y < 0 {
self.pos_y = FIELD_SIZE_Y;
}
}
pub fn change_direction(&mut self, button: &Button) {
let last_direction = self.direction.clone();
self.direction = match button {
Button::Keyboard(Key::W) => Direction::Up,
Button::Keyboard(Key::S) => Direction::Down,
Button::Keyboard(Key::A) => Direction::Left,
Button::Keyboard(Key::D) => Direction::Right,
_ => last_direction,
}
}
}
pub struct Game {
gl: GlGraphics, gl: GlGraphics,
snake: Snake, snake: Snake,
food: Food, food: Food,
@@ -50,64 +103,6 @@ impl Game {
} }
} }
struct Snake {
pos_x: i32,
pos_y: i32,
direction: Direction,
}
impl Snake {
fn render(&self, gl: &mut GlGraphics, args: &RenderArgs) {
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
let square = graphics::rectangle::square(
(self.pos_x * PIXEL_SIZE) as f64,
(self.pos_y * PIXEL_SIZE) as f64,
PIXEL_SIZE as f64);
gl.draw(args.viewport(), |c, gl| {
let transform = c.transform;
graphics::rectangle(GREEN, square, transform, gl);
});
}
fn update(&mut self) {
match self.direction {
Direction::Up => self.pos_y -=1,
Direction::Down => self.pos_y +=1,
Direction::Left => self.pos_x -=1,
Direction::Right => self.pos_x +=1,
}
if self.pos_x > FIELD_SIZE_X {
self.pos_x = 0;
} else if self.pos_x < 0 {
self.pos_x = FIELD_SIZE_X;
}
if self.pos_y > FIELD_SIZE_Y {
self.pos_y = 0;
} else if self.pos_y < 0 {
self.pos_y = FIELD_SIZE_Y;
}
}
fn change_direction(&mut self, button: &Button) {
let last_direction = self.direction.clone();
self.direction = match button {
Button::Keyboard(Key::W) => Direction::Up,
Button::Keyboard(Key::S) => Direction::Down,
Button::Keyboard(Key::A) => Direction::Left,
Button::Keyboard(Key::D) => Direction::Right,
_ => last_direction,
}
}
}
struct Food { struct Food {
pos_x: i32, pos_x: i32,
pos_y: i32, pos_y: i32,
@@ -115,6 +110,7 @@ struct Food {
impl Food { impl Food {
fn new() -> Food { fn new() -> Food {
// TODO: Not random yet
let random_x: i32 = 3; let random_x: i32 = 3;
let random_y: i32 = 3; let random_y: i32 = 3;
@@ -138,6 +134,7 @@ impl Food {
} }
} }
fn main() { fn main() {
let opengl = OpenGL::V3_2; let opengl = OpenGL::V3_2;
@@ -163,7 +160,7 @@ fn main() {
let mut events = Events::new(EventSettings::new()) let mut events = Events::new(EventSettings::new())
.max_fps(60) .max_fps(60)
.ups(10); .ups(15);
while let Some(e) = events.next(&mut window) { while let Some(e) = events.next(&mut window) {
if let Some(r) = e.render_args() { if let Some(r) = e.render_args() {