pixelfaucet/examples/text.cr

51 lines
939 B
Crystal
Raw Permalink Normal View History

2021-12-12 06:10:52 +01:00
require "../src/game"
class TextGame < PF::Game
2022-01-03 02:41:17 +01:00
def initialize(*args, **kwargs)
2021-12-12 06:10:52 +01:00
super
@x = 0.0
@y = 0.0
@dx = 50.0
@dy = 50.0
@msg = "Hello, World!"
2022-02-04 06:09:09 +01:00
@color = PF::Pixel.random
@font = Pixelfont::Font.new("#{__DIR__}/../lib/pixelfont/fonts/pixel-5x7.txt")
2021-12-12 06:10:52 +01:00
end
def update(dt)
2021-12-12 06:10:52 +01:00
@x += @dx * dt
@y += @dy * dt
if @x < 0
@x = 0
@dx = -@dx
2022-02-04 06:09:09 +01:00
@color = PF::Pixel.random
2021-12-12 06:10:52 +01:00
end
if @x > width - @font.width_of(@msg)
@x = width - @font.width_of(@msg)
2021-12-12 06:10:52 +01:00
@dx = -@dx
2022-02-04 06:09:09 +01:00
@color = PF::Pixel.random
2021-12-12 06:10:52 +01:00
end
if @y < 0
@y = 0
@dy = -@dy
2022-02-04 06:09:09 +01:00
@color = PF::Pixel.random
2021-12-12 06:10:52 +01:00
end
if @y > height - @font.line_height
@y = height - @font.line_height
2021-12-12 06:10:52 +01:00
@dy = -@dy
2022-02-04 06:09:09 +01:00
@color = PF::Pixel.random
2021-12-12 06:10:52 +01:00
end
end
def draw
clear(0, 0, 50)
draw_string(@msg, @x.to_i, @y.to_i, @font, @color)
2021-12-12 06:10:52 +01:00
end
end
2022-02-04 06:09:09 +01:00
engine = TextGame.new(160, 100, 4).run!