Game Development Reference
In-Depth Information
96
angle
-= 45.0
if
$window
.
button_down?(left)
97
angle
+= 45.0
if
$window
.
button_down?(right)
98
elsif
$window
.
button_down?(left)
99
angle
= 90.0
100
angle
+= 45.0
if
$window
.
button_down?(up)
101
angle
-= 45.0
if
$window
.
button_down?(down)
102
elsif
$window
.
button_down?(right)
103
angle
= 270.0
104
angle
-= 45.0
if
$window
.
button_down?(up)
105
angle
+= 45.0
if
$window
.
button_down?(down)
106
end
107
angle
||
previous_angle
108
end
109
end
Tank
has to be aware of the
Map
to check where it's moving, and it uses
Camera
to find
out where to aim the guns. When it
shoot
s, it produces instances of
Bullet
, that are
simply returned to the caller. Tank won't keep track of them, it's “fire and forget”.
Implementing Bullets And Explosions
Bullets will require some simple vector math. You have a point that moves along the vector
with some speed. It also needs to limit the maximum vector length, so if you try to aim too
far, the bullet will only go as far as it can reach.
03-prototype/entities/bullet.rb
1
class
Bullet
2
COLOR
=
Gosu
::
Color
::
BLACK
MAX_DIST
= 300
3
START_DIST
= 20
4
5
6
def
initialize
(source_x, source_y, target_x, target_y)
7
@x
,
@y
=
source_x, source_y
8
@target_x
,
@target_y
=
target_x, target_y
9
@x
,
@y
=
point_at_distance(
START_DIST
)
10
if
trajectory_length
>
MAX_DIST
11
@target_x
,
@target_y
=
point_at_distance(
MAX_DIST
)
12
end
13
sound
.
play
14
end
15
16
def
draw
17
unless
arrived?
18
$window
.
draw_quad(
@x
- 2
,
@y
- 2
,
COLOR
,