pixelfaucet/examples/text.cr

54 lines
1.1 KiB
Crystal
Raw Normal View History

2021-12-12 06:10:52 +01:00
require "../src/game"
require "../src/pixel_text"
class TextGame < PF::Game
@text : PF::PixelText
2022-01-03 02:41:17 +01:00
def initialize(*args, **kwargs)
2021-12-12 06:10:52 +01:00
super
@text = PF::PixelText.new("assets/pf-font.png")
@text.color(PF::Pixel.new(255, 255, 255))
@x = 0.0
@y = 0.0
@dx = 50.0
@dy = 50.0
@msg = "Hello, World!"
end
2022-01-03 01:39:17 +01:00
def update(dt, event)
2021-12-12 06:10:52 +01:00
@x += @dx * dt
@y += @dy * dt
if @x < 0
@x = 0
@dx = -@dx
2022-01-03 02:41:17 +01:00
@text.color(PF::Pixel.random)
2021-12-12 06:10:52 +01:00
end
2022-01-03 02:41:17 +01:00
if @x > @width - (@msg.size * @text.char_width)
@x = @width - (@msg.size * @text.char_width)
2021-12-12 06:10:52 +01:00
@dx = -@dx
2022-01-03 02:41:17 +01:00
@text.color(PF::Pixel.random)
2021-12-12 06:10:52 +01:00
end
if @y < 0
@y = 0
@dy = -@dy
2022-01-03 02:41:17 +01:00
@text.color(PF::Pixel.random)
2021-12-12 06:10:52 +01:00
end
2022-01-03 02:41:17 +01:00
if @y > @height - (@text.char_height)
@y = @height - (@text.char_height)
2021-12-12 06:10:52 +01:00
@dy = -@dy
2022-01-03 02:41:17 +01:00
@text.color(PF::Pixel.random)
2021-12-12 06:10:52 +01:00
end
end
def draw
clear(0, 0, 50)
@text.draw(@screen, @msg, @x.to_i, @y.to_i)
end
end
2022-01-03 02:41:17 +01:00
engine = TextGame.new(160, 100, 4, flags: SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC).run!