Class: Ghost

Inherits:
Figure show all
Defined in:
lib/figure.rb

Overview

Trida Ghost reprezentuje pocitacem ovladane duchy.

Instance Attribute Summary collapse

Attributes inherited from Figure

#direction, #left, #speed, #top

Instance Method Summary collapse

Methods inherited from Figure

#can_move, #move

Constructor Details

#initialize(left, top, speed, r) ⇒ Ghost

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



111
112
113
114
# File 'lib/figure.rb', line 111

def initialize(left, top, speed, r)
  super(left, top, speed, r)
  @am_i_blue = false
end

Instance Attribute Details

#am_i_blueObject

reprezentuje stav ducha



117
118
119
# File 'lib/figure.rb', line 117

def am_i_blue
  @am_i_blue
end

#speed=(value) ⇒ Object (writeonly)

duch ma moznost zmenit rychlost pohybu



120
121
122
# File 'lib/figure.rb', line 120

def speed=(value)
  @speed = value
end

Instance Method Details

#eat_pacman(p) ⇒ Object

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



130
131
132
133
134
135
136
137
138
# 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) ⇒ Object

Vyzkousi kam je mozne se pohybovat a nastavi novy smer pohybu

  • Args :

    • area -> mapa hry



144
145
146
147
148
149
150
151
152
# 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