/
tetris.rb
241 lines (206 loc) · 5.88 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require "starruby"
include StarRuby
BLOCK_SIZE = 32
FIELD_ROW = 20
FIELD_COL = 10
FIELD_W = BLOCK_SIZE * FIELD_COL
FIELD_H = BLOCK_SIZE * FIELD_ROW
WINDOW_ROW = 26
WINDOW_COL = 18
WINDOW_W = BLOCK_SIZE * WINDOW_COL
WINDOW_H = BLOCK_SIZE * WINDOW_ROW
RGBS = [[ 0, 255, 255],
[255, 255, 0],
[ 0, 255, 0],
[255, 0, 0],
[ 0, 0, 255],
[255, 127, 0],
[255, 0, 255]]
FONT = Font.new("/Library/Fonts/Arial Bold.ttf", 24)
class Texture
def draw_block(x, y, rgb)
render_rect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 1, BLOCK_SIZE - 1, Color.new(*rgb))
end
def draw_tetrimino(tetrimino)
return if !tetrimino
tetrimino.blocks.each_with_index do |row, r|
row.each_with_index do |col, c|
draw_block(tetrimino.x + c , tetrimino.y + r, RGBS[tetrimino.id]) if col == 1
end
end
end
def draw_field(field)
return if !field
alpha = (field.state == :live ? 255 : 64)
field.matrix.each_with_index do |row, r|
row.each_with_index do |col, c|
draw_block(c, r, RGBS[col] + [alpha]) if col != nil
end
end
end
def draw_text(str, col, row)
render_text(str, col * BLOCK_SIZE, row * BLOCK_SIZE, FONT, Color.new(0, 0, 0))
end
end
class Tetrimino
attr_reader :id, :state, :x, :y
@@minos = []
@@minos << [[0,0,0,0],
[1,1,1,1],
[0,0,0,0],
[0,0,0,0]]
@@minos << [[1,1],
[1,1]]
@@minos << [[0,1,1],
[1,1,0],
[0,0,0]]
@@minos << [[1,1,0],
[0,1,1],
[0,0,0]]
@@minos << [[1,0,0],
[1,1,1],
[0,0,0]]
@@minos << [[0,0,1],
[1,1,1],
[0,0,0]]
@@minos << [[0,1,0],
[1,1,1],
[0,0,0]]
def initialize(game, field)
@game = game
@field = field
@id = rand(0..6)
@blocks = @@minos[@id]
@x, @y, @angle = 3, 0, 0
@state = :live
@last_chance = 0
end
def blocks(angle = @angle)
case angle % 4
when 0
@blocks
when 1
@blocks.transpose.map(&:reverse) #右90度回転
when 2
@blocks.reverse.map(&:reverse) #180度回転
when 3
@blocks.transpose.reverse #左90度回転(右270度回転)
end
end
def rotate(dr)
if can_move?(0, 0, dr) then
@angle += dr
end
end
def side_step(dx)
if can_move?(dx, 0, 0) then
@x += dx
end
end
def fall(dy)
@last_chance -= 1
@y = @y.to_i if dy == 1
if can_move?(0, 1, 0) then
@y += dy
else
case
when @last_chance < 0
@last_chance = @game.fps / 2
when @last_chance == 0
@state = :dead
end
end
end
def can_move?(dx, dy, dr)
x = @x + dx
y = @y + dy
angle = @angle + dr
blocks(angle).each_with_index do |row, r|
row.each_with_index do |col, c|
if col == 1 then
if x + c < 0 ||
x + c >= FIELD_COL ||
y + r >= FIELD_ROW ||
@field.matrix[y + r][x + c] != nil then
return false
end
end
end
end
true
end
end
class Field
attr_reader :matrix, :state
def initialize(row, col)
@matrix = Array.new(row){Array.new(col)}
@state = :live
end
def import(tetrimino)
tetrimino.blocks.each_with_index do |row, r|
row.each_with_index do |col, c|
@matrix[tetrimino.y + r][tetrimino.x + c] = tetrimino.id if col == 1
end
end
end
def clear_lines
@matrix.reject!{|row| !row.include?(nil)}
deleted_line = FIELD_ROW - @matrix.size
deleted_line.times{@matrix.unshift(Array.new(10){nil})}
end
def freeze
@state = :dead
end
end
class Frame
def initialize(screen)
@screen = screen
@field_view = Texture.new(FIELD_W, FIELD_H)
@score_view = Texture.new(4 * BLOCK_SIZE, 1 * BLOCK_SIZE)
@lines_view = Texture.new(4 * BLOCK_SIZE, 1 * BLOCK_SIZE)
@next_view = Texture.new(FIELD_W, 2 * BLOCK_SIZE)
end
def update(sender)
@field_view.fill(Color.new(0, 0, 0, 128))
@score_view.fill(Color.new(0, 0, 0, 128))
@lines_view.fill(Color.new(0, 0, 0, 128))
@next_view.fill(Color.new(0, 0, 0, 128))
@field = sender.instance_variable_get(:@field)
@tetrimino = sender.instance_variable_get(:@tetrimino)
@field_view.draw_field(@field)
@field_view.draw_tetrimino(@tetrimino)
@screen.fill(Color.new(255, 255, 255))
@screen.render_texture(@field_view, 1 * BLOCK_SIZE, 5 * BLOCK_SIZE)
@screen.draw_text("SCORE", 13, 5)
@screen.render_texture(@score_view, 13 * BLOCK_SIZE, 6 * BLOCK_SIZE)
@screen.draw_text("LINES", 13, 9)
@screen.render_texture(@lines_view, 13 * BLOCK_SIZE, 10 * BLOCK_SIZE)
@screen.draw_text("NEXT", 4, 1)
@screen.render_texture(@next_view , 1 * BLOCK_SIZE, 2 * BLOCK_SIZE)
end
end
Game.run(WINDOW_W, WINDOW_H, :title => "tetris") do |game|
@field ||= Field.new(FIELD_ROW, FIELD_COL)
@tetrimino ||= Tetrimino.new(game, @field)
@frame ||= Frame.new(game.screen)
dx = 0
dy = 0.0625
dr = 0
break if Input.keys(:keyboard).include?(:escape)
dx = 1 if Input.keys(:keyboard, {:duration => 1, :delay => 3, :interval => 1}).include?(:right)
dx = -1 if Input.keys(:keyboard, {:duration => 1, :delay => 3, :interval => 1}).include?(:left)
dy = 1 if Input.keys(:keyboard, {:duration =>-1, :delay =>-1, :interval => 0}).include?(:down)
dr = 1 if Input.keys(:keyboard, {:duration => 1, :delay => 3, :interval => 3}).include?(:x)
dr = 3 if Input.keys(:keyboard, {:duration => 1, :delay => 3, :interval => 3}).include?(:z)
next if @field.state == :dead
@tetrimino.rotate(dr)
@tetrimino.side_step(dx)
@tetrimino.fall(dy)
if @tetrimino.state == :dead then
@field.import(@tetrimino)
@field.clear_lines
@field.freeze if @tetrimino.y <= 0
@tetrimino = nil
end
@frame.update(self)
end