Game Development Reference
In-Depth Information
Images And Animation
It's time to make something more exciting. Our game will have to have explosions,
therefore we need to learn to animate them. We will set up a background scene and trigger
explosions on top of it with our mouse.
01-hello/hello_animation.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
initialize
(animation, x, y)
18
@animation
=
animation
19
@x
,
@y
=
x, y
20
@current_frame
= 0
21
end
22
23
def
update
24
@current_frame
+= 1
if
frame_expired?
25
end
26
27
def
draw
28
returnif
done?
29
image
=
current_frame
image
.
draw(
30
@x
-
image
.
width
/ 2.0
,
31
@y
-
image
.
height
/ 2.0
,
32
0
)
33
34
end
35
36
def
done?
37
@done
||=
@current_frame
==
@animation
.
size
38
end
39