Small Cleanup
This commit is contained in:
@@ -2,5 +2,7 @@
|
||||
|
||||
Yet another snake game \o/
|
||||
|
||||
Used to learn some rust.
|
||||
|
||||
## Dependencies
|
||||
Written using the [piston Crate](https://github.com/PistonDevelopers/piston).
|
||||
|
||||
135
src/main.rs
135
src/main.rs
@@ -1,27 +1,80 @@
|
||||
extern crate glutin_window;
|
||||
extern crate graphics;
|
||||
extern crate opengl_graphics;
|
||||
extern crate piston;
|
||||
|
||||
use piston::window::WindowSettings;
|
||||
use piston::input::*;
|
||||
use piston::event_loop::*;
|
||||
use glutin_window::GlutinWindow;
|
||||
use opengl_graphics::{GlGraphics, OpenGL};
|
||||
|
||||
const PIXEL_SIZE: i32 = 20;
|
||||
const FIELD_SIZE_X: i32 = 30;
|
||||
const FIELD_SIZE_Y: i32 = 30;
|
||||
pub const PIXEL_SIZE: i32 = 20;
|
||||
pub const FIELD_SIZE_X: i32 = 30;
|
||||
pub const FIELD_SIZE_Y: i32 = 30;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum Direction {
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
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,
|
||||
snake: Snake,
|
||||
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 {
|
||||
pos_x: i32,
|
||||
pos_y: i32,
|
||||
@@ -115,6 +110,7 @@ struct Food {
|
||||
|
||||
impl Food {
|
||||
fn new() -> Food {
|
||||
// TODO: Not random yet
|
||||
let random_x: i32 = 3;
|
||||
let random_y: i32 = 3;
|
||||
|
||||
@@ -138,6 +134,7 @@ impl Food {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let opengl = OpenGL::V3_2;
|
||||
|
||||
@@ -163,7 +160,7 @@ fn main() {
|
||||
|
||||
let mut events = Events::new(EventSettings::new())
|
||||
.max_fps(60)
|
||||
.ups(10);
|
||||
.ups(15);
|
||||
while let Some(e) = events.next(&mut window) {
|
||||
|
||||
if let Some(r) = e.render_args() {
|
||||
|
||||
Reference in New Issue
Block a user