Add animated sprite example

This commit is contained in:
Alex Clink 2022-01-24 22:05:04 -05:00
parent 7be005de2f
commit 15929e4bb1
2 changed files with 36 additions and 0 deletions

BIN
assets/walking.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 KiB

View file

@ -0,0 +1,36 @@
require "../src/game"
require "../src/sprite"
require "../src/pixel_text"
module PF
class SpriteExample < Game
@text : PixelText = PixelText.new("assets/pf-font.png")
@tiles : Array(Sprite)
@frame = 0
@sub_frame = 0.0
@frame_time = 0.1
def initialize(*args, **kwargs)
super
@tiles = Sprite.load_tiles("assets/walking.png", 32, 64)
end
def update(dt, event)
@sub_frame += dt
if @sub_frame > @frame_time
@sub_frame = @sub_frame % @frame_time
@frame = (@frame + 1) % @tiles.size
end
end
def draw
clear(60, 120, 200)
@text.draw_to(screen, "Frame: #{@frame}", 5, 5)
@tiles[@frame].draw_to(screen, (viewport // 2) - @tiles[@frame].size // 2)
end
end
end
game = PF::SpriteExample.new(120, 80, 5)
game.run!