Skip to content

Instantly share code, notes, and snippets.

@Incanus3
Last active October 20, 2019 08:28
Show Gist options
  • Save Incanus3/01799c30ebe8c2a575d88faafe4296f7 to your computer and use it in GitHub Desktop.
Save Incanus3/01799c30ebe8c2a575d88faafe4296f7 to your computer and use it in GitHub Desktop.
require 'time'
require 'ruby2d'
DEFAULT_FONT = 'DejaVuSansMono.ttf'
DEFAULT_FONT_SIZE = 12
DEFAULT_FONT_COLOR = 'green'
def get_info_text(tick)
"time: #{Time.now.strftime('%T')}, tick: #{tick}"
end
def get_event_text(event)
"x: #{event.x}, y: #{event.y}"
end
def draw_text(text, x: 10, y: 10,
font: DEFAULT_FONT, size: DEFAULT_FONT_SIZE, color: DEFAULT_FONT_COLOR)
Text.new(text, font: font, size: size, color: color, x: x, y: y)
end
set title: 'ruby2d test'
tick = 0
triangle = Triangle.new(x1: 320, y1: 50,
x2: 540, y2: 430,
x3: 100, y3: 430,
color: ['red', 'green', 'blue'])
info_text = draw_text(get_info_text(tick), y: 10)
event_text = draw_text('' , y: info_text.y + DEFAULT_FONT_SIZE + 5)
update do
close if tick == 1000
tick += 1
info_text.text = get_info_text(tick)
triangle.x1 = (triangle.x1 + 1) % Window.width
triangle.x2 = triangle.x1 + 220
triangle.x3 = triangle.x1 - 220
end
on :mouse_down do |event|
hit = triangle.contains?(event.x, event.y)
event_text.text = "#{get_event_text(event)}, hit the triangle: #{hit ? 'yes' : 'no'}"
end
show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment