Class: Game

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

Overview

The main game window class

NOTE: all the sizes in the code are in_blocks

Copyright © 2011 Nikolay Nemshilov

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/game.rb', line 12

def initialize
  super(27 * Block::SIZE, 27 * Block::SIZE, false)
  self.caption = "Pentix"

  @glass    = Glass.new(self,   1, 1)
  @status   = Status.new(self, 16, 1)

  @controls = Controls.new     # keybindings
  @finish   = Finish.new(self) # the gameover screen

  reset!
end

Instance Attribute Details

#glassObject

Returns the value of attribute glass.



10
11
12
# File 'lib/game.rb', line 10

def glass
  @glass
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/game.rb', line 10

def status
  @status
end

Instance Method Details

#button_down(button) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/game.rb', line 65

def button_down(button)
  case @controls.command_for(button)
    when :drop       then @figure.drop        if @playing
    when :left       then @figure.move_left   if @playing
    when :right      then @figure.move_right  if @playing
    when :turn_left  then @figure.turn_left   if @playing
    when :turn_right then @figure.turn_right  if @playing
    when :reset      then reset!
    when :quit       then close
    else
      @finish.enter! if button == Button::KbReturn
  end
end

#drawObject



25
26
27
28
29
30
31
32
33
# File 'lib/game.rb', line 25

def draw
  if @playing
    @glass.draw
    @status.draw
    @figure.draw
  else
    @finish.draw
  end
end

#its_over!Object



60
61
62
63
# File 'lib/game.rb', line 60

def its_over!
  @finish.score = @status.score
  @playing = false
end

#reset!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/game.rb', line 45

def reset!
  @status.reset!
  @glass.reset!
  @finish.reset!

  show_next_figure

  @time_offset = 1000 / @status.level # blocks per second
  @next_time   = 0

  time_to_move # precalculating the next time to move

  @playing     = true
end

#show_next_figureObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/game.rb', line 79

def show_next_figure
  @figure        = @status.figure || Figure.new(self)
  @status.figure = Figure.new(self)

  x = (Glass::WIDTH - @figure.size_x)/2 + 2
  y = @glass.pos_y

  if @glass.has_space_for?(@figure.matrix, x, y)
    @figure.move_to(x, y)
  else
    @figure.pos_x = x
    @figure.pos_y = y

    its_over!
  end
end

#time_to_moveObject



96
97
98
99
100
101
102
# File 'lib/game.rb', line 96

def time_to_move
  if Gosu::milliseconds > @next_time
    @next_time = Gosu::milliseconds + @time_offset
  else
    false
  end
end

#updateObject



35
36
37
38
39
40
41
42
43
# File 'lib/game.rb', line 35

def update
  if @playing && time_to_move
    if @figure.distance > 0
      @figure.move_down
    else
      @figure.drop
    end
  end
end