-
Notifications
You must be signed in to change notification settings - Fork 5
/
tetris.rb
173 lines (149 loc) · 3.95 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
require "./starruby_ext"
module Tetris
ROWS = 20
COLS = 10
class View
WIDTH = 300
HEIGHT = 600
BLOCK_W = WIDTH / COLS
BLOCK_H = HEIGHT / ROWS
def self.size
[WIDTH, HEIGHT]
end
def initialize(model)
@model = model
end
def render(screen)
screen.clear
@model.bord.each do |col, rows|
rows.each do |row, val|
if @model.bord[col][row] != 0
screen.draw_block(row, col, BLOCK_W, BLOCK_H, val)
end
end
end
@model.current.each do |col, rows|
rows.each do |row, val|
if @model.current[col][row] != 0
screen.draw_block(row + @model.currentX, col + @model.currentY, BLOCK_W, BLOCK_H, val)
end
end
end
end
end
class Model
attr_reader :bord, :current, :currentX, :currentY
SHAPES = [
{ 0 => { 0 => 1, 1 => 1, 2 => 1, 3 => 1 } },
{ 0 => { 0 => 1, 1 => 1, 2 => 1, 3 => 0 },
1 => { 0 => 1, 1 => 0, 2 => 0, 3 => 0 } },
{ 0 => { 0 => 1, 1 => 1, 2 => 1, 3 => 0 },
1 => { 0 => 0, 1 => 0, 2 => 1, 3 => 0 } },
{ 0 => { 0 => 1, 1 => 1, 2 => 0, 3 => 0 },
1 => { 0 => 1, 1 => 1, 2 => 0, 3 => 0 } },
{ 0 => { 0 => 1, 1 => 1, 2 => 0, 3 => 0 },
1 => { 0 => 0, 1 => 1, 2 => 1, 3 => 0 } },
{ 0 => { 0 => 0, 1 => 1, 2 => 1, 3 => 0 },
1 => { 0 => 1, 1 => 1, 2 => 0, 3 => 0 } },
{ 0 => { 0 => 0, 1 => 1, 2 => 0, 3 => 0 },
1 => { 0 => 1, 1 => 1, 2 => 1, 3 => 0 } },
]
def initialize
create_bord
new_shape
end
def current_enum(&block)
(0..3).each{|y| (0..3).each{|x| block.call(y, x)}}
end
def create_bord
@bord = Hash.new {|k,v| k[v] = {}}
(0..(ROWS - 1)).each do |y|
(0..(COLS - 1)).each {|x| @bord[y][x] = 0}
end
end
def new_shape
shape = SHAPES.sample
id = (1..SHAPES.size).to_a.sample
@current = Hash.new {|k,v| k[v] = {}}
current_enum do |y, x|
@current[y][x] = shape[y] ? shape[y][x] == 1 ? id : 0 : 0
end
@currentX, @currentY = 5, 0
end
def tick
if valid
@currentY += 1
else
freez
clear_lines
new_shape
end
end
def freez
current_enum do |y, x|
@bord[y + @currentY][x + @currentX] = @current[y][x] if @current[y][x] != 0
end
@bord.delete(20)
end
def clear_lines
@bord.each do |y, rows|
unless rows.values.include?(0)
rows.each {|x, val| @bord[y][x] = 0}
y.downto(1).each do |i|
@bord[i] = @bord[i - 1]
end
end
end
end
def valid(current = nil, move_value = nil)
line = if move_value
->(x,y) {@bord[y + @currentY][x + @currentX + move_value]}
else
->(x,y) {@bord[y + @currentY + 1][x + @currentX]}
end
(current ||= @current).map do |y, rows|
rows.each {|x, val| return false if val != 0 && line[x, y] != 0}
end
end
def rotate
@_current = Hash.new {|k,v| k[v] = {}}
current_enum do |y, x|
@_current[y][x] = @current[3 - x][y]
end
@current = @_current if valid(@_current)
end
def key_press(op)
case op
when :right
@currentX += 1 if valid(nil, 1)
when :left
@currentX -= 1 if valid(nil, -1)
when :up
rotate
when :down
@currentY += 1 if valid
when :escape
exit 0
end
end
end
class Controller
def update(model)
[:right, :left, :up, :down, :escape].each do |op|
if Input.keys(:keyboard).include?(op)
model.key_press op
end
end
end
end
end
model = Tetris::Model.new
view = Tetris::View.new(model)
controller = Tetris::Controller.new
counter = 0
StarRuby::Game.run(*Tetris::View.size) do |game|
counter += 1
view.render(game.screen)
controller.update(model) if counter % 3 == 0
model.tick if counter % 6 == 0
end