-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.rb
162 lines (132 loc) · 2.98 KB
/
tetris.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'arduino-lights'
@colors = [
[ 253, 0, 0 ],
[ 253, 253, 0 ],
[ 0, 253, 0 ],
[ 0, 253, 253 ],
[ 0, 0, 253 ]
]
@shapes = [
[ [ 1, 1, 1, 1 ] ],
[ [ 1, 1, 0 ],
[ 0, 1, 1 ] ],
[ [ 1, 1, 1 ],
[ 0, 1, 0 ] ],
[ [ 1, 1, 1 ],
[ 0, 0, 1 ] ],
[ [ 1, 1 ],
[ 1, 1 ] ]
]
class Screen
def initialize
reset
end
def reset
@buffer = (0..11).collect { |c| (0..11).collect { |c| [0,0,0] } }
end
def set_pixel_xy(x, y, r, g, b)
@buffer[y][x] = [r, g, b]
end
def draw
@buffer.each_with_index do |row, y|
row.each_with_index do |pixel, x|
ArduinoLights::set_pixel_xy(x, y, pixel[0], pixel[1], pixel[2])
end
end
ArduinoLights::end_frame()
end
def collision(block, x, y)
block.shape.each_with_index do |row, sy|
next if sy + y < 0 or sy + y > 11
row.each_with_index do |pixel, sx|
if block.shape[sy][sx] != 0 and @buffer[y + sy][x + sx] != [0, 0, 0] and !block.contains?(x + sx, y + sy)
return true
end
end
end
false
end
end
class Block
attr_reader :shape
def initialize(shape, rotation, color, startx)
@shape = Block::transform(shape, rotation)
@x = [ startx, 11 - @shape[0].size ].min
@y = 1 - @shape.size
@color = color
end
def can_fall?(screen)
if @y + @shape.size >= 12
return false
end
return !screen.collision(self, @x, @y + 1)
end
def fall(screen)
if can_fall?(screen)
@y = @y + 1
end
end
def off_screen?
@y > 11
end
def contains?(x, y)
return (x - @x >= 0) && (y - @y >= 0) && (x - @x < @shape[0].size) && (y - @y < @shape.size) && (@shape[y - @y][x - @x] == 1)
end
def in_collision?(screen)
screen.collision(self, @x, @y)
end
def draw(screen, black = false)
color = black ? [0,0,0] : @color
@shape.each_with_index do |row, sy|
row.each_with_index do |pixel, sx|
if pixel == 1
if @x + sx < 12 and @y + sy < 12 and @y + sy >= 0
screen.set_pixel_xy(@x + sx, @y + sy,
color[0], color[1], color[2])
end
end
end
end
end
def clear(screen)
self.draw(screen, true)
end
class << self
def transform(input_shape, rotation)
shape = input_shape.clone
if (rotation % 2 == 1)
shape = shape.transpose
end
if (rotation >= 2)
shape = shape.collect { |row| row.reverse }
end
shape
end
end
end
screen = Screen.new
screen.draw
block = nil
frame = 0
while true do
if block.nil?
block = Block.new(@shapes.sample, rand(4), @colors.sample, rand(12))
if !block.can_fall?(screen)
screen = Screen.new
screen.draw
block = nil
next
end
end
if block.can_fall?(screen)
block.clear(screen)
block.fall(screen)
block.draw(screen) unless block.in_collision?(screen)
else
puts "Block hit the bottom. New block"
block = nil
end
screen.draw
frame = frame + 1
sleep(0.2)
end