pixelfaucet/examples/triangle.cr

82 lines
2 KiB
Crystal
Raw Normal View History

2021-11-29 08:26:51 +01:00
# require "crystaledge"
require "../src/game"
require "../src/controller"
require "../src/sprite"
require "../src/sprite/vector_sprite"
require "../src/pixel"
2021-11-29 08:26:51 +01:00
class Triangle < PF::Sprite
include PF::VectorSprite
def update(dt)
end
def draw(engine)
frame = project_points(@frame)
2021-12-01 05:05:07 +01:00
engine.fill_triangle(frame[0], frame[1], frame[2], PF::Pixel.yellow)
2021-11-29 08:26:51 +01:00
end
end
class TriangleThing < PF::Game
@tri : Triangle
2021-12-06 04:02:11 +01:00
@paused = true
@controller : PF::Controller(LibSDL::Scancode)
2021-11-29 08:26:51 +01:00
def initialize(@width, @height, @scale)
super(@width, @height, @scale)
@tri = Triangle.build do |t|
t.position = Vector2.new(@width / 2, @height / 2)
t.frame = PF::VectorSprite.generate_circle(3, size = @width / 3)
2021-11-29 08:26:51 +01:00
end
@controller = PF::Controller(LibSDL::Scancode).new({
LibSDL::Scancode::RIGHT => "Rotate Right",
LibSDL::Scancode::LEFT => "Rotate Left",
LibSDL::Scancode::SPACE => "Pause",
LibSDL::Scancode::A => "Move Left",
LibSDL::Scancode::D => "Move Right",
LibSDL::Scancode::W => "Move Up",
LibSDL::Scancode::S => "Move Down",
2021-11-29 08:26:51 +01:00
})
end
def update(dt)
@paused = !@paused if @controller.pressed?("Pause")
@tri.rotation += 0.5 * dt if @controller.action?("Rotate Right")
@tri.rotation -= 0.5 * dt if @controller.action?("Rotate Left")
2021-11-29 08:26:51 +01:00
2021-12-06 04:02:11 +01:00
if @controller.action?("Move Up")
@tri.frame[1] = @tri.frame[1] + Vector2.new(0.0, -10.0) * dt
end
if @controller.action?("Move Down")
@tri.frame[1] = @tri.frame[1] + Vector2.new(0.0, 10.0) * dt
end
if @controller.action?("Move Left")
@tri.frame[1] = @tri.frame[1] + Vector2.new(-10.0, 0.0) * dt
end
if @controller.action?("Move Right")
@tri.frame[1] = @tri.frame[1] + Vector2.new(10.0, 0.0) * dt
end
2021-11-29 08:26:51 +01:00
unless @paused
@tri.rotation += 1.0 * dt
end
@tri.update(dt)
end
def draw
clear(0, 0, 100)
2021-11-29 08:26:51 +01:00
@tri.draw(self)
end
end
2021-12-06 04:02:11 +01:00
engine = TriangleThing.new(50, 50, 10)
2021-11-29 08:26:51 +01:00
engine.run!