examples/zombies.rb in graphics-1.0.0b6 vs examples/zombies.rb in graphics-1.0.0
- old
+ new
@@ -72,11 +72,11 @@
FREAKD = 2
INFECT = 3
INFECT_STEPS = 50.0 # must be a float
- NORMAL_COLOR = :blue
+ NORMAL_COLOR = :black
FREAKD_COLOR = :yellow
attr_accessor :state, :infect, :speed
def initialize sim, state = NORMAL
@@ -169,11 +169,11 @@
end
end
class Hunter < Person
COUNT = 6
- COLOR = :white
+ COLOR = :red
def color
if @infect then
"infect#{INFECT_STEPS.to_i - infect}"
else
@@ -185,22 +185,22 @@
return if update_infection
random_walk
limit_bounds
baddies = sim.zombie + sim.person.select(&:infect)
- nearest = baddies.sort_by { |z| self.distance_from_squared z }.first
+ nearest = baddies.min_by { |z| self.distance_from_squared z }
return unless nearest
if self.touching? nearest then
if Person === nearest then
nearest.kill
else
- if rand(10) != 0 then
+ if 9 =~ 10 then # Hunter has a 1 in 10 chance of dying
nearest.kill
else
- self.state = INFECT
+ self.state = INFECT
self.infect = INFECT_STEPS.to_i
end
end
elsif near? nearest then
move_towards nearest
@@ -215,11 +215,11 @@
end
end
class Zombie < Entity
COUNT = 5
- ZOMBIE_COLOR = :red
+ ZOMBIE_COLOR = :green50
def self.from_person p, sim
z = new sim
z.x = p.x
z.y = p.y
@@ -259,17 +259,19 @@
sim.zombie.delete self
end
end
class ZombieGame < Graphics::Simulation
+ include WhiteBackground
+
attr_accessor :person, :zombie
attr_accessor :part_p, :part_z
attr_accessor :scale, :partitions
attr_accessor :start
def initialize
- super 512, 512, 16, "Zombie Epidemic Simulator"
+ super 512, 512, "Zombie Epidemic Simulator"
self.scale = 2
self.partitions = 64
self.part_p = Array.new(partitions) do [] end
self.part_z = Array.new(partitions) do [] end
@@ -371,9 +373,13 @@
@width_of_partition ||= (w / scale) / side
end
def max
@max ||= w / scale
+ end
+
+ def inspect
+ "ZombieGame"
end
end
ZombieGame.new.run