Game Development Reference
In-Depth Information
Implementing Game Statistics
Games like one we are building are all about competition, and you cannot compete if you
don't know the score. Let us introduce a class that will be responsible for keeping tabs on
various statistics of every tank.
12-stats/misc/stats.rb
1 class Stats
2 attr_reader :name , :kills , :deaths , :shots , :changed_at
3 def initialize ( name )
4 @name = name
5 @kills = @deaths = @shots = @damage = @damage_dealt = 0
6 changed
7 end
8
9 def add_kill (amount = 1 )
10 @kills += amount
11 changed
12 end
13
14 def add_death
15 @deaths += 1
16 changed
17 end
18
19 def add_shot
20 @shots += 1
21 changed
22 end
23
24 def add_damage (amount)
25 @damage += amount
26 changed
27 end
28
29 def damage
30 @damage . round
31 end
32
33 def add_damage_dealt (amount)
34
@damage_dealt += amount
Search WWH ::




Custom Search