class Fiveinarow::Game

Attributes

state[RW]

Whether is game or the end of a game

Public Class Methods

new(root_dir) click to toggle source
Calls superclass method
# File lib/fiveinarow.rb, line 17
def initialize(root_dir)
  super(800, 800, false)
  self.caption = 'Five In A Row'
  @root_dir = root_dir
  @board = Board.new(self, 22, root_dir)

  @player_a = HotseatPlayer.new(Cell::PLAYER_A)
  @player_b = AIPlayer.new(Cell::PLAYER_B)

  @player_on_turn = @player_a

  @font = Gosu::Font.new(60)



  @background = Gosu::Image.new(self, File.join(root_dir, "lib/media/background_800_800.png"))
  @the_end = Gosu::Image.new(self, File.join(root_dir, "lib/media/the_end_800_800.png"))
  @last_milliseconds = 0
end

Public Instance Methods

button_up(key) click to toggle source

this is a callback for key up events or equivalent (there are constants for gamepad buttons and mouse clicks)

# File lib/fiveinarow.rb, line 52
def button_up(key)
  self.close if key == Gosu::KbEscape

  # reset the game
  if @state == :end && key == Gosu::MsLeft
    @board = Board.new(self, 22, @root_dir)
    @state = :game
    return
  end

  if @player_on_turn.class == HotseatPlayer && key == Gosu::MsLeft
    if @board.cell_clicked(mouse_x, mouse_y, @player_on_turn.sym)

      if @state == :end
        return
      end

      switch_players

      if @player_on_turn.class == AIPlayer
        @player_on_turn.make_move(@board)
        switch_players
      end
    end
  end
end
draw() click to toggle source
# File lib/fiveinarow.rb, line 37
def draw
  @background.draw(0, 0, ZOrder::Background)
  @board.draw(mouse_x, mouse_y, @player_on_turn.sym)
  if @state == :end
    @the_end.draw(0, 0, ZOrder::TheEnd)
  end

end
needs_cursor?() click to toggle source
# File lib/fiveinarow.rb, line 46
def needs_cursor?
  true
end
switch_players() click to toggle source
# File lib/fiveinarow.rb, line 79
def switch_players
  if @player_on_turn == @player_a
    @player_on_turn = @player_b
  else
    @player_on_turn = @player_a
  end
end
update() click to toggle source
# File lib/fiveinarow.rb, line 87
def update
  self.update_delta
  # with a delta we need to express the speed of our entities in
  # terms of pixels/second
end
update_delta() click to toggle source
# File lib/fiveinarow.rb, line 93
def update_delta
  # Gosu::millisecodns returns the time since the window was created
  # Divide by 1000 since we want to work in seconds
  current_time = Gosu::milliseconds / 1000.0
  # clamping here is important to avoid strange behaviors
  @delta = [current_time - @last_milliseconds, 0.25].min
  @last_milliseconds = current_time
end