Last active
October 20, 2019 08:28
-
-
Save Incanus3/01799c30ebe8c2a575d88faafe4296f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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