Class: AMazeIng::GameWindow

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

Instance Method Summary collapse

Constructor Details

#initialize(full_screen, game_mode) ⇒ GameWindow

Returns a new instance of GameWindow.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/a_maze_ing/game_window.rb', line 28

def initialize(full_screen, game_mode)
  @game_mode = game_mode
  super DIMENSION + SIDE_BAR, DIMENSION, full_screen, 30
  self.caption = "Maze"

  #---------------------------------------------------------------------------------#
  # create code block (update, player_draw and new_player) for different game mode  #
  #---------------------------------------------------------------------------------#
  if @game_mode == 1
    @update_lambda = lambda {
      check_for_finish(@player)
      @player.move
    }
    @player_draw_lambda = lambda {
      @player.draw
    }
    @new_player_lambda = lambda {
      @player = Player.new(1, 0, PLAYER_COLOR_PRIMARY)
    }
  elsif @game_mode == 2
    @update_lambda = lambda {

      if check_for_finish(@player)
        @infor.player_1_point += 1
      end
      @player.move
      if check_for_finish(@player_2)
        @infor.player_2_point += 1
      end
      @player_2.move

    }
    @player_draw_lambda = lambda {
      @player.draw
      @player_2.draw
    }
    @new_player_lambda = lambda {
      @player = Player.new(1, 0, PLAYER_COLOR_PRIMARY)
      @player_2 = Player.new(0, 1, PLAYER_COLOR_SECONDARY)
    }
  elsif @game_mode == 3
    @update_lambda = lambda {

      if check_for_finish(@player)
        @infor.player_1_point += 1
      end
      @player.move
      if check_for_collision(@player, @player_2)
        @infor.player_2_point += 1
      end
      @player_2.move

    }
    @player_draw_lambda = lambda {
      @player.draw
      @player_2.draw
    }
    @new_player_lambda = lambda {
      @player = Player.new(1, 0, PLAYER_COLOR_PRIMARY)
      @player_2 = Player.new($cols-1, 0, PLAYER_COLOR_ANGRY)
    }
  end
  #-------------------------#
  # end create code block   #
  #-------------------------#

  new_round
  if @game_mode == 1
    @infor = Infor.new
  elsif @game_mode == 2
    @infor = Infor.new(PLAYER_COLOR_PRIMARY, PLAYER_COLOR_SECONDARY)
  elsif @game_mode == 3
    @infor = Infor.new(PLAYER_COLOR_PRIMARY, PLAYER_COLOR_ANGRY)
  end

end

Instance Method Details

#button_down(id) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/a_maze_ing/game_window.rb', line 173

def button_down(id)
  if id == Gosu::KB_LEFT
    check_available_move(3, @player)
  end

  if id == Gosu::KB_RIGHT
    check_available_move(1, @player)
  end

  if id == Gosu::KB_UP
    check_available_move(0, @player)
  end
  
  if id == Gosu::KB_DOWN
    check_available_move(2, @player)
  end

  # control keys for player 2
  if @game_mode == 2 or @game_mode == 3

    if id == Gosu::KB_A
      check_available_move(3, @player_2)
    end

    if id == Gosu::KB_D
      check_available_move(1, @player_2)
    end

    if id == Gosu::KB_W
      check_available_move(0, @player_2)
    end
    
    if id == Gosu::KB_S
      check_available_move(2, @player_2)
    end

  end

  if id == Gosu::KB_ESCAPE
    close
  else
    super
  end
end

#check_available_move(path, player) ⇒ Object

check weather the direction player want to go to available or not if available, set new status for player



165
166
167
168
169
170
171
# File 'lib/a_maze_ing/game_window.rb', line 165

def check_available_move(path, player)
  if !$cells[player.cell_index_x + player.cell_index_y * $cols].walls[path]

    player.set_status(path)
    player.is_moving = true
  end
end

#check_for_collision(player_1, player_2) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/a_maze_ing/game_window.rb', line 135

def check_for_collision(player_1, player_2)
  if (player_2.x - player_1.x).abs < $player_size and (player_2.y - player_1.y).abs < $player_size
    new_round
    @infor.level += 1
    return true
  else
    return false
  end
end

#check_for_finish(player) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/a_maze_ing/game_window.rb', line 125

def check_for_finish(player)
  if player.x == @target_x and player.y == @target_y
    new_round
    @infor.level += 1
    return true
  else 
    return false
  end
end

#drawObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/a_maze_ing/game_window.rb', line 150

def draw
  $cells.each do |cell|
    if cell.visited 
      cell.draw($cell_size, Color::BLUE) 
    else
      cell.draw($cell_size, PLAYER_COLOR_PRIMARY)
    end
  end
  @player_draw_lambda.call
  @infor.draw
  draw_target
end

#draw_targetObject



118
119
120
121
122
123
# File 'lib/a_maze_ing/game_window.rb', line 118

def draw_target
  draw_quad @target_x,                @target_y,                Color::WHITE,
            @target_x + $player_size, @target_y,                Color::WHITE,
            @target_x + $player_size, @target_y + $player_size, Color::WHITE,
            @target_x,                @target_y + $player_size, Color::WHITE
end

#new_roundObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/a_maze_ing/game_window.rb', line 106

def new_round
  $rows += 2
  $cols += 2
  @maze = Maze.new
  @maze.generate_maze
  @new_player_lambda.call
  $target_cell_index_x += 2
  $target_cell_index_y += 2
  @target_x = ($target_cell_index_x * $cell_size) + $cell_size/2 - $player_size/2
  @target_y = ($target_cell_index_y * $cell_size) + $cell_size/2 - $player_size/2
end

#updateObject



145
146
147
148
# File 'lib/a_maze_ing/game_window.rb', line 145

def update
 @update_lambda.call
 @infor.update
end