Closed dealingwith closed 3 years ago
adding an attr_accessor
for height
and width
to the Gameboard
class, then using gameboard.height
and gameboard.width
breaks everything--why?
gameboard.rb
class Gameboard < Matrix
# matrix is installed when you install ruby,
# no need to use gem.
# docs: https://ruby-doc.org/stdlib-2.5.1/libdoc/matrix/rdoc/Matrix.html
+ attr_accessor :height, :width
end
game.rb
:
- @gameboard_height = gameboard_height
- @gameboard_width = gameboard_width
@gameboard = Gameboard.zero(gameboard_height, gameboard_width)
- @squares = Gameboard.zero(gameboard_height, gameboard_width)
+ @gameboard.height = gameboard_height
+ @gameboard.width = gameboard_width
+ @squares = Matrix.zero(gameboard_height, gameboard_width)
create_tetromino()
end
@@ -36,13 +36,13 @@ class Game
end
def create_tetromino()
- @tetromino = Tetromino.new(@gameboard, @tetromino_shapes.sample, [0, 0], @gameboard_height, @gameboard_width)
+ @tetromino = Tetromino.new(@gameboard, @tetromino_shapes.sample, [0, 0], @gameboard.height, @gameboard.width)
@gameboard = tetromino.put_tetromino(@gameboard, [0, 0], tetromino.width, tetromino.height)
end
def draw(start_pos, size) # size is the side length of a square
- (0...@gameboard_width).each do |i|
- (0...@gameboard_height).each do |j|
+ (0...@gameboard.width).each do |i|
+ (0...@gameboard.height).each do |j|
if @squares[j, i] != 0
Traceback (most recent call last):
10: from tetris.rb:44:in `<main>'
9: from /Users/danielmiller/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/ruby2d-0.10.0/lib/ruby2d/dsl.rb:39:in `show'
8: from /Users/danielmiller/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/ruby2d-0.10.0/lib/ruby2d/window.rb:619:in `show'
7: from /Users/danielmiller/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/ruby2d-0.10.0/lib/ruby2d/window.rb:619:in `ext_show'
6: from /Users/danielmiller/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/ruby2d-0.10.0/lib/ruby2d/window.rb:563:in `update_callback'
5: from tetris.rb:28:in `block in <main>'
4: from /Users/danielmiller/code/2021-computer-adventures/game.rb:44:in `draw'
3: from /Users/danielmiller/code/2021-computer-adventures/game.rb:44:in `each'
2: from /Users/danielmiller/code/2021-computer-adventures/game.rb:45:in `block in draw'
1: from /Users/danielmiller/code/2021-computer-adventures/game.rb:45:in `each'
/Users/danielmiller/code/2021-computer-adventures/game.rb:47:in `block (2 levels) in draw': undefined method `remove' for nil:NilClass (NoMethodError)
closes #37
Gameboard just extends Matrix
is this even necessary/useful?