Class: Piece
- Inherits:
-
Object
- Object
- Piece
- Defined in:
- lib/tetris/piece.rb
Constant Summary collapse
- @@type_width =
4
Instance Method Summary collapse
- #drop(map) ⇒ Object
-
#initialize ⇒ Piece
constructor
A new instance of Piece.
- #move(x, y, map) ⇒ Object
- #rotate(map) ⇒ Object
- #squares(piece = current) ⇒ Object
- #update(map) ⇒ Object
Constructor Details
#initialize ⇒ Piece
Returns a new instance of Piece.
4 5 6 7 8 9 10 11 12 |
# File 'lib/tetris/piece.rb', line 4 def initialize @@types ||= load_pieces($Pieces) @type = @@types.rand_item @rotation = 0 @x = 0 @y = 0 @colour = $Colours.rand_item end |
Instance Method Details
#drop(map) ⇒ Object
30 31 32 |
# File 'lib/tetris/piece.rb', line 30 def drop(map) @y += 1 while not collision?(map, 0, 1) end |
#move(x, y, map) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/tetris/piece.rb', line 23 def move(x, y, map) unless collision?(map, x, y) @x += x @y += y end end |
#rotate(map) ⇒ Object
34 35 36 |
# File 'lib/tetris/piece.rb', line 34 def rotate(map) @rotation = next_rotation unless collision?(map, 0, 0, @type[next_rotation]) end |
#squares(piece = current) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/tetris/piece.rb', line 14 def squares(piece=current) Enumerator.new do |yielder| piece.each_with_index do |tile, i| x, y = coords(i) yielder.yield(@x+x, @y+y, @colour) if tile end end end |
#update(map) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/tetris/piece.rb', line 38 def update(map) if collision?(map, 0, 1) squares.each {|x, y, colour| map.set(x, y, colour)} initialize end end |