Permalink
Browse files

refactor rotation

  • Loading branch information...
1 parent ef6a1b9 commit 9e5bca28a99a1521e0452b0a5cdd6b3ccde7f159 @andrewfader committed Feb 12, 2013
Showing with 13 additions and 22 deletions.
  1. +1 −1 grid.rb
  2. +12 −21 piece.rb
View
2 grid.rb
@@ -48,7 +48,7 @@ def fit_to_grid(piece)
@to_fill << firstxy
shift_up = 0
- piece.current_shape.each do |shape_x,shape_y|
+ piece.shape.each do |shape_x,shape_y|
shapexy = [firstxy[0] + shape_x,firstxy[1] - shape_y + shift_up]
while @filled.include?(shapexy) || shapexy[1] < 0
shift_up += 1
View
33 piece.rb
@@ -4,31 +4,26 @@ class Piece
HORIZONTAL_MOVEMENT_AMOUNT = 28
VERTICAL_MOVEMENT_AMOUNT = 12
- attr_accessor :x, :y, :current_shape
+ attr_accessor :x, :y, :shape
def initialize(window, grid)
@image = Gosu::Image.new(window, 'block.png', false)
@x = 512
@y = Grid::GRID_TOP
- # type = [:I, :O].shuffle.first
- @type = [:I,:J,:L,:O,:S,:T,:Z].shuffle.first
- @rotation = 0
+ type = [:I,:J,:L,:O,:S,:T,:Z].shuffle.first
+ @shape = Piece.set_shape(type)
@grid = grid
@window = window
end
def draw
@image.draw(@x,@y,100)
- current_shape.each do |x,y|
+ @shape.each do |x,y|
@image.draw(@x+Grid::PX_PER_BLOCK*x,@y+Grid::PX_PER_BLOCK*y, 100)
end
end
- def current_shape
- Piece.set_shape(@type, @rotation)
- end
-
- def self.set_shape(type, rotation)
- shape = case type
+ def self.set_shape(type)
+ case type
when :I
[[1,0],[2,0],[3,0]]
when :J
@@ -44,21 +39,17 @@ def self.set_shape(type, rotation)
when :Z
[[-1,0],[-1,-1],[-2,-1]]
end
- rotation.times do
- shape = shape.map{|arr|(Matrix[arr] * Matrix[[0,-1],[1,0]]).to_a.flatten}
- end
- shape.to_a
end
def rotate_right
- @rotation < 3 ? @rotation += 1 : @rotation = 0
+ @shape = @shape.map{|arr|(Matrix[arr] * Matrix[[0,-1],[1,0]]).to_a.flatten}
end
def move(amount=VERTICAL_MOVEMENT_AMOUNT)
curx = Grid.nearest_x(@x)
cury = Grid.nearest_y(@y)
if (0..amount).any? { |test| @grid.filled.include?([curx,cury+test]) ||
- current_shape.any? { |shape| @grid.filled.include?([curx + shape[0],cury + test - shape[1]]) } }
+ @shape.any? { |shape| @grid.filled.include?([curx + shape[0],cury + test - shape[1]]) } }
@grid.fit_to_grid(self)
@window.piece = Piece.new(@window, @grid)
else
@@ -68,9 +59,9 @@ def move(amount=VERTICAL_MOVEMENT_AMOUNT)
def move_left(amount=HORIZONTAL_MOVEMENT_AMOUNT)
if @x > Grid::GRID_LEFT
- if current_shape.all? { |shape| @x + shape[0]*Grid::PX_PER_BLOCK > Grid::GRID_LEFT }
+ if @shape.all? { |shape| @x + shape[0]*Grid::PX_PER_BLOCK > Grid::GRID_LEFT }
if (0..amount).all? { |test| !@grid.filled.include?([Grid.nearest_x(@x-test),Grid.nearest_y(@y)]) &&
- current_shape.all? { |shape| !@grid.filled.include?([Grid.nearest_x(@x-test + shape[0]*Grid::PX_PER_BLOCK),Grid.nearest_y(@y + shape[1]*Grid::PX_PER_BLOCK)]) } }
+ @shape.all? { |shape| !@grid.filled.include?([Grid.nearest_x(@x-test + shape[0]*Grid::PX_PER_BLOCK),Grid.nearest_y(@y + shape[1]*Grid::PX_PER_BLOCK)]) } }
@x -= amount
end
end
@@ -79,9 +70,9 @@ def move_left(amount=HORIZONTAL_MOVEMENT_AMOUNT)
def move_right(amount=HORIZONTAL_MOVEMENT_AMOUNT)
if @x < Grid::GRID_RIGHT
- if current_shape.all? { |shape| @x + shape[0]*Grid::PX_PER_BLOCK < Grid::GRID_RIGHT }
+ if @shape.all? { |shape| @x + shape[0]*Grid::PX_PER_BLOCK < Grid::GRID_RIGHT }
if (0..amount).all? { |test| !@grid.filled.include?([Grid.nearest_x(@x+test),Grid.nearest_y(@y)]) &&
- current_shape.all? { |shape| !@grid.filled.include?([Grid.nearest_x(@x+test + shape[0]*Grid::PX_PER_BLOCK),Grid.nearest_y(@y + shape[1]*Grid::PX_PER_BLOCK)]) } }
+ @shape.all? { |shape| !@grid.filled.include?([Grid.nearest_x(@x+test + shape[0]*Grid::PX_PER_BLOCK),Grid.nearest_y(@y + shape[1]*Grid::PX_PER_BLOCK)]) } }
@x += amount
end
end

0 comments on commit 9e5bca2

Please sign in to comment.