Game Development Reference
In-Depth Information
def button_down ( id )
# ...
if id == Gosu :: KbQ
leave
$window . close
end
end
# ...
end
Now,
to
enable
profiling,
simply
start
your
game
with ENABLE_PROFILING=1
environmental variable, like this:
$ ENABLE_PROFILING = 1 ruby-prof 03-prototype/main.rb
Adjusting Game Speed For Variable Performance
You should have noticed that our optimized Tanks prototype runs way too fast. Tanks and
bullets should travel same distance no matter how fast or slow the code is.
One would expect Gosu::Window#update_interval to be designed exactly for
that purpose, but it returns 16.6666 in both original and optimized version of the
prototype, so you can guess it is the desired interval, not the actual one.
To find out actual update interval, we will use Gosu.milliseconds and calculate it
ourselves. To do that, we will introduce Game#track_update_interval that will be
called in GameWindow#update , and Game#update_interval which will retrieve
actual update interval, so we can use it to adjust our run speed.
We will also add Game#adjust_speed method that will take arbitrary speed value
and shift it so is as fast as it was when the game was running at 30 FPS. The formula is
simple, if 60 FPS expects to call Gosu::Window#update every 16.66 ms, our speed
adjustment will divide actual update rate from 33.33 , which roughly equals to 16.66 *
2 . So, if bullet would fly 100 pixels per update in 30 FPS, adjusted speed will change it to
50 pixels at 60 FPS.
Here is the implementation:
# 04-prototype-optimized/main.rb
module Game
# ...
Search WWH ::




Custom Search