Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created September 15, 2019 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/c2cce87b3ffe1eb4c8c85df04b0ed297 to your computer and use it in GitHub Desktop.
Save amirrajan/c2cce87b3ffe1eb4c8c85df04b0ed297 to your computer and use it in GitHub Desktop.
Vertical Platformer Written in DragonRuby Game Toolkit
class VerticalPlatformer
gtk_args
def inputs
args.inputs
end
def s
state.vertical_platformer ||= state.new_entity(:vertical_platformer)
state.vertical_platformer
end
def new_platform hash
s.new_entity_strict(:platform, hash)
end
def defaults
s.platforms ||= [
new_platform(x: 0, y: 0, w: 700, h: 32, dx: 1, speed: 0, rect: nil),
new_platform(x: 0, y: 300, w: 700, h: 32, dx: 1, speed: 0, rect: nil),
]
s.tick_count = args.state.tick_count
s.gravity = -0.3
s.player.platforms_cleared ||= 0
s.player.x ||= 0
s.player.y ||= 100
s.player.w ||= 64
s.player.h ||= 64
s.player.dy ||= 0
s.player.dx ||= 0
s.player_jump_power = 15
s.player_jump_power_duration = 10
s.player_max_run_speed = 5
s.player_speed_slowdown_rate = 0.9
s.player_acceleration = 1
s.camera ||= { y: -100 }
end
def render
outputs.solids << s.platforms.map do |p|
[p.x + 300, p.y - s.camera[:y], p.w, p.h]
end
outputs.solids << [
s.player.x + 300,
s.player.y - s.camera[:y],
s.player.w,
s.player.h,
100,
100,
200
]
end
def input
if inputs.keyboard.key_down.space || inputs.keyboard.key_held.space
s.player.jumped_at ||= s.tick_count
if s.player.jumped_at.elapsed_time < s.player_jump_power_duration && !s.player.falling
s.player.dy = s.player_jump_power
end
end
if inputs.keyboard.key_up.space
s.player.falling = true
end
if inputs.keyboard.left
s.player.dx -= s.player_acceleration
s.player.dx = s.player.dx.greater(-s.player_max_run_speed)
elsif inputs.keyboard.right
s.player.dx += s.player_acceleration
s.player.dx = s.player.dx.lesser(s.player_max_run_speed)
else
s.player.dx *= s.player_speed_slowdown_rate
end
end
def calc
s.platforms.each do |p|
p.rect = [p.x, p.y, p.w, p.h]
end
s.player.point = [s.player.x + s.player.w.half, s.player.y]
collision = s.platforms.find { |p| s.player.point.inside_rect? p.rect }
if collision && s.player.dy <= 0
s.player.y = collision.rect.y + collision.rect.h - 2
s.player.dy = 0 if s.player.dy < 0
if !s.player.platform
s.player.dx = 0
end
s.player.x += collision.dx * collision.speed
s.player.platform = collision
if s.player.falling
s.player.dx = 0
end
s.player.falling = false
s.player.jumped_at = nil
else
s.player.platform = nil
s.player.y += s.player.dy
s.player.dy += s.gravity
end
s.platforms.each do |p|
p.x += p.dx * p.speed
if p.x < -300
p.dx *= -1
p.x = -300
elsif p.x > (1000 - p.w)
p.dx *= -1
p.x = (1000 - p.w)
end
end
delta = (s.player.y - s.camera[:y] - 100)
if delta > -200
s.camera[:y] += delta * 0.01
s.player.x += s.player.dx
has_platforms = s.platforms.find { |p| p.y > (s.player.y + 300) }
if !has_platforms
width = 700 - (700 * (0.1 * s.player.platforms_cleared))
s.player.platforms_cleared += 1
last_platform = s.platforms[-1]
s.platforms << new_platform(x: (700 - width) * rand,
y: last_platform.y + 300,
w: width,
h: 32,
dx: 1.randomize(:sign),
speed: 2 * s.player.platforms_cleared,
rect: nil)
end
else
s.hash.clear
end
end
def tick
defaults
render
calc
input
end
end
$game = VerticalPlatformer.new
def tick args
$game.args = args
$game.tick
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment