Game Development Reference
In-Depth Information
def self . track_update_interval
now = Gosu . milliseconds
@update_interval = (now - ( @last_update ||= 0 )) . to_f
@last_update = now
end
def self . update_interval
@update_interval ||= $window . update_interval
end
def self . adjust_speed (speed)
speed * update_interval / 33.33
end
end
# 04-prototype-optimized/game_window.rb
class GameWindow < Gosu :: Window
# ...
def update
Game . track_update_interval
@state . update
end
# ...
end
Now, to fix that speed problem, we will need to apply Game.adjust_speed to tank,
bullet and camera movements.
Here are all the changes needed to make our game run at roughly same speed in different
conditions:
# 04-prototype-optimized/entities/tank.rb
class Tank
# ...
def update (camera)
# ...
shift = Game . adjust_speed(speed)
new_x -= shift if $window . button_down?( Gosu :: KbA )
new_x += shift if $window . button_down?( Gosu :: KbD )
new_y -= shift if $window . button_down?( Gosu :: KbW )
new_y += shift if $window . button_down?( Gosu :: KbS )
# ...
end
# ...
end
Search WWH ::




Custom Search