Game Development Reference
In-Depth Information
5.
Again,
we
are
saving
resources
and
not
redrawing
when
there
are
no
@explosions
in progress.
needs_redraw?
handles it.
It is important to understand that
update
and
draw
order is unpredictable, these methods
can be called by your system at different rate, you can't tell which one will be called more
often than the other one, so
update
should only be concerned with advancing object state,
and
draw
should only draw current state on screen if it is needed. The only reliable thing
Rule of the thumb:
draw
should be as lightweight as possible. Prepare all calculations in
update
and you will have responsive, smooth graphics.
Music And Sound
Our previous program was clearly missing a soundtrack, so we will add one. A background
music will be looping, and each explosion will become audible.
01-hello/hello_sound.rb
1
require
'gosu'
2
3
def
media_path
(file)
4
File
.
join(
File
.
dirname(
File
.
dirname(
__FILE__
)),
'media'
, file)
5
6
end
7
8
class
Explosion
9
FRAME_DELAY
= 10
# ms
SPRITE
=
media_path(
'explosion.png'
)
10
11
12
def
self
.
load_animation
(window)
13
Gosu
::
Image
.
load_tiles(
window,
SPRITE
,
128
,
128
,
false
)
14
15
end
16
17
def
self
.
load_sound
(window)
18
Gosu
::
Sample
.
new(
window, media_path(
'explosion.mp3'
))
19
20
end
21
22
def
initialize
(animation, sound, x, y)
23
@animation
=
animation
sound
.
play
24
@x
,
@y
=
x, y
25