class Ghost
Trida Ghost
reprezentuje pocitacem ovladane duchy.
Attributes
am_i_blue[RW]
reprezentuje stav ducha
speed[W]
duch ma moznost zmenit rychlost pohybu
Public Class Methods
new(left, top, speed, r)
click to toggle source
Vytvori noveho ducha umesteneho na mape v poloze left
a top
, pohybujiciho se rychlosti speed
a vychozim smerem pohybu r
.
-
Args :
-
left
-> poloha na ose X -
top
-> poloha na ose Y -
speed
-> poloha na ose rychlost pohybu -
r
-> vychozi smer pohybu
-
Calls superclass method
Figure::new
# File lib/figure.rb, line 111 def initialize(left, top, speed, r) super(left, top, speed, r) @am_i_blue = false end
Public Instance Methods
eat_pacman(p)
click to toggle source
Pokud se duch srazi s hracem p
tak ho bud sni (hra konci), nebo je snezen a vrati se na vychozi umisteni.
-
Args :
-
p
-> postava pacmana
-
-
Returns :
-
GAME_OVER -> pacman byl snezen a hra konci
-
I GO HOME -> duch byl snezen a vraci se
-
# File lib/figure.rb, line 130 def eat_pacman(p) c = CROFTWIDTH return unless (@left - p.left).abs < c - 18 && (@top - p.top).abs < c - 18 if !am_i_blue 'GAME_OVER' else 'I GO HOME' end end
try_set_direction(area)
click to toggle source
Vyzkousi kam je mozne se pohybovat a nastavi novy smer pohybu
-
Args :
-
area
-> mapa hry
-
# File lib/figure.rb, line 144 def try_set_direction(area) original_direction = @direction possible_moves = [] @directions.each do |dir| @direction = dir possible_moves.push(dir) if can_move(area) end select_direction(original_direction, possible_moves) end
Private Instance Methods
compare_l_r(org, dir)
click to toggle source
# File lib/figure.rb, line 178 def compare_l_r(org, dir) lr1 = org == :left && dir == :right lr2 = org == :right && dir == :left return true if lr1 || lr2 false end
compare_t_d(org, dir)
click to toggle source
# File lib/figure.rb, line 171 def compare_t_d(org, dir) td1 = org == :up && dir == :down td2 = org == :down && dir == :up return true if td1 || td2 false end
oposit?(org, dir)
click to toggle source
# File lib/figure.rb, line 166 def oposit?(org, dir) return true if compare_l_r(org, dir) || compare_t_d(org, dir) false end
select_direction(original_direction, possible_moves)
click to toggle source
# File lib/figure.rb, line 154 def select_direction(original_direction, possible_moves) if possible_moves.length == 1 || possible_moves.empty? @direction = possible_moves[0] else r = Random.rand(possible_moves.length) while oposit?(original_direction, possible_moves[r]) r = Random.rand(possible_moves.length) end @direction = possible_moves[r] end end