Last active
May 15, 2020 19:11
-
-
Save amirrajan/e23e00790e6c7451ed530dad87edf7d4 to your computer and use it in GitHub Desktop.
bouncy circle
This file contains 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
class Game | |
attr_gtk | |
def tick | |
fiddle | |
defaults | |
render | |
input | |
calc | |
end | |
def render | |
outputs.solids << [0, 0, 1280, 100] | |
outputs.sprites << circle_sprite | |
outputs.lines << args.state.terrain | |
if args.state.working_terrain_1 | |
if args.keyboard.shift? | |
outputs.lines << [args.state.working_terrain_1, | |
inputs.mouse.x, | |
state.working_terrain_1.y] | |
else | |
outputs.lines << [args.state.working_terrain_1, | |
inputs.mouse.x, | |
inputs.mouse.y] | |
end | |
end | |
end | |
def input | |
if inputs.mouse.press? | |
if !state.working_terrain_1 | |
state.working_terrain_1 = [inputs.mouse.x, inputs.mouse.y] | |
else | |
if inputs.keyboard.shift? | |
state.terrain << [state.working_terrain_1.x, | |
state.working_terrain_1.y, | |
inputs.mouse.x, | |
state.working_terrain_1.y] | |
else | |
state.terrain << [state.working_terrain_1.x, | |
state.working_terrain_1.y, | |
inputs.mouse.x, | |
inputs.mouse.y] | |
end | |
state.working_terrain_1 = nil | |
end | |
end | |
end | |
def circle_sprite | |
[state.x, state.y, 50, 50, 'sprites/circle-green.png'] | |
end | |
def fiddle | |
state.gravity = -0.5 | |
state.elasticity = 0.8 | |
state.entropy = 0.8 | |
end | |
def defaults | |
state.x ||= 640 | |
state.y ||= 700 | |
state.dy ||= state.gravity | |
state.drag ||= 0.005 | |
state.terrain ||= [] | |
end | |
def calc | |
outputs.debug << [30, 690, state.dy].label | |
if state.dy > 0 && state.dy < state.entropy | |
state.dy = 0 | |
end | |
state.y += state.dy | |
if state.y <= 100 | |
state.y = 100 | |
state.dy = (state.dy * -1) * state.elasticity | |
else | |
state.dy += state.gravity | |
state.dy += (state.dy * state.drag) ** 2 | |
end | |
end | |
end | |
$game = Game.new | |
def tick args | |
$game.args = args | |
$game.tick | |
end | |
$gtk.reset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment