Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example of animation using the keyboard #110

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}