Class: BTetrisKp::GameState
- Inherits:
-
Object
- Object
- BTetrisKp::GameState
- Defined in:
- lib/btetris_kp/game.rb
Overview
class representing game window state
Instance Attribute Summary collapse
-
#game_over ⇒ Object
readonly
Returns the value of attribute game_over.
-
#paused ⇒ Object
readonly
Returns the value of attribute paused.
-
#rows_cleared ⇒ Object
Returns the value of attribute rows_cleared.
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
button handler.
-
#draw ⇒ Object
draws board and texts if paused or game over.
-
#get_board_s ⇒ Object
returns game board in a string (current piece included).
-
#initialize(window, x, y) ⇒ GameState
constructor
A new instance of GameState.
-
#initialize_sounds ⇒ Object
initializes ingame sounds.
-
#insert_garbage!(cnt) ⇒ Object
inserts garbage in board depending on count of cleared rows.
-
#needs_cursor? ⇒ Boolean
hides default system cursor.
-
#pause! ⇒ Object
switches pause boolean.
-
#update ⇒ Object
updates game.
Constructor Details
#initialize(window, x, y) ⇒ GameState
Returns a new instance of GameState.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/btetris_kp/game.rb', line 11 def initialize(window, x, y) @window = window @font = Gosu::Font.new(@window, Gosu.default_font_name, Const::FONT_BIG_SIZE) @board = Board.new(@window, x, y) @paused, @game_over = false, false # press time, set 0 when left, right or down key is pressed # used for smoother controls in update method @counter, @press_time, @rows_cleared = 0, 0, 0 initialize_sounds end |
Instance Attribute Details
#game_over ⇒ Object (readonly)
Returns the value of attribute game_over.
8 9 10 |
# File 'lib/btetris_kp/game.rb', line 8 def game_over @game_over end |
#paused ⇒ Object (readonly)
Returns the value of attribute paused.
8 9 10 |
# File 'lib/btetris_kp/game.rb', line 8 def paused @paused end |
#rows_cleared ⇒ Object
Returns the value of attribute rows_cleared.
9 10 11 |
# File 'lib/btetris_kp/game.rb', line 9 def rows_cleared @rows_cleared end |
Instance Method Details
#button_down(id) ⇒ Object
button handler
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/btetris_kp/game.rb', line 81 def (id) if @game_over || @paused pause! if @paused && id == Gosu::KbP if @game_over && id == Gosu::KbEscape @window.state = MenuState.new(@window) end else # handling specific events case id when Gosu::KbEscape @window.state = MenuState.new(@window) when Gosu::KbP pause! when Gosu::KbLeft @board.piece_left! @press_time = 0 when Gosu::KbRight @board.piece_right! @press_time = 0 when Gosu::KbDown @board.piece_down! @press_time = 0 when Gosu::KbUp @rotate_snd.play if @board.piece_rotate! when Gosu::KbSpace @board.piece_drop! @drop_snd.play @counter = Const::GAME_SPEED - 10 end end end |
#draw ⇒ Object
draws board and texts if paused or game over
72 73 74 75 76 77 78 |
# File 'lib/btetris_kp/game.rb', line 72 def draw @board.draw x = @window.width / 2 y = @window.height / 2 - 30 @font.draw(Const::PAUSE_CAPTION, x - 130, y, 0) if @paused @font.draw(Const::GAME_OVER_CAPTION, x - 170, y, 0) if @game_over end |
#get_board_s ⇒ Object
returns game board in a string (current piece included)
35 36 37 |
# File 'lib/btetris_kp/game.rb', line 35 def get_board_s @board.get_board_s end |
#initialize_sounds ⇒ Object
initializes ingame sounds
23 24 25 26 27 |
# File 'lib/btetris_kp/game.rb', line 23 def initialize_sounds @drop_snd = Gosu::Sample.new(@window, Const::PATH_SND_DROP) @clear_snd = Gosu::Sample.new(@window, Const::PATH_SND_POP) @rotate_snd = Gosu::Sample.new(@window, Const::PATH_SND_ROTATE) end |
#insert_garbage!(cnt) ⇒ Object
inserts garbage in board depending on count of cleared rows
30 31 32 |
# File 'lib/btetris_kp/game.rb', line 30 def insert_garbage!(cnt) @board.insert_garbage!(cnt) end |
#needs_cursor? ⇒ Boolean
hides default system cursor
114 115 116 |
# File 'lib/btetris_kp/game.rb', line 114 def needs_cursor? true end |
#pause! ⇒ Object
switches pause boolean
40 41 42 |
# File 'lib/btetris_kp/game.rb', line 40 def pause! @paused = !@paused end |
#update ⇒ Object
updates game
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 |
# File 'lib/btetris_kp/game.rb', line 45 def update unless @paused || @game_over @press_time += 1 @counter += 1 if @press_time % Const::DROP_SPEED == 0 # check if KbDown is pressed, move piece down in faster interval @board.piece_down! if @window.(Gosu::KbDown) end if @press_time % Const::TURN_SPEED == 0 # check if KbLeft is pressed, move piece left in faster interval @board.piece_left! if @window.(Gosu::KbLeft) # check if KbRight is pressed, move piece right in faster interval @board.piece_right! if @window.(Gosu::KbRight) end # checks if it's time for next step in game if @counter % Const::GAME_SPEED == 0 @rows_cleared = @board.next_step! @clear_snd.play if @rows_cleared > 0 # checks if it's game over @game_over = @board.game_over? end end end |