Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/invaders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ cargo run --release --package invaders

<kbd>Pause</kbd> <kbd>P</kbd>: Pause

<kbd>R</kbd>: Reset Game

<kbd>escape</kbd>: Quit

## GamePad Controls

`D-Pad 🡰` `D-Pad 🡲`: Move tank
Expand Down
63 changes: 46 additions & 17 deletions examples/invaders/simple-invaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,43 @@ impl World {
});
}
}

pub fn reset_game(&mut self) {
// Recreate the alien
self.invaders = Invaders {
grid: make_invader_grid(&self.assets),
stepper: Point::new(COLS - 1, 0),
direction: Direction::Right,
descend: false,
bounds: Bounds::default(),
};

// Empty laser
self.lasers.clear();

// Recreate the shield
self.shields = (0..4)
.map(|i| Shield {
sprite: Sprite::new(&self.assets, Frame::Shield1),
pos: Point::new(i * 45 + 32, 192),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move these magic numbers to const values at global (module) scope. If they need to be reused in multiple modules, we can create a crate::consts module for them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

})
.collect();

// Reset player position
self.player.pos = PLAYER_START;

// Remove bullet
self.bullet = None;

// Reset collision state
self.collision.clear();

// Reset game score
self._score = 0;

// Set gameover to false
self.gameover = false;
}
}

/// Create a default `World` with a static PRNG seed.
Expand Down Expand Up @@ -501,23 +538,15 @@ impl Invaders {

// Scan through the entire grid
for (y, row) in self.grid.iter().enumerate() {
for (x, col) in row.iter().enumerate() {
if col.is_some() {
// Build a boundary box of invaders in the grid
if top > y {
top = y;
}
if bottom < y {
bottom = y;
}
if left > x {
left = x;
}
if right < x {
right = x;
}
}
}
row.iter()
.enumerate()
.filter(|(_, col)| col.is_some())
.for_each(|(x, _)| {
top = top.min(y);
bottom = bottom.max(y);
left = left.min(x);
right = right.max(x);
});
}

if top > bottom || left > right {
Expand Down
9 changes: 9 additions & 0 deletions examples/invaders/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ impl Game {
Controls { direction, fire }
};
}

fn reset_game(&mut self) {
self.world.reset_game();
}
}

fn main() -> Result<(), Error> {
Expand Down Expand Up @@ -165,6 +169,11 @@ fn main() -> Result<(), Error> {
return;
}

// Reset game
if g.game.input.key_pressed(VirtualKeyCode::R) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a binding for controller inputs as well. Maybe the "select" button? I don't actually know what it's called any more on XBox/PS/Switch!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the "select" button? I don't know!LOL ; )

But I believe that if add the selected operation interface, this problem will be solved!

g.game.reset_game();
}

// Resize the window
if let Some(size) = g.game.input.window_resized() {
if let Err(err) = g.game.pixels.resize_surface(size.width, size.height) {
Expand Down