Class: Fiveinarow::Game

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/fiveinarow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fiveinarow.rb', line 15

def initialize
  super(800, 800, false)
  self.caption = 'Five In A Row'

  @board = Board.new(self, 22)

  @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, 'media/background_800_800.png')
  @the_end = Gosu::Image.new(self, 'media/the_end_800_800.png')
  @last_milliseconds = 0
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



13
14
15
# File 'lib/fiveinarow.rb', line 13

def state
  @state
end

Instance Method Details

#button_up(key) ⇒ Object

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fiveinarow.rb', line 47

def button_up(key)
  self.close if key == Gosu::KbEscape

  # reset the game
  if @state == :end && key == Gosu::MsLeft
    @board = Board.new(self, 22)
    @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

#drawObject



32
33
34
35
36
37
38
39
# File 'lib/fiveinarow.rb', line 32

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?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fiveinarow.rb', line 41

def needs_cursor?
  true
end

#switch_playersObject



74
75
76
77
78
79
80
# File 'lib/fiveinarow.rb', line 74

def switch_players
  if @player_on_turn == @player_a
    @player_on_turn = @player_b
  else
    @player_on_turn = @player_a
  end
end

#updateObject



82
83
84
85
86
# File 'lib/fiveinarow.rb', line 82

def update
  self.update_delta
  # with a delta we need to express the speed of our entities in
  # terms of pixels/second
end

#update_deltaObject



88
89
90
91
92
93
94
95
# File 'lib/fiveinarow.rb', line 88

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