Class: GamesAndRpgParadise::Tetris::GameWindow

Inherits:
Gosu::Window
  • Object
show all
Includes:
GamesAndRpgParadise::Tetris
Defined in:
lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb

Overview

#

This is the actual game-window.

#

Constant Summary collapse

BLOCK_WIDTH =
#

BLOCK_WIDTH

#
32
BLOCK_HEIGHT =
#

BLOCK_HEIGHT

#
32
WIDTH =
#

WIDTH

#
400
HEIGHT =
#

HEIGHT

#
700

Constants included from GamesAndRpgParadise::Tetris

FILE_BLOCK, FILE_TETRIS_THEME, STATE_GAMEOVER, STATE_PLAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Gosu::Window

#gosu_button_down?, #image, #image10?, #image1?, #image2?, #image3?, #image4?, #image5?, #image6?, #image7?, #image8?, #image9?, #on_left_arrow_pressed?, #on_right_arrow_pressed?, #q_means_quit, #set_font, #set_title, #sqrt, #tab_key?, #write_this_text

Constructor Details

#initializeGameWindow

#

initialize

#


52
53
54
55
56
57
58
59
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 52

def initialize
  super(WIDTH, HEIGHT, false)
  reset
  spawn_next_shape
  self.caption = "Tetris : #{@lines_cleared} lines"
  do_initialize_the_main_tetris_theme
  do_start_playing_the_main_song
end

Instance Attribute Details

#block_heightObject (readonly)

Returns the value of attribute block_height.



27
28
29
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 27

def block_height
  @block_height
end

#blocksObject

Returns the value of attribute blocks.



25
26
27
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 25

def blocks
  @blocks
end

Instance Method Details

#block_width?Boolean Also known as: block_width

#

block_width?

#

Returns:

  • (Boolean)


256
257
258
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 256

def block_width?
  @block_width
end

#button_down(id) ⇒ Object

#

button_down

#


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 305

def button_down(id)
  case id
  # ======================================================================= #
  # === Button 'H' pressed
  # ======================================================================= #
  when Gosu::KbH
    @show_the_help_menu = true
  # ======================================================================= #
  # === Button 'Spacebar' was pressed
  # ======================================================================= #
  when Gosu::KbSpace
    # ===================================================================== #
    # Rotate the shape when the space-key was pressed.
    # ===================================================================== #
    unless @falling_shape.nil?
      @falling_shape.rotation += 1
      if @falling_shape.collide
        @falling_shape.rotation -= 1
      end
    end
  end
end

#button_up(id) ⇒ Object

#

button_up

This is the button-release action.

#


292
293
294
295
296
297
298
299
300
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 292

def button_up(id)
  case id
  # ======================================================================= #
  # The 'H' key is used for the help-screen display.
  # ======================================================================= #
  when Gosu::KbH
    @show_the_help_menu = false
  end
end

#check_whether_the_song_should_be_paused_or_notObject

#

check_whether_the_song_should_be_paused_or_not

The “P” button can be used to silence audio-output from this game, or re-establish it again.

#


162
163
164
165
166
167
168
169
170
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 162

def check_whether_the_song_should_be_paused_or_not
  if button_down?(Gosu::KbP)
    if @song.paused?
      @song.play(true)
    else
      @song.pause
    end
  end
end

#check_whether_we_should_quitObject

#

check_whether_we_should_quit

#


175
176
177
178
179
180
181
182
183
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 175

def check_whether_we_should_quit
  if button_down?(Gosu::KbEscape)
    close
  elsif button_down?(Gosu::KbBackspace)
    close
  elsif button_down?(Gosu::KbQ)
    close
  end
end

#consider_drawing_the_help_screen(x = 25, y = 250) ⇒ Object

#

consider_drawing_the_help_screen (help tag)

This method will draw the on-game-map help screen.

#


333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 333

def consider_drawing_the_help_screen(
    x = 25,
    y = 250
  )
  return unless @show_the_help_menu
  @help_font.red_text('ESC:   quit the application',          x, y     )
  @help_font.red_text('P:     silence the audio ',            x, y + 20)
  @help_font.red_text('       (or re-instate audio)',         x, y + 40)
  @help_font.red_text('SPACE: turns the tetris-block',        x, y +  60)
  @help_font.red_text('↓:     accelerate this tetris-block',  x, y +  80)
  @help_font.red_text('Q:     quits the application as well', x, y + 100)
end

#delete_lines_of(shape) ⇒ Object

#

delete_lines_of

#


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 229

