Skip to content

Commit

Permalink
feat: add pipe pair rect setting
Browse files Browse the repository at this point in the history
  • Loading branch information
suhdonghwi committed May 29, 2021
1 parent 9d81007 commit d5951b5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
Binary file added .DS_Store
Binary file not shown.
14 changes: 12 additions & 2 deletions examples/flappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ggez::timer;
use neat::network::Network;
use neat::{innovation_record::InnovationRecord, network::feedforward::Feedforward, pool::Pool};

use helper::flappy::Bird;
use helper::flappy::{Bird, PipePair};
use helper::{main_layout::MainLayout, plot::Axis};

struct MainState {
Expand All @@ -26,13 +26,15 @@ struct MainState {

birds: Vec<Bird>,
spritebatch: spritebatch::SpriteBatch,

pipes: Vec<PipePair>,
}

impl MainState {
fn reset_birds(&mut self) {
let mut birds = Vec::new();
for _ in 0..self.population {
let bird = Bird::new(na::Point2::new(100.0, 300.0), 0.0, 0.3);
let bird = Bird::new(na::Point2::new(70.0, 300.0), 0.0, 0.3);
birds.push(bird);
}

Expand All @@ -58,6 +60,8 @@ impl MainState {
let bird_image = graphics::Image::new(ctx, "/flappy/bird.png").unwrap();
let batch = spritebatch::SpriteBatch::new(bird_image);

let pipe_image = graphics::Image::new(ctx, "/flappy/pipe.png").unwrap();

let mut state = MainState {
innov_record,
pool,
Expand All @@ -67,6 +71,8 @@ impl MainState {

birds: Vec::new(),
spritebatch: batch,

pipes: vec![PipePair::new(pipe_image, na::Point2::new(100.0, 200.0))],
};

state.reset_birds();
Expand Down Expand Up @@ -136,6 +142,10 @@ impl event::EventHandler for MainState {

graphics::draw(ctx, &self.spritebatch, (na::Point2::new(0.0, 0.0),))?;

for pipe_pair in &self.pipes {
pipe_pair.draw(ctx)?;
}

self.spritebatch.clear();
graphics::present(ctx)
}
Expand Down
44 changes: 42 additions & 2 deletions examples/helper/flappy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl Bird {
}

pub fn jump(&mut self) {
if self.y_velocity >= -5.0 {
self.y_velocity -= 10.0;
if self.y_velocity >= 0.0 {
self.y_velocity = -7.0;
}
}

Expand All @@ -70,3 +70,43 @@ impl Bird {
self.fitness
}
}

pub struct PipePair {
pipe_image: graphics::Image,
upper_rect: graphics::Rect,
lower_rect: graphics::Rect,
}

impl PipePair {
pub fn new(image: graphics::Image, pos: na::Point2<f32>) -> Self {
PipePair {
pipe_image: image,
upper_rect: graphics::Rect::new(pos.x, pos.y - 400.0, 65.0, 400.0),
lower_rect: graphics::Rect::new(pos.x, pos.y + 100.0, 65.0, 400.0),
}
}

pub fn draw(&self, ctx: &mut ggez::Context) -> ggez::GameResult<()> {
/*
let param = graphics::DrawParam::new()
.dest(na::Point2::new(0.0, 0.0))
.scale(na::Vector2::new(1.25, 1.25));
graphics::draw(ctx, &self.pipe_image, param)
*/

let upper = graphics::Mesh::new_rectangle(
ctx,
graphics::DrawMode::fill(),
self.upper_rect,
*opencolor::GRAY5,
)?;
let lower = graphics::Mesh::new_rectangle(
ctx,
graphics::DrawMode::fill(),
self.lower_rect,
*opencolor::GRAY5,
)?;
graphics::draw(ctx, &upper, (na::Point2::new(0.0, 0.0),))?;
graphics::draw(ctx, &lower, (na::Point2::new(0.0, 0.0),))
}
}
2 changes: 1 addition & 1 deletion params/flappy.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
input_number = 1
output_number = 1
population = 100
population = 150

hidden_activation = 'Sigmoid'
output_activation = 'Sigmoid'
Expand Down
Binary file added resources/.DS_Store
Binary file not shown.

0 comments on commit d5951b5

Please sign in to comment.