Skip to content

Commit

Permalink
feat(Game): Added grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinatorul committed Aug 28, 2015
1 parent 66020fa commit 489ba90
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "minesweeper"
version = "0.1.0"
authors = ["Alexander Kuvaev <alexander@kuvaev.me>"]
license-file = "LICENSE"
description = "Simple minesweeper in Rust"
repository = "https://github.com/Vinatorul/minesweeper-rs.git"
readme = "README.md"
Expand Down
31 changes: 23 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ impl Game {
self.draw_field(window);
}

fn get_field_size(&self, window: &PistonWindow) -> [u32; 2] {
[window.size().width, window.size().height]
fn get_max_field_size(&self, window: &PistonWindow) -> [u32; 2] {
[2*window.size().width/3, window.size().height]
}

fn draw_field(&mut self, window: &PistonWindow) {
let field_size = self.get_field_size(window);
let cell_w = (field_size[0] / self.field.get_width()) as f64;
let cell_h = (field_size[1] / self.field.get_height()) as f64;
let max_field_size = self.get_max_field_size(window);
let cell_w = (max_field_size[0] / self.field.get_width()) as f64;
let cell_h = (max_field_size[1] / self.field.get_height()) as f64;
window.draw_2d(|c, g| {
clear([0.0, 0.0, 0.0, 1.0], g);
for i in 0..self.field.get_width() {
Expand Down Expand Up @@ -61,6 +61,17 @@ impl Game {
}
}
}
let field_size = [cell_w*(self.field.get_width() as f64), cell_h*(self.field.get_height() as f64)];
for i in 0..self.field.get_width()+1 {
line::Line::new([0.5, 0.5, 0.5, 1.0], 1.0)
.draw([(i as f64)*cell_w, 0.0, (i as f64)*cell_w, field_size[1]],
&c.draw_state,
c.transform, g);
line::Line::new([0.5, 0.5, 0.5, 1.0], 1.0)
.draw([0.0, (i as f64)*cell_h, field_size[0], (i as f64)*cell_h],
&c.draw_state,
c.transform, g);
}
});
}

Expand All @@ -75,9 +86,13 @@ impl Game {
Button::Mouse(btn) => {
match btn {
MouseButton::Left => {
let field_size = self.get_field_size(window);
let cell_w = field_size[0] / self.field.get_width();
let cell_h = field_size[1] / self.field.get_height();
let max_field_size = self.get_max_field_size(window);
let cell_w = max_field_size[0] / self.field.get_width();
let cell_h = max_field_size[1] / self.field.get_height();
if (self.mouse_x > (cell_w*self.field.get_width()) as f64) ||
(self.mouse_y > (cell_h*self.field.get_height()) as f64) {
return;
}
let x = (self.mouse_x.floor() as u32)/cell_w;
let y = (self.mouse_y.floor() as u32)/cell_h;
let w = self.field.get_width();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
.arg(Arg::from_usage("--height [height] 'window height'"))
.get_matches();

let mut width = 800;
let mut width = 1024;
let mut height = 600;
if let Some(w) = matches.value_of("width") {
width = w.parse().unwrap_or(width);
Expand Down

0 comments on commit 489ba90

Please sign in to comment.