class Pacman::Game

Represent game model

Attributes

ghosts[RW]
grid[RW]
lose[RW]
player[RW]
points[RW]
score[RW]
win[RW]

Public Class Methods

new(window) click to toggle source
# File lib/pacman/Game.rb, line 64
def initialize(window)
  path = ROOT_PATH + 'lvl.txt'
  map = File.foreach(path).map { |line| line.split(' ') }
  @player = Player.new(window)
  @player.warp(320, 240)
  @points = []
  @ghosts = []
  @score = 0
  @win = false
  @lose = false
  @grid = Array.new(GRIDX) { Array.new(GRIDY) }
  load_map(window, map)
end

Public Instance Methods

axis_val(x) click to toggle source
# File lib/pacman/Game.rb, line 19
def axis_val(x)
  BLOCK_SIZE * x + BLOCK_HSIZE
end
load_map(window, map) click to toggle source
# File lib/pacman/Game.rb, line 23
def load_map(window, map)
  map.each_with_index do
    |row, y| row.each_with_index do
      |col, x|
      case col
      when 's'
        @grid[x][y] = @player
        @player.warp(axis_val(x), axis_val(y))
      when 'x'
        @grid[x][y] = Block.new(window)
        @grid[x][y].warp(axis_val(x), axis_val(y))
      when 'p'
        point = Point.new(window)
        @grid[x][y] = point
        @grid[x][y].warp(axis_val(x), axis_val(y))
        @points.push(point)
      when 'r'
        ghost = Red.new(window)
        ghosts.push(ghost)
        @grid[x][y] = ghost
        @grid[x][y].warp(axis_val(x), axis_val(y))
      when 'i'
        ghost = Pink.new(window)
        ghosts.push(ghost)
        @grid[x][y] = ghost
        @grid[x][y].warp(axis_val(x), axis_val(y))
      when 'a'
        ghost = Orange.new(window)
        ghosts.push(ghost)
        @grid[x][y] = ghost
        @grid[x][y].warp(axis_val(x), axis_val(y))
      when 'c'
        ghost = Cyan.new(window)
        ghosts.push(ghost)
        @grid[x][y] = ghost
        @grid[x][y].warp(axis_val(x), axis_val(y))
      end
    end
  end
end
pressed_down() click to toggle source
# File lib/pacman/Game.rb, line 96
def pressed_down
  if @grid[@player.grid_x][(@player.grid_y + 1) % GRIDY].class != Block
    @player.turn_down
  end
end
pressed_left() click to toggle source
# File lib/pacman/Game.rb, line 78
def pressed_left
  if @grid[(@player.grid_x - 1) % GRIDX][@player.grid_y].class != Block
    @player.turn_left
  end
end
pressed_right() click to toggle source
# File lib/pacman/Game.rb, line 84
def pressed_right
  if @grid[(@player.grid_x + 1) % GRIDX][@player.grid_y].class != Block
    @player.turn_right
  end
end
pressed_up() click to toggle source
# File lib/pacman/Game.rb, line 90
def pressed_up
  if @grid[@player.grid_x][(@player.grid_y - 1) % GRIDY].class != Block
    @player.turn_up
  end
end
update() click to toggle source
# File lib/pacman/Game.rb, line 102
def update
  @player.move(@grid, @points)
  @ghosts.each { |ghost| ghost.move(@grid, @player, @ghosts) }

  @win = true if @points.length == 0
  @lose = true if @player.alive? == false
end