-
Notifications
You must be signed in to change notification settings - Fork 1
/
piece.rb
61 lines (56 loc) · 1.44 KB
/
piece.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Piece
attr_accessor :x, :y, :type
def initialize(window)
@image = Gosu::Image.new(window, 'block.png', false)
@x = 512
@y = 200
@type = [:I,:J,:L,:O,:S,:T,:Z].sample
@rotation = 0
end
def draw
@image.draw(@x,@y,100)
shape[@rotation].each do |xy|
@image.draw(@x+28*xy[0],@y+28*xy[1], 100)
end
end
def shape
case @type
when :I
{0 => [[1,0],[2,0],[3,0]],
1 => [[0,-1],[0,-2],[0,-3]],
2 => [[-1,0],[-2,0],[-3,0]],
3 => [[0,1],[0,2],[0,3]] }
when :J
{0 => [[1,0],[2,0],[2,-1]],
1 => [[0,-1],[0,-2],[-1,-2]],
2 => [[-1,0],[-2,0],[-2,1]],
3 => [[0,1],[0,2],[1,2]] }
when :L
{0 => [[1,0],[2,0],[2,-1]],
1 => [[0,-1],[0,-2],[-1,-2]],
2 => [[-1,0],[-2,0],[-2,1]],
3 => [[0,1],[0,2],[1,2]] }
# when :O
# [0..3].each_with_index.inject({}) do |hash,index|
# hash.merge!({index => [[0,1],[1,1],[1,0]]})
# end
when :S
{0 => [[1,0],[0,-1],[-1,-1]],
1 => [[0,-1],[1,-1],[1,-2]],
2 => [[1,0],[0,-1],[-1,-1]],
3 => [[0,-1],[1,-1],[1,-2]] }
when :T
{0 => [[-1,0],[1,0],[0,-1]],
1 => [[0,-1],[0,1],[-1,0]],
2 => [[1,0],[-1,0],[0,1]],
3 => [[0,1],[0,-1],[1,0]] }
when :Z
{0 => [[-1,0],[-1,-1],[-2,-1]],
1 => [[0,1],[1,1],[1,2]],
2 => [[-1,0],[-1,-1],[-2,-1]],
3 => [[0,1],[1,1],[1,2]] }
end
end
def move
end
end