mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2024-11-16 07:47:36 +01:00
Add animated sprite example
This commit is contained in:
parent
7be005de2f
commit
15929e4bb1
2 changed files with 36 additions and 0 deletions
BIN
assets/walking.png
Normal file
BIN
assets/walking.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3 KiB |
36
examples/animated_sprite.cr
Normal file
36
examples/animated_sprite.cr
Normal 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!
|
Loading…
Reference in a new issue