Skip to content

Commit

Permalink
Merge pull request #110 from mgocobachi/master
Browse files Browse the repository at this point in the history
Added an example of animation using the keyboard
  • Loading branch information
17cupsofcoffee authored Mar 20, 2019
2 parents 9d73de1 + 92e3412 commit e9cccbd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ don't break when rendered by mdbook.
| [`canvas`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/render_target.rs) | Graphics | Uses a custom render target to apply post-processing effects. |
| [`audio`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/audi.rs) | Audio | Plays back an audio file. |
| [`keyboard`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/keyboard.rs) | Input | Moves a texture around based on keyboard input. |
| [`keyboard_animation`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/keyboard_animation.rs) | Input | Moves a sprite with its animation around based on keyboard input. |
| [`mouse`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/mouse.rs) | Input | Moves a texture around based on mouse input. |
| [`gamepad`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/gamepad.rs) | Input | Displays the input from a connected gamepad. |
| [`text_input`](https://github.com/17cupsofcoffee/tetra/blob/master/examples/text_input.rs) | Input | Displays text as it is typed in by the player. |
Expand Down
85 changes: 85 additions & 0 deletions examples/keyboard_animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use tetra::graphics::{self, Color, DrawParams, Vec2, Texture, Animation, Rectangle};
use tetra::input::{self, Key};
use tetra::{Context, ContextBuilder, State};

struct GameState {
animation: Animation,
position: Vec2,
}

impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
Ok(GameState {
animation: Animation::new(
Texture::new(ctx, "./examples/resources/tiles.png")?,
Rectangle::row(0.0, 256.0, 16.0, 16.0).take(8).collect(),
5,
),
position: Vec2::new(240.0, 160.0),
})
}

pub fn player_idle(&mut self) {
self.animation.set_frames(Rectangle::row(0.0, 256.0, 16.0, 16.0).take(8).collect());
}

pub fn player_walk(&mut self) {
self.animation.set_frames(Rectangle::row(0.0, 272.0, 16.0, 16.0).take(8).collect());
}
}

impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
self.animation.tick();

if input::is_key_down(ctx, Key::A) {
self.position.x -= 1.5;
}

if input::is_key_pressed(ctx, Key::A) {
self.player_walk();
}

if input::is_key_released(ctx, Key::A) {
self.player_idle();
}

if input::is_key_down(ctx, Key::D) {
self.position.x += 1.5;
}

if input::is_key_pressed(ctx, Key::D) {
self.player_walk();
}

if input::is_key_released(ctx, Key::D) {
self.player_idle();
}

Ok(())
}

fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.769, 0.812, 0.631));

graphics::draw(
ctx,
&self.animation,
DrawParams::new()
.position(self.position)
.origin(Vec2::new(8.0, 8.0))
.scale(Vec2::new(2.0, 2.0)),
);

Ok(())
}
}

fn main() -> tetra::Result {
ContextBuilder::new("Keyboard Input with animation", 480, 320)
.maximized(false)
.resizable(false)
.quit_on_escape(true)
.build()?
.run_with(GameState::new)
}

0 comments on commit e9cccbd

Please sign in to comment.