-
Notifications
You must be signed in to change notification settings - Fork 6
/
tetris.rb
193 lines (169 loc) · 5.78 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
# Copyright (c) 2007-2024 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'glimmer-dsl-swt'
require_relative 'tetris/model/game'
require_relative 'tetris/view/playfield'
require_relative 'tetris/view/score_lane'
require_relative 'tetris/view/high_score_dialog'
require_relative 'tetris/view/tetris_menu_bar'
# Tetris App View Custom Shell (represents `tetris` keyword)
class Tetris
include Glimmer::UI::CustomShell
BLOCK_SIZE = 25
FONT_NAME = 'Menlo'
FONT_TITLE_HEIGHT = 32
FONT_TITLE_STYLE = :bold
BEVEL_CONSTANT = 20
option :playfield_width, default: Model::Game::PLAYFIELD_WIDTH
option :playfield_height, default: Model::Game::PLAYFIELD_HEIGHT
attr_reader :game
before_body do
@mutex = Mutex.new
@game = Model::Game.new(playfield_width, playfield_height)
@game.configure_beeper do
display.beep
end
Display.app_name = 'Glimmer Tetris'
display {
on_swt_keydown do |key_event|
case key_event.keyCode
when swt(:arrow_down), 's'.bytes.first
if OS.mac?
game.down!
else
# rate limit downs in Windows/Linux as they go too fast when key is held
@queued_downs ||= 0
@queued_downs += 1
async_exec do
game.down! if @queued_downs < 3
@queued_downs -= 1
end
end
when swt(:arrow_up)
case game.up_arrow_action
when :instant_down
game.down!(instant: true)
when :rotate_right
game.rotate!(:right)
when :rotate_left
game.rotate!(:left)
end
when swt(:arrow_left), 'a'.bytes.first
game.left!
when swt(:arrow_right), 'd'.bytes.first
game.right!
when swt(:shift), swt(:alt)
if key_event.keyLocation == swt(:right) # right key
game.rotate!(:right)
elsif key_event.keyLocation == swt(:left) # left key
game.rotate!(:left)
end
end
end
# if running in app mode, set the Mac app about dialog (ignored in platforms)
on_about do
show_about_dialog
end
on_quit do
exit(0)
end
}
end
after_body do
observe(@game, :game_over) do |game_over|
if game_over
show_high_score_dialog
else
start_moving_tetrominos_down
end
end
observe(@game, :show_high_scores) do |show_high_scores|
if show_high_scores
show_high_score_dialog
else
@high_score_dialog.close unless @high_score_dialog.nil? || @high_score_dialog.disposed? || !@high_score_dialog.visible?
end
end
@game.start!
end
body {
shell(:no_resize) {
grid_layout {
num_columns 2
make_columns_equal_width false
margin_width 0
margin_height 0
horizontal_spacing 0
}
text 'Glimmer Tetris'
minimum_size 475, 500
image tetris_icon
tetris_menu_bar(game: game)
playfield(game_playfield: game.playfield, playfield_width: playfield_width, playfield_height: playfield_height, block_size: BLOCK_SIZE)
score_lane(game: game, block_size: BLOCK_SIZE) {
layout_data(:fill, :fill, true, true)
}
}
}
def tetris_icon
icon_block_size = 64
icon_bevel_size = icon_block_size.to_f / 25.to_f
icon_bevel_pixel_size = 0.16*icon_block_size.to_f
icon_size = 8
icon_pixel_size = icon_block_size * icon_size
image(icon_pixel_size, icon_pixel_size) {
icon_size.times do |row|
icon_size.times do |column|
colored = row >= 1 && column.between?(1, 6)
color = colored ? color(([:white] + Model::Tetromino::LETTER_COLORS.values).sample) : color(:white)
x = column * icon_block_size
y = row * icon_block_size
bevel(x: x, y: y, base_color: color, size: icon_block_size)
end
end
}
end
def start_moving_tetrominos_down
Thread.new do
@mutex.synchronize do
loop do
time = Time.now
sleep @game.delay
break if @game.game_over? || body_root.disposed?
# ensure entire game tetromino down movement happens as one GUI update event with sync_exec (to avoid flicker/stutter)
sync_exec { @game.down! unless @game.paused? }
end
end
end
end
def show_high_score_dialog
return if @high_score_dialog&.visible?
@high_score_dialog = high_score_dialog(parent_shell: body_root, game: @game) if @high_score_dialog.nil? || @high_score_dialog.disposed?
@high_score_dialog.show
end
def show_about_dialog
message_box {
text 'Glimmer Tetris'
message "Glimmer Tetris\n\nGlimmer DSL for SWT Sample\n\nCopyright (c) 2007-2024 Andy Maleh"
}.open
end
end
Tetris.launch