Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created November 21, 2023 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/5c8da2094458e3e61c8cfaf24f60df71 to your computer and use it in GitHub Desktop.
Save amirrajan/5c8da2094458e3e61c8cfaf24f60df71 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit - Simple Dodging Game
def tick args
defaults args
input_player args
spawn_bullets args
calc_bullets args
render args
end
def defaults args
args.state.player ||= { x: 100, y: 100 + rand(520), w: 32, h: 32, path: "sprites/square/blue.png", hp: 10 }
args.state.bullets ||= []
args.state.explosion_queue ||= []
end
def input_player args
args.state.player.y += args.inputs.up_down * 5
if args.state.player.y <= 0
args.state.player.y = 0
elsif (args.state.player.y + args.state.player.h) >= 720
args.state.player.y = 720 - args.state.player.h
end
end
def spawn_bullets args
return if !args.state.tick_count.zmod? 10
args.state.bullets << { x: 1280 + 64, y: 64 + rand(592), w: 64, h: 64, path: "sprites/square/red.png" }
end
def calc_bullets args
args.state.bullets.each do |b|
b.x -= 10
if b.intersect_rect? args.state.player
args.state.player.hp -= 1
b.exploded = true
args.state.explosion_queue << b.merge(exploded_at: args.state.tick_count)
end
end
args.state.bullets.reject! { |b| b.exploded }
args.state.bullets.reject! { |b| b.x < -64 }
args.state.explosion_queue.reject! { |e| !e.exploded_at.frame_index(7, 4, false) }
args.gtk.reset_next_tick if args.state.player.hp <= 0
end
def render args
args.outputs.labels << {
x: args.state.player.x + 16,
y: args.state.player.y + 45,
text: "#{args.state.player.hp}",
alignment_enum: 1,
vertical_alignment_enum: 1
}
args.outputs.sprites << args.state.player
args.outputs.sprites << args.state.bullets
args.outputs.sprites << args.state.explosion_queue.map do |e|
frame_index = e.exploded_at.frame_index(7, 4, false)
e.merge path: "sprites/misc/explosion-#{frame_index}.png"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment