class Tetris

Copyright © 2021-2022 Andy Maleh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Copyright © 2021-2022 Andy Maleh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constants

BEVEL_CONSTANT
BLOCK_SIZE
COLOR_GRAY

Public Class Methods

new() click to toggle source
# File examples/tetris.rb, line 12
def initialize
  @game = Model::Game.new
end

Public Instance Methods

block(row: , column: , block_size: , &extra_content) click to toggle source
# File examples/tetris.rb, line 227
def block(row: , column: , block_size: , &extra_content)
  block = {}
  bevel_pixel_size = 0.16 * block_size.to_f
  color = Glimmer::LibUI.interpret_color(Model::Block::COLOR_CLEAR)
  block[:area] = area {
    block[:background_square] = square(0, 0, block_size) {
      fill color
    }
    
    block[:top_bevel_edge] = polygon {
      point_array 0, 0, block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size, bevel_pixel_size
      fill r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT
    }
    
    block[:right_bevel_edge] = polygon {
      point_array block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size, block_size
      fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
    }
    
    block[:bottom_bevel_edge] = polygon {
      point_array block_size, block_size, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size
      fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
    }
    
    block[:left_bevel_edge] = polygon {
      point_array 0, 0, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size
      fill r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT
    }
    
    block[:border_square] = square(0, 0, block_size) {
      stroke COLOR_GRAY
    }
    
    on_key_down do |key_event|
      case key_event
      in ext_key: :down
        if OS.windows?
          # rate limit downs in Windows as they go too fast when key is held
          @queued_downs ||= 0
          if @queued_downs < 2
            @queued_downs += 1
            Glimmer::LibUI.timer(0.01, repeat: false) do
              @game.down! if @queued_downs < 2
              @queued_downs -= 1
            end
          end
        else
          @game.down!
        end
      in key: ' '
        @game.down!(instant: true)
      in ext_key: :up
        case @game.up_arrow_action
        when :instant_down
          @game.down!(instant: true)
        when :rotate_right
          @game.rotate!(:right)
        when :rotate_left
          @game.rotate!(:left)
        end
      in ext_key: :left
        @game.left!
      in ext_key: :right
        @game.right!
      in modifier: :shift
        @game.rotate!(:right)
      in modifier: :control
        @game.rotate!(:left)
      else
        # Do Nothing
      end
    end
    
    extra_content&.call
  }
  block
end
create_gui() click to toggle source
# File examples/tetris.rb, line 23
def create_gui
  menu_bar
  
  @main_window = window('Glimmer Tetris') {
    content_size Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE + 98
    resizable false
    
    vertical_box {
      label { # filler
        stretchy false
      }
      
      score_board(block_size: BLOCK_SIZE) {
        stretchy false
      }
      
      @playfield_blocks = playfield(playfield_width: Model::Game::PLAYFIELD_WIDTH, playfield_height: Model::Game::PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
    }
  }
end
launch() click to toggle source
# File examples/tetris.rb, line 16
def launch
  create_gui
  register_observers
  @game.start!
  @main_window.show
end
menu_bar() click to toggle source
playfield(playfield_width: , playfield_height: , block_size: , &extra_content) click to toggle source
# File examples/tetris.rb, line 206
def playfield(playfield_width: , playfield_height: , block_size: , &extra_content)
  blocks = []
  vertical_box {
    padded false
    
    playfield_height.times.map do |row|
      blocks << []
      horizontal_box {
        padded false
        
        playfield_width.times.map do |column|
          blocks.last << block(row: row, column: column, block_size: block_size)
        end
      }
    end
    
    extra_content&.call
  }
  blocks
end
register_observers() click to toggle source
# File examples/tetris.rb, line 44
def register_observers
  observe(@game, :game_over) do |game_over|
    if game_over
      @pause_menu_item.enabled = false
      show_game_over_dialog
    else
      @pause_menu_item.enabled = true
      start_moving_tetrominos_down
    end
  end
  
  Model::Game::PLAYFIELD_HEIGHT.times do |row|
    Model::Game::PLAYFIELD_WIDTH.times do |column|
      observe(@game.playfield[row][column], :color) do |new_color|
        Glimmer::LibUI.queue_main do
          color = Glimmer::LibUI.interpret_color(new_color)
          block = @playfield_blocks[row][column]
          block[:background_square].fill = color
          block[:top_bevel_edge].fill = {r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT}
          block[:right_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
          block[:bottom_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
          block[:left_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
          block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
        end
      end
    end
  end
  
  Model::Game::PREVIEW_PLAYFIELD_HEIGHT.times do |row|
    Model::Game::PREVIEW_PLAYFIELD_WIDTH.times do |column|
      preview_updater = proc do
        Glimmer::LibUI.queue_main do
          new_color = @game.preview_playfield[row][column].color
          color = Glimmer::LibUI.interpret_color(new_color)
          block = @preview_playfield_blocks[row][column]
          if @game.show_preview_tetromino?
            block[:background_square].fill = color
            block[:top_bevel_edge].fill = {r: color[:r] + 4*BEVEL_CONSTANT, g: color[:g] + 4*BEVEL_CONSTANT, b: color[:b] + 4*BEVEL_CONSTANT}
            block[:right_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
            block[:bottom_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
            block[:left_bevel_edge].fill = {r: color[:r] - BEVEL_CONSTANT, g: color[:g] - BEVEL_CONSTANT, b: color[:b] - BEVEL_CONSTANT}
            block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
          else
            transparent_color = {r: 255, g: 255, b: 255, a: 0}
            block[:background_square].fill = transparent_color
            block[:top_bevel_edge].fill = transparent_color
            block[:right_bevel_edge].fill = transparent_color
            block[:bottom_bevel_edge].fill = transparent_color
            block[:left_bevel_edge].fill = transparent_color
            block[:border_square].stroke = transparent_color
          end
        end
      end
      observe(@game.preview_playfield[row][column], :color, &preview_updater)
      observe(@game, :show_preview_tetromino, &preview_updater)
    end
  end

  observe(@game, :score) do |new_score|
    Glimmer::LibUI.queue_main do
      @score_label.text = new_score.to_s
    end
  end

  observe(@game, :lines) do |new_lines|
    Glimmer::LibUI.queue_main do
      @lines_label.text = new_lines.to_s
    end
  end

  observe(@game, :level) do |new_level|
    Glimmer::LibUI.queue_main do
      @level_label.text = new_level.to_s
    end
  end
end
score_board(block_size: , &extra_content) click to toggle source
# File examples/tetris.rb, line 305
def score_board(block_size: , &extra_content)
  vertical_box {
    horizontal_box {
      label # filler
      grid {
        stretchy false
        
        label('Score') {
          left 0
          top 0
          halign :fill
        }
        @score_label = label {
          left 0
          top 1
          halign :center
        }
  
        label('Lines') {
          left 1
          top 0
          halign :fill
        }
        @lines_label = label {
          left 1
          top 1
          halign :center
        }
  
        label('Level') {
          left 2
          top 0
          halign :fill
        }
        @level_label = label {
          left 2
          top 1
          halign :center
        }
      }
      label # filler
    }
    
    horizontal_box {
      label # filler
      @preview_playfield_blocks = playfield(playfield_width: Model::Game::PREVIEW_PLAYFIELD_WIDTH, playfield_height: Model::Game::PREVIEW_PLAYFIELD_HEIGHT, block_size: block_size)
      label # filler
    }
  
    extra_content&.call
  }
end
show_about_dialog() click to toggle source
# File examples/tetris.rb, line 392
def show_about_dialog
  Glimmer::LibUI.queue_main do
    msg_box('About', 'Glimmer Tetris - Glimmer DSL for LibUI Example - Copyright (c) 2021-2022 Andy Maleh')
  end
end
show_game_over_dialog() click to toggle source
# File examples/tetris.rb, line 369
def show_game_over_dialog
  Glimmer::LibUI.queue_main do
    msg_box('Game Over!', "Score: #{@game.high_scores.first.score}\nLines: #{@game.high_scores.first.lines}\nLevel: #{@game.high_scores.first.level}")
    @game.restart!
  end
end
show_high_scores() click to toggle source
# File examples/tetris.rb, line 376
def show_high_scores
  Glimmer::LibUI.queue_main do
    game_paused = !!@game.paused
    @game.paused = true
    if @game.high_scores.empty?
      high_scores_string = "No games have been scored yet."
    else
      high_scores_string = @game.high_scores.map do |high_score|
        "#{high_score.name} | Score: #{high_score.score} | Lines: #{high_score.lines} | Level: #{high_score.level}"
      end.join("\n")
    end
    msg_box('High Scores', high_scores_string)
    @game.paused = game_paused
  end
end
start_moving_tetrominos_down() click to toggle source
# File examples/tetris.rb, line 358
def start_moving_tetrominos_down
  unless @tetrominos_start_moving_down
    @tetrominos_start_moving_down = true
    tetromino_move = proc do
      @game.down! if !@game.game_over? && !@game.paused?
      Glimmer::LibUI.timer(@game.delay, repeat: false, &tetromino_move)
    end
    Glimmer::LibUI.timer(@game.delay, repeat: false, &tetromino_move)
  end
end