pixelfaucet/examples/text.cr

51 lines
915 B
Crystal
Raw 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
# @msg = "HI"
@color = PF::Pixel.random
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 - (@msg.size * PF::Sprite::CHAR_WIDTH)
@x = width - (@msg.size * PF::Sprite::CHAR_WIDTH)
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 - (PF::Sprite::CHAR_HEIGHT)
@y = height - (PF::Sprite::CHAR_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)
2022-02-04 06:09:09 +01:00
draw_string(@msg, @x.to_i, @y.to_i, @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!