class Pacman

Trida Pacman je hlavni trida ktera reprezentuje celou hru.

Attributes

area[R]

mapa hry

ghosts[R]

pole duchu ve hre

player[R]

postava ovladana hracem

Public Class Methods

new() click to toggle source

Vytvori novou instanci hry

# File lib/pacman.rb, line 22
def initialize
  @pc_speed = 10
  @gh_sp = 15
  @time_mark = 0
  @area = Area.new
  @ghosts = []
  make_ghosts
  @player = Player.new(@area.pc_pos[:x], @area.pc_pos[:y], @pc_speed, 1)
end

Public Instance Methods

move(direction) click to toggle source

Zmeni smer pohybu hrace na direction

  • Args :

    • direction -> novy smer pohybu hrace

# File lib/pacman.rb, line 42
def move(direction)
  @player.direction = direction
end
update() click to toggle source

Aktualizuje stav hry

  • Returns :

    • GAME_OVER -> Konec hry

# File lib/pacman.rb, line 50
def update
  try_time
  @player.move if @player.can_move(@area)
  if @player.collect_bonus(@area) == 10
    make_ghosts_blue
    @time_mark = Time.now
  end
  update_ghosts
end

Private Instance Methods

decide(value, ghost) click to toggle source
# File lib/pacman.rb, line 79
def decide(value, ghost)
  return true if value == 'GAME_OVER'
  return unless value == 'I GO HOME'
  @ghosts[ghost] = Ghost.new(@area.gh_pos[:x], @area.gh_pos[:y], @gh_sp, 2)
  @player.you_eat_them
  false
end
make_ghosts() click to toggle source
# File lib/pacman.rb, line 32
def make_ghosts
  4.times do
    @ghosts.push Ghost.new(@area.gh_pos[:x], @area.gh_pos[:y], @gh_sp, 2)
  end
end
make_ghosts_blue() click to toggle source
# File lib/pacman.rb, line 87
def make_ghosts_blue
  @ghosts.each do |ghost|
    ghost.am_i_blue = true
    ghost.speed = 5
  end
end
try_time() click to toggle source
# File lib/pacman.rb, line 70
def try_time
  return unless @time_mark != 0 && Time.now - @time_mark >= 5
  @ghosts.each do |ghost|
    ghost.am_i_blue = false
    ghost.speed = @gh_sp
  end
  @time_mark = 0
end
update_ghosts() click to toggle source
# File lib/pacman.rb, line 60
def update_ghosts
  @ghosts.each do |ghost|
    ghost.try_set_direction(@area)
    ghost.move
    if decide(ghost.eat_pacman(@player), @ghosts.index(ghost)) == true
      return 'GAME_OVER'
    end
  end
end