Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created September 4, 2025 22:08
Show Gist options
  • Select an option

  • Save amirrajan/8b7b1182ffe0817b9f62614a4263ee54 to your computer and use it in GitHub Desktop.

Select an option

Save amirrajan/8b7b1182ffe0817b9f62614a4263ee54 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit - Star Perf Revisited
# Sprites represented as Hashes using the queue ~args.outputs.sprites~
# code up, but are the "slowest" to render.
# The reason for this is the access of the key in the Hash and also
# because the data args.outputs.sprites is cleared every tick.
def random_x
rand * $grid.w * -1
end
def random_y
rand * $grid.h * -1
end
def random_speed
1 + 4 * rand
end
def new_star args
{
x: random_x,
y: random_y,
w: 4, h: 4, path: 'sprites/tiny-star.png',
s: random_speed
}
end
def move_star star
star.x += star.s
star.y += star.s
if star.x > $grid.w || star.y > $grid.h
star.x = random_x
star.y = random_y
star.s = random_speed
end
end
# def move_star star
# star[:x] += star[:s]
# star[:y] += star[:s]
# if star[:x] > $grid.w || star[:y] > $grid.h
# star[:x] = random_x
# star[:y] = random_y
# star[:s] = random_speed
# end
# end
def boot args
args.state = {}
end
def tick args
args.state.star_count ||= 0
# sets console command when sample app initially opens
if Kernel.global_tick_count == 0
puts ""
puts ""
puts "========================================================="
puts "* INFO: Sprites, Hashes"
puts "* INFO: Please specify the number of sprites to render."
GTK.console.set_command "reset_with count: 100"
end
if args.inputs.keyboard.key_down.space
reset_with count: 5000
end
# init
if Kernel.tick_count == 0
args.state.stars = args.state.star_count.map { |i| new_star args }
end
# update
args.state.stars.each { |s| move_star s }
# render
args.outputs.sprites << args.state.stars
args.outputs.background_color = [0, 0, 0]
args.outputs.primitives << GTK.current_framerate_primitives
end
# resets game, and assigns star count given by user
def reset_with count: count
GTK.reset
GTK.args.state.star_count = count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment