class Game
Constants
- EMPTY
Attributes
current_difficulty[R]
game_over[R]
score[R]
Public Class Methods
new(window, x, y, height, width, block_size, difficulty)
click to toggle source
# File lib/game.rb, line 9 def initialize(window, x, y, height, width, block_size, difficulty) @window = window @x = x @y = y @height = height @width = width @block_size = block_size @difficulty = difficulty @current_difficulty = difficulty.zero? ? 1 : difficulty @data = Array.new(height) { Array.new(width, EMPTY) } @ticks = 0 @score = 0 @game_over = false create_new_player_entity end
Public Instance Methods
collision?()
click to toggle source
# File lib/game.rb, line 60 def collision? (0..@player.width - 1).each do |x| (0..@player.height - 1).each do |y| next unless @player.is_brick(x, y) return true if @data[@player.y + y][@player.x + x] != EMPTY end end false end
compare_color(a, b)
click to toggle source
# File lib/game.rb, line 93 def compare_color(a, b) a.alpha == b.alpha && a.red == b.red && a.green == b.green && a.blue == b.blue end
create_new_player_entity()
click to toggle source
# File lib/game.rb, line 40 def create_new_player_entity entities = [Triangle, Square, El, ElTwo, Tower, Zet, ZetTwo] random = rand(0..entities.size - 1) @player = entities[random].new(@width / 2, 0) @player.x = @player.x - @player.width / 2 if collision? @game_over = true @window.add_score(@score) end end
draw(context)
click to toggle source
# File lib/game.rb, line 139 def draw(context) context.draw_rect(@x, @y, @width * @block_size, @height * @block_size, Gosu::Color::GRAY) (0..@width - 1).each do |x| (0..@height - 1).each do |y| next if @data[y][x] == EMPTY draw_brick(context, x, y, @data[y][x]) end end draw_player(context) if game_over context.font.draw_text('Game over!', 250, 100, ZOrder::UI, 1.0, 1.0, Gosu::Color::YELLOW) end end
draw_brick(context, x, y, color)
click to toggle source
# File lib/game.rb, line 168 def draw_brick(context, x, y, color) outer_color = Gosu::Color.new(color.alpha / 2, color.red, color.green, color.blue) context.draw_rect(@x + x * @block_size, @y + y * @block_size, @block_size, @block_size, outer_color) padding = 2 context.draw_rect(@x + x * @block_size + padding, @y + y * @block_size + padding, @block_size - 2 * padding, @block_size - 2 * padding, color) end
draw_player(context)
click to toggle source
# File lib/game.rb, line 156 def draw_player(context) return if game_over (0..@player.width - 1).each do |x| (0..@player.height - 1).each do |y| next unless @player.is_brick(x, y) draw_brick(context, @player.x + x, @player.y + y, @player.color) end end end
fast_move()
click to toggle source
# File lib/game.rb, line 118 def fast_move @player.y += 1 while !collision? && is_within_bounds(0, 1) @player.y = @player.y - 1 move(0, 1) end
full_row?(row_id)
click to toggle source
# File lib/game.rb, line 100 def full_row?(row_id) @data[row_id].filter { |x| !compare_color(x, EMPTY) }.size == @width end
imprint_player_to_grid()
click to toggle source
# File lib/game.rb, line 104 def imprint_player_to_grid (0..@player.width - 1).each do |x| (0..@player.height - 1).each do |y| next unless @player.is_brick(x, y) @data[@player.y + y][@player.x + x] = @player.color end end (@player.y..@player.y + @player.height - 1).each do |row_id| score_and_update_row(row_id) if full_row?(row_id) end end
is_within_bounds(x_offset, y_offset)
click to toggle source
# File lib/game.rb, line 53 def is_within_bounds(x_offset, y_offset) @player.x + x_offset >= 0 && @player.x + x_offset + @player.width - 1 < @width && @player.y + y_offset >= 0 && @player.y + y_offset + @player.height - 1 < @height end
move(x_offset, y_offset)
click to toggle source
# File lib/game.rb, line 72 def move(x_offset, y_offset) return if !is_within_bounds(x_offset, y_offset) || game_over @player.x = @player.x + x_offset @player.y = @player.y + y_offset if collision? @player.x = @player.x + x_offset * -1 @player.y = @player.y + y_offset * -1 if y_offset == 1 imprint_player_to_grid create_new_player_entity end elsif @player.y + @player.height >= @height imprint_player_to_grid create_new_player_entity end end
rotate()
click to toggle source
# File lib/game.rb, line 125 def rotate @player.rotate @player.rotate_rollback if !is_within_bounds(0, 0) || collision? end
score_and_update_row(row_id)
click to toggle source
# File lib/game.rb, line 131 def score_and_update_row(row_id) @data.delete_at(row_id) @data.prepend(Array.new(@width, EMPTY)) @score += @current_difficulty @current_difficulty += 1 if @difficulty.zero? && @current_difficulty < Difficulty::MAX_DIFFICULTY end
update()
click to toggle source
# File lib/game.rb, line 29 def update return if game_over @ticks += 1 threshold = 60.0 - 5 * @current_difficulty if @ticks >= threshold move(0, 1) @ticks = 0 end end