def delete_lines_of(shape)
  # ======================================================================= #
  # Go through each block of the shape and check if the lines they are 
  # on are complete.
  # ======================================================================= #
  deleted_lines = []
  shape.get_blocks.each { |block|
    if line_complete(block.y)
      deleted_lines.push(block.y)
      @blocks = @blocks.delete_if { |item| item.y == block.y }
    end
  }

  @lines_cleared += deleted_lines.length
  # ======================================================================= #
  # This applies the standard gravity found in classic Tetris games -
  # all blocks go down by the amount of lines cleared.
  # ======================================================================= #
  @blocks.each { |block|
    i = deleted_lines.count{ |y| y > block.y }
    block.y += i*block_height
  }
end

#do_initialize_the_main_tetris_themeObject

#

do_initialize_the_main_tetris_theme

#


115
116
117
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 115

def do_initialize_the_main_tetris_theme
  @song = Gosu::Song.new(::GamesAndRpgParadise::Tetris::FILE_TETRIS_THEME)
end

#do_start_playing_the_main_songObject

#

do_start_playing_the_main_song

#


108
109
110
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 108

def do_start_playing_the_main_song
  @song.play(true)
end

#drawObject

#

draw

#


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 263

def draw
  consider_drawing_the_help_screen
  @blocks.each { |block| block.draw }
  @falling_shape.draw
  case @state
  # ======================================================================= #
  # Handle game-over situations next.
  # ======================================================================= #
  when STATE_GAMEOVER
    # Show a game-over picture in this case.
    # text = Gosu::Image.from_text(self, 'Game Over', 'Arial', 40)
    text = Gosu::Image.from_text('Game Over', 40, font: 'Arial')
    text.draw(width/2 - 90, height/2 - 20, 0, 1, 1)
    @song.stop # Stop the song in this case as well.
  end
end

#falling_shape?Boolean Also known as: falling_shape

#

falling_shape?

#

Returns:

  • (Boolean)


283
284
285
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 283

def falling_shape?
  @falling_shape
end

#level?Boolean Also known as: level

#

level?

#

Returns:

  • (Boolean)


122
123
124
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 122

def level?
  @level
end

#line_complete(y) ⇒ Object

#

line_complete

#


216
217
218
219
220
221
222
223
224
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 216

def line_complete(y)
  # Important is that the screen resolution should be divisable by the block_width, otherwise there would be gap
  # If the count of blocks at a line is equal to the max possible blocks for any line - the line is complete
  i = @blocks.count {|item| item.y == y}
  if ( i == width / block_width )
    return true
  end
  return false
end

#resetObject

#

reset

#


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/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 64

def reset
  # ======================================================================= #
  # === @falling_shape
  # ======================================================================= #
  @falling_shape = nil
  # ======================================================================= #
  # === @blocks
  # ======================================================================= #
  @blocks = []
  # ======================================================================= #
  # === @state
  # ======================================================================= #
  @state = STATE_PLAY
  # ======================================================================= #
  # === @block_width
  # ======================================================================= #
  @block_width  = BLOCK_WIDTH
  # ======================================================================= #
  # === @block_height
  # ======================================================================= #
  @block_height = BLOCK_HEIGHT
  # ======================================================================= #
  # === @lines_cleared
  # ======================================================================= #
  @lines_cleared = 0
  # ======================================================================= #
  # === @help_font
  #
  # The help-font is the one that is used for the on-game help screen.
  # ======================================================================= #
  @help_font = return_font(:hack_18) # Gosu::Font.new(22)
  # ======================================================================= #
  # === @level
  # ======================================================================= #
  @level = 0
  # ======================================================================= #
  # === @show_the_help_menu
  # ======================================================================= #
  @show_the_help_menu = false
end

#spawn_next_shapeObject

#

spawn_next_shape

This method will spawn a random shape and add the current falling shape’ blocks to the “static” blocks list.

In other words: this is the method that will generate a new shape.

#


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 193

def spawn_next_shape
  unless @falling_shape.nil?
    @blocks += @falling_shape.get_blocks
  end
  # ======================================================================= #
  # Define the 7 possible shapes next.
  # ======================================================================= #
  shapes = [
    ShapeI.new(self),
    ShapeL.new(self),
    ShapeJ.new(self),
    ShapeCube.new(self),
    ShapeZ.new(self),
    ShapeT.new(self),
    ShapeS.new(self)
  ]
  sample = shapes.sample
  @falling_shape = sample
end

#updateObject

#

update

#


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/games_and_rpg_paradise/gui/gosu/tetris/game_window.rb', line 129

def update
  case @state
  when STATE_PLAY
    if @falling_shape
      if @falling_shape.collide
        @state = STATE_GAMEOVER # Change the state to game-over.
      else
        @falling_shape.update
      end
    end
    @level = @lines_cleared / 10
    self.caption = "Tetris : #{@lines_cleared} lines"
  else
    if button_down?(Gosu::KbSpace)
      @blocks = []
      @falling_shape = nil
      @level = 0
      @lines_cleared = 0
      spawn_next_shape
      @state = STATE_PLAY
    end
  end

  check_whether_we_should_quit
  check_whether_the_song_should_be_paused_or_not
end