glimmer-dsl-libui 0.2.16 → 0.2.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +132 -2
- data/VERSION +1 -1
- data/bin/girb +0 -0
- data/examples/meta_example.rb +3 -1
- data/examples/tetris/model/block.rb +48 -0
- data/examples/tetris/model/game.rb +306 -0
- data/examples/tetris/model/past_game.rb +39 -0
- data/examples/tetris/model/tetromino.rb +329 -0
- data/examples/tetris.rb +124 -0
- data/glimmer-dsl-libui.gemspec +0 -0
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d6e095fec39866bb2bfe5b9d1295a642dfaea6d8014c0fe99e56d038435a4589
|
|
4
|
+
data.tar.gz: c884e58ae378f0a453f0f40ec5e5f61f96b786894eeabf74d954c3fd58329d41
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b4148815da2f069acc297de2b882277cb0026ff2cb9f8175a78e0d8b54cabaec6afc03db5a3be1b8abca89ba3b29727c5b9b0aa3f3b659ae4e36838074eaed9c
|
|
7
|
+
data.tar.gz: 255e3885c541a8bb73a5887ef21e1ec2dcbd8a288c07973cf7c3ae6e17dd31121fa784fc2e8af0a8b5c0ed974831e59ca752f3617a1821c6ec73e0faf6489b8e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for LibUI 0.2.
|
|
1
|
+
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for LibUI 0.2.17
|
|
2
2
|
## Prerequisite-Free Ruby Desktop Development GUI Library
|
|
3
3
|
[](http://badge.fury.io/rb/glimmer-dsl-libui)
|
|
4
4
|
[](https://gitter.im/AndyObtiva/glimmer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
@@ -284,6 +284,7 @@ Other [Glimmer](https://rubygems.org/gems/glimmer) DSL gems you might be interes
|
|
|
284
284
|
- [Basic Draw Text](#basic-draw-text)
|
|
285
285
|
- [Custom Draw Text](#custom-draw-text)
|
|
286
286
|
- [Method-Based Custom Keyword](#method-based-custom-keyword)
|
|
287
|
+
- [Tetris](#tetris)
|
|
287
288
|
- [Applications](#applications)
|
|
288
289
|
- [Manga2PDF](#manga2pdf)
|
|
289
290
|
- [Befunge98 GUI](#befunge98-gui)
|
|
@@ -376,7 +377,7 @@ gem install glimmer-dsl-libui
|
|
|
376
377
|
Or install via Bundler `Gemfile`:
|
|
377
378
|
|
|
378
379
|
```ruby
|
|
379
|
-
gem 'glimmer-dsl-libui', '~> 0.2.
|
|
380
|
+
gem 'glimmer-dsl-libui', '~> 0.2.17'
|
|
380
381
|
```
|
|
381
382
|
|
|
382
383
|
Add `require 'glimmer-dsl-libui'` at the top, and then `include Glimmer` into the top-level main object for testing or into an actual class for serious usage.
|
|
@@ -1076,6 +1077,7 @@ window('Method-Based Custom Keyword') {
|
|
|
1076
1077
|
- `table` controls on Windows intentionally get an extra empty row at the end because if any row were to be deleted for the first time, double-deletion happens due to an issue in [libui](https://github.com/andlabs/libui) on Windows.
|
|
1077
1078
|
- `table` `progress_bar` column on Windows cannot be updated with a positive value if it started initially with `-1` (it ignores update to avoid crashing due to an issue in [libui](https://github.com/andlabs/libui) on Windows.
|
|
1078
1079
|
- It seems that [libui](https://github.com/andlabs/libui) does not support nesting multiple `area` controls under a `grid` as only the first one shows up in that scenario. To workaround that limitation, use a `vertical_box` with nested `horizontal_box`s instead to include multiple `area`s in a GUI.
|
|
1080
|
+
- As per the code of [examples/basic_transform.rb](#basic-transform), Windows requires different ordering of transforms than Mac and Linux.
|
|
1079
1081
|
|
|
1080
1082
|
### Original API
|
|
1081
1083
|
|
|
@@ -6198,6 +6200,134 @@ window('Method-Based Custom Keyword') {
|
|
|
6198
6200
|
}.show
|
|
6199
6201
|
```
|
|
6200
6202
|
|
|
6203
|
+
### Tetris
|
|
6204
|
+
|
|
6205
|
+
[examples/tetris.rb](examples/tetris.rb)
|
|
6206
|
+
|
|
6207
|
+
Run with this command from the root of the project if you cloned the project:
|
|
6208
|
+
|
|
6209
|
+
```
|
|
6210
|
+
ruby -r './lib/glimmer-dsl-libui' examples/tetris.rb
|
|
6211
|
+
```
|
|
6212
|
+
|
|
6213
|
+
Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
|
|
6214
|
+
|
|
6215
|
+
```
|
|
6216
|
+
ruby -r glimmer-dsl-libui -e "require 'examples/tetris'"
|
|
6217
|
+
```
|
|
6218
|
+
|
|
6219
|
+
Mac
|
|
6220
|
+
|
|
6221
|
+

|
|
6222
|
+
|
|
6223
|
+
New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
|
|
6224
|
+
|
|
6225
|
+
```ruby
|
|
6226
|
+
require 'glimmer-dsl-libui'
|
|
6227
|
+
|
|
6228
|
+
require_relative 'tetris/model/game'
|
|
6229
|
+
|
|
6230
|
+
class Tetris
|
|
6231
|
+
include Glimmer
|
|
6232
|
+
|
|
6233
|
+
BLOCK_SIZE = 25
|
|
6234
|
+
BEVEL_CONSTANT = 20
|
|
6235
|
+
|
|
6236
|
+
attr_reader :game
|
|
6237
|
+
|
|
6238
|
+
def initialize
|
|
6239
|
+
@game = Model::Game.new
|
|
6240
|
+
create_gui
|
|
6241
|
+
register_observers
|
|
6242
|
+
end
|
|
6243
|
+
|
|
6244
|
+
def launch
|
|
6245
|
+
@game.start!
|
|
6246
|
+
@main_window.show
|
|
6247
|
+
end
|
|
6248
|
+
|
|
6249
|
+
def create_gui
|
|
6250
|
+
@main_window = window('Glimmer Tetris', Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE) {
|
|
6251
|
+
playfield(playfield_width: Model::Game::PLAYFIELD_WIDTH, playfield_height: Model::Game::PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
|
|
6252
|
+
}
|
|
6253
|
+
end
|
|
6254
|
+
|
|
6255
|
+
def register_observers
|
|
6256
|
+
Glimmer::DataBinding::Observer.proc do |game_over|
|
|
6257
|
+
if game_over
|
|
6258
|
+
show_game_over_dialog
|
|
6259
|
+
else
|
|
6260
|
+
start_moving_tetrominos_down
|
|
6261
|
+
end
|
|
6262
|
+
end.observe(@game, :game_over)
|
|
6263
|
+
|
|
6264
|
+
Model::Game::PLAYFIELD_HEIGHT.times do |row|
|
|
6265
|
+
Model::Game::PLAYFIELD_HEIGHT.times do |column|
|
|
6266
|
+
Glimmer::DataBinding::Observer.proc do |new_color|
|
|
6267
|
+
@blocks[row][column].fill = new_color
|
|
6268
|
+
end.observe(@game.playfield[row][column], :color)
|
|
6269
|
+
end
|
|
6270
|
+
end
|
|
6271
|
+
end
|
|
6272
|
+
|
|
6273
|
+
def playfield(playfield_width: , playfield_height: , block_size: )
|
|
6274
|
+
area {
|
|
6275
|
+
@blocks = playfield_height.times.map do |row|
|
|
6276
|
+
playfield_width.times.map do |column|
|
|
6277
|
+
block(row: row, column: column, block_size: block_size)
|
|
6278
|
+
end
|
|
6279
|
+
end
|
|
6280
|
+
|
|
6281
|
+
on_key_down do |key_event|
|
|
6282
|
+
case key_event
|
|
6283
|
+
in ext_key: :down
|
|
6284
|
+
game.down!
|
|
6285
|
+
in ext_key: :up
|
|
6286
|
+
case game.up_arrow_action
|
|
6287
|
+
when :instant_down
|
|
6288
|
+
game.down!(instant: true)
|
|
6289
|
+
when :rotate_right
|
|
6290
|
+
game.rotate!(:right)
|
|
6291
|
+
when :rotate_left
|
|
6292
|
+
game.rotate!(:left)
|
|
6293
|
+
end
|
|
6294
|
+
in ext_key: :left
|
|
6295
|
+
game.left!
|
|
6296
|
+
in ext_key: :right
|
|
6297
|
+
game.right!
|
|
6298
|
+
in modifier: :shift
|
|
6299
|
+
game.rotate!(:right)
|
|
6300
|
+
in modifier: :control
|
|
6301
|
+
game.rotate!(:left)
|
|
6302
|
+
else
|
|
6303
|
+
# Do Nothing
|
|
6304
|
+
end
|
|
6305
|
+
end
|
|
6306
|
+
}
|
|
6307
|
+
end
|
|
6308
|
+
|
|
6309
|
+
def block(row: , column: , block_size: )
|
|
6310
|
+
path {
|
|
6311
|
+
square(column * block_size, row * block_size, block_size)
|
|
6312
|
+
|
|
6313
|
+
fill Model::Block::COLOR_CLEAR
|
|
6314
|
+
}
|
|
6315
|
+
end
|
|
6316
|
+
|
|
6317
|
+
def start_moving_tetrominos_down
|
|
6318
|
+
Glimmer::LibUI.timer(@game.delay) do
|
|
6319
|
+
@game.down! if !@game.game_over? && !@game.paused?
|
|
6320
|
+
end
|
|
6321
|
+
end
|
|
6322
|
+
|
|
6323
|
+
def show_game_over_dialog
|
|
6324
|
+
msg_box('Game Over', "Score: #{@game.high_scores.first.score}")
|
|
6325
|
+
end
|
|
6326
|
+
end
|
|
6327
|
+
|
|
6328
|
+
Tetris.new.launch
|
|
6329
|
+
```
|
|
6330
|
+
|
|
6201
6331
|
## Applications
|
|
6202
6332
|
|
|
6203
6333
|
Here are some applications built with [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui)
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.17
|
data/bin/girb
CHANGED
|
File without changes
|
data/examples/meta_example.rb
CHANGED
|
@@ -11,7 +11,7 @@ class MetaExample
|
|
|
11
11
|
|
|
12
12
|
def examples
|
|
13
13
|
if @examples.nil?
|
|
14
|
-
example_files = Dir.glob(File.join(File.expand_path('.', __dir__), '
|
|
14
|
+
example_files = Dir.glob(File.join(File.expand_path('.', __dir__), '*.rb'))
|
|
15
15
|
example_file_names = example_files.map { |f| File.basename(f, '.rb') }
|
|
16
16
|
example_file_names = example_file_names.reject { |f| f == 'meta_example' || f.match(/\d$/) }
|
|
17
17
|
@examples = example_file_names.map { |f| f.underscore.titlecase }
|
|
@@ -110,6 +110,8 @@ class MetaExample
|
|
|
110
110
|
FileUtils.mkdir_p(parent_dir)
|
|
111
111
|
example_file = File.join(parent_dir, "#{selected_example.underscore}.rb")
|
|
112
112
|
File.write(example_file, @code_entry.text)
|
|
113
|
+
example_supporting_directory = File.expand_path(selected_example.underscore, __dir__)
|
|
114
|
+
FileUtils.cp_r(example_supporting_directory, parent_dir) if Dir.exist?(example_supporting_directory)
|
|
113
115
|
FileUtils.cp_r(File.expand_path('../icons', __dir__), File.dirname(parent_dir))
|
|
114
116
|
FileUtils.cp_r(File.expand_path('../sounds', __dir__), File.dirname(parent_dir))
|
|
115
117
|
run_example(example_file)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
class Tetris
|
|
23
|
+
module Model
|
|
24
|
+
class Block
|
|
25
|
+
COLOR_CLEAR = :white
|
|
26
|
+
|
|
27
|
+
attr_accessor :color
|
|
28
|
+
|
|
29
|
+
# Initializes with color. Default color (gray) signifies an empty block
|
|
30
|
+
def initialize(color = COLOR_CLEAR)
|
|
31
|
+
@color = color
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Clears block color. `quietly` option indicates if it should not notify observers by setting value quietly via variable not attribute writer.
|
|
35
|
+
def clear
|
|
36
|
+
self.color = COLOR_CLEAR unless self.color == COLOR_CLEAR
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def clear?
|
|
40
|
+
self.color == COLOR_CLEAR
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def occupied?
|
|
44
|
+
!clear?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require 'fileutils'
|
|
23
|
+
require 'json'
|
|
24
|
+
require 'glimmer/data_binding/observer'
|
|
25
|
+
require 'glimmer/config'
|
|
26
|
+
|
|
27
|
+
require_relative 'block'
|
|
28
|
+
require_relative 'tetromino'
|
|
29
|
+
require_relative 'past_game'
|
|
30
|
+
|
|
31
|
+
class Tetris
|
|
32
|
+
module Model
|
|
33
|
+
class Game
|
|
34
|
+
PLAYFIELD_WIDTH = 10
|
|
35
|
+
PLAYFIELD_HEIGHT = 20
|
|
36
|
+
PREVIEW_PLAYFIELD_WIDTH = 4
|
|
37
|
+
PREVIEW_PLAYFIELD_HEIGHT = 2
|
|
38
|
+
SCORE_MULTIPLIER = {1 => 40, 2 => 100, 3 => 300, 4 => 1200}
|
|
39
|
+
|
|
40
|
+
attr_reader :playfield_width, :playfield_height
|
|
41
|
+
attr_accessor :game_over, :paused, :preview_tetromino, :lines, :score, :level, :high_scores, :beeping, :added_high_score, :show_high_scores, :up_arrow_action
|
|
42
|
+
alias game_over? game_over
|
|
43
|
+
alias paused? paused
|
|
44
|
+
alias beeping? beeping
|
|
45
|
+
alias added_high_score? added_high_score
|
|
46
|
+
|
|
47
|
+
def initialize(playfield_width = PLAYFIELD_WIDTH, playfield_height = PLAYFIELD_HEIGHT)
|
|
48
|
+
@playfield_width = playfield_width
|
|
49
|
+
@playfield_height = playfield_height
|
|
50
|
+
@high_scores = []
|
|
51
|
+
@show_high_scores = false
|
|
52
|
+
@beeping = true
|
|
53
|
+
@up_arrow_action = :rotate_left
|
|
54
|
+
load_high_scores!
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def configure_beeper(&beeper)
|
|
58
|
+
@beeper = beeper
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def game_in_progress?
|
|
62
|
+
!game_over? && !paused?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def start!
|
|
66
|
+
self.show_high_scores = false
|
|
67
|
+
self.paused = false
|
|
68
|
+
self.level = 1
|
|
69
|
+
self.score = 0
|
|
70
|
+
self.lines = 0
|
|
71
|
+
reset_playfield
|
|
72
|
+
reset_preview_playfield
|
|
73
|
+
reset_tetrominoes
|
|
74
|
+
preview_next_tetromino!
|
|
75
|
+
consider_adding_tetromino
|
|
76
|
+
self.game_over = false
|
|
77
|
+
end
|
|
78
|
+
alias restart! start!
|
|
79
|
+
|
|
80
|
+
def game_over!
|
|
81
|
+
add_high_score!
|
|
82
|
+
beep
|
|
83
|
+
self.game_over = true
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def clear_high_scores!
|
|
87
|
+
high_scores.clear
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def add_high_score!
|
|
91
|
+
self.added_high_score = true
|
|
92
|
+
high_scores.prepend(PastGame.new("Player #{high_scores.count + 1}", score, lines, level))
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def save_high_scores!
|
|
96
|
+
high_score_file_content = @high_scores.map {|past_game| past_game.to_a.join("\t") }.join("\n")
|
|
97
|
+
FileUtils.mkdir_p(tetris_dir)
|
|
98
|
+
File.write(tetris_high_score_file, high_score_file_content)
|
|
99
|
+
rescue => e
|
|
100
|
+
# Fail safely by keeping high scores in memory if unable to access disk
|
|
101
|
+
Glimmer::Config.logger.error {"Failed to save high scores in: #{tetris_high_score_file}\n#{e.full_message}"}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def load_high_scores!
|
|
105
|
+
if File.exist?(tetris_high_score_file)
|
|
106
|
+
self.high_scores = File.read(tetris_high_score_file).split("\n").map {|line| PastGame.new(*line.split("\t")) }
|
|
107
|
+
end
|
|
108
|
+
rescue => e
|
|
109
|
+
# Fail safely by keeping high scores in memory if unable to access disk
|
|
110
|
+
Glimmer::Config.logger.error {"Failed to load high scores from: #{tetris_high_score_file}\n#{e.full_message}"}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def tetris_dir
|
|
114
|
+
@tetris_dir ||= File.join(File.expand_path('~'), '.glimmer-tetris')
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def tetris_high_score_file
|
|
118
|
+
File.join(tetris_dir, "high_scores.txt")
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def down!(instant: false)
|
|
122
|
+
return unless game_in_progress?
|
|
123
|
+
current_tetromino.down!(instant: instant)
|
|
124
|
+
game_over! if current_tetromino.row <= 0 && current_tetromino.stopped?
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def right!
|
|
128
|
+
return unless game_in_progress?
|
|
129
|
+
current_tetromino.right!
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def left!
|
|
133
|
+
return unless game_in_progress?
|
|
134
|
+
current_tetromino.left!
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def rotate!(direction)
|
|
138
|
+
return unless game_in_progress?
|
|
139
|
+
current_tetromino.rotate!(direction)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def current_tetromino
|
|
143
|
+
tetrominoes.last
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def tetrominoes
|
|
147
|
+
@tetrominoes ||= reset_tetrominoes
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Returns blocks in the playfield
|
|
151
|
+
def playfield
|
|
152
|
+
@playfield ||= @original_playfield = @playfield_height.times.map do
|
|
153
|
+
@playfield_width.times.map do
|
|
154
|
+
Block.new
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Executes a hypothetical scenario without truly changing playfield permanently
|
|
160
|
+
def hypothetical(&block)
|
|
161
|
+
@playfield = hypothetical_playfield
|
|
162
|
+
block.call
|
|
163
|
+
@playfield = @original_playfield
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Returns whether currently executing a hypothetical scenario
|
|
167
|
+
def hypothetical?
|
|
168
|
+
@playfield != @original_playfield
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def hypothetical_playfield
|
|
172
|
+
@playfield_height.times.map { |row|
|
|
173
|
+
@playfield_width.times.map { |column|
|
|
174
|
+
playfield[row][column].clone
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def preview_playfield
|
|
180
|
+
@preview_playfield ||= PREVIEW_PLAYFIELD_HEIGHT.times.map {|row|
|
|
181
|
+
PREVIEW_PLAYFIELD_WIDTH.times.map {|column|
|
|
182
|
+
Block.new
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def preview_next_tetromino!
|
|
188
|
+
self.preview_tetromino = Tetromino.new(self)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def calculate_score!(eliminated_lines)
|
|
192
|
+
new_score = SCORE_MULTIPLIER[eliminated_lines] * (level + 1)
|
|
193
|
+
self.score += new_score
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def level_up!
|
|
197
|
+
self.level += 1 if lines >= self.level*10
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def delay
|
|
201
|
+
[1.1 - (level.to_i * 0.1), 0.001].max
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def beep
|
|
205
|
+
@beeper&.call if beeping
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def instant_down_on_up=(value)
|
|
209
|
+
self.up_arrow_action = :instant_down if value
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def instant_down_on_up
|
|
213
|
+
self.up_arrow_action == :instant_down
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def rotate_right_on_up=(value)
|
|
217
|
+
self.up_arrow_action = :rotate_right if value
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def rotate_right_on_up
|
|
221
|
+
self.up_arrow_action == :rotate_right
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def rotate_left_on_up=(value)
|
|
225
|
+
self.up_arrow_action = :rotate_left if value
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def rotate_left_on_up
|
|
229
|
+
self.up_arrow_action == :rotate_left
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def reset_tetrominoes
|
|
233
|
+
@tetrominoes = []
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def reset_playfield
|
|
237
|
+
playfield.each do |row|
|
|
238
|
+
row.each do |block|
|
|
239
|
+
block.clear
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def reset_preview_playfield
|
|
245
|
+
preview_playfield.each do |row|
|
|
246
|
+
row.each do |block|
|
|
247
|
+
block.clear
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def consider_adding_tetromino
|
|
253
|
+
if tetrominoes.empty? || current_tetromino.stopped?
|
|
254
|
+
preview_tetromino.launch!
|
|
255
|
+
preview_next_tetromino!
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def consider_eliminating_lines
|
|
260
|
+
eliminated_lines = 0
|
|
261
|
+
playfield.each_with_index do |row, playfield_row|
|
|
262
|
+
if row.all? {|block| !block.clear?}
|
|
263
|
+
eliminated_lines += 1
|
|
264
|
+
shift_blocks_down_above_row(playfield_row)
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
if eliminated_lines > 0
|
|
268
|
+
beep
|
|
269
|
+
self.lines += eliminated_lines
|
|
270
|
+
level_up!
|
|
271
|
+
calculate_score!(eliminated_lines)
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def playfield_remaining_heights(tetromino = nil)
|
|
276
|
+
@playfield_width.times.map do |playfield_column|
|
|
277
|
+
bottom_most_block = tetromino.bottom_most_block_for_column(playfield_column)
|
|
278
|
+
(playfield.each_with_index.detect do |row, playfield_row|
|
|
279
|
+
!row[playfield_column].clear? &&
|
|
280
|
+
(
|
|
281
|
+
tetromino.nil? ||
|
|
282
|
+
bottom_most_block.nil? ||
|
|
283
|
+
(playfield_row > tetromino.row + bottom_most_block[:row_index])
|
|
284
|
+
)
|
|
285
|
+
end || [nil, @playfield_height])[1]
|
|
286
|
+
end.to_a
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
private
|
|
290
|
+
|
|
291
|
+
def shift_blocks_down_above_row(row)
|
|
292
|
+
row.downto(0) do |playfield_row|
|
|
293
|
+
playfield[playfield_row].each_with_index do |block, playfield_column|
|
|
294
|
+
previous_row = playfield[playfield_row - 1]
|
|
295
|
+
previous_block = previous_row[playfield_column]
|
|
296
|
+
block.color = previous_block.color unless block.color == previous_block.color
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
playfield[0].each(&:clear)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
class Tetris
|
|
23
|
+
module Model
|
|
24
|
+
class PastGame
|
|
25
|
+
attr_accessor :name, :score, :lines, :level
|
|
26
|
+
|
|
27
|
+
def initialize(name, score, lines, level)
|
|
28
|
+
@name = name
|
|
29
|
+
@score = score.to_i
|
|
30
|
+
@lines = lines.to_i
|
|
31
|
+
@level = level.to_i
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_a
|
|
35
|
+
[@name, @score, @lines, @level]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require_relative 'block'
|
|
23
|
+
|
|
24
|
+
require 'matrix'
|
|
25
|
+
|
|
26
|
+
class Tetris
|
|
27
|
+
module Model
|
|
28
|
+
class Tetromino
|
|
29
|
+
ORIENTATIONS = [:north, :east, :south, :west]
|
|
30
|
+
|
|
31
|
+
LETTER_COLORS = {
|
|
32
|
+
I: :cyan,
|
|
33
|
+
J: :blue,
|
|
34
|
+
L: :dark_yellow,
|
|
35
|
+
O: :yellow,
|
|
36
|
+
S: :green,
|
|
37
|
+
T: :magenta,
|
|
38
|
+
Z: :red,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
attr_reader :game, :letter, :preview
|
|
42
|
+
alias preview? preview
|
|
43
|
+
attr_accessor :orientation, :blocks, :row, :column
|
|
44
|
+
|
|
45
|
+
def initialize(game)
|
|
46
|
+
@game = game
|
|
47
|
+
@letter = LETTER_COLORS.keys.sample
|
|
48
|
+
@orientation = :north
|
|
49
|
+
@blocks = default_blocks
|
|
50
|
+
@preview = true
|
|
51
|
+
new_row = 0
|
|
52
|
+
new_column = (Model::Game::PREVIEW_PLAYFIELD_WIDTH - width)/2
|
|
53
|
+
update_playfield(new_row, new_column)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def playfield
|
|
57
|
+
@preview ? game.preview_playfield : game.playfield
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def launch!
|
|
61
|
+
remove_from_playfield
|
|
62
|
+
@preview = false
|
|
63
|
+
new_row = 1 - height
|
|
64
|
+
new_column = (game.playfield_width - width)/2
|
|
65
|
+
update_playfield(new_row, new_column)
|
|
66
|
+
game.tetrominoes << self
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def update_playfield(new_row = nil, new_column = nil)
|
|
70
|
+
remove_from_playfield
|
|
71
|
+
if !new_row.nil? && !new_column.nil?
|
|
72
|
+
@row = new_row
|
|
73
|
+
@column = new_column
|
|
74
|
+
add_to_playfield
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def add_to_playfield
|
|
79
|
+
update_playfield_block do |playfield_row, playfield_column, row_index, column_index|
|
|
80
|
+
playfield[playfield_row][playfield_column].color = blocks[row_index][column_index].color if playfield_row >= 0 && playfield[playfield_row][playfield_column]&.clear? && !blocks[row_index][column_index].clear? && playfield[playfield_row][playfield_column].color != blocks[row_index][column_index].color
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def remove_from_playfield
|
|
85
|
+
return if @row.nil? || @column.nil?
|
|
86
|
+
update_playfield_block do |playfield_row, playfield_column, row_index, column_index|
|
|
87
|
+
playfield[playfield_row][playfield_column].clear if playfield_row >= 0 && !blocks[row_index][column_index].clear? && playfield[playfield_row][playfield_column]&.color == color
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def stopped?
|
|
92
|
+
return true if @stopped || @preview
|
|
93
|
+
playfield_remaining_heights = game.playfield_remaining_heights(self)
|
|
94
|
+
result = bottom_most_blocks.any? do |bottom_most_block|
|
|
95
|
+
playfield_column = @column + bottom_most_block[:column_index]
|
|
96
|
+
playfield_remaining_heights[playfield_column] &&
|
|
97
|
+
@row + bottom_most_block[:row_index] >= playfield_remaining_heights[playfield_column] - 1
|
|
98
|
+
end
|
|
99
|
+
if result && !game.hypothetical?
|
|
100
|
+
@stopped = result
|
|
101
|
+
game.consider_eliminating_lines
|
|
102
|
+
@game.consider_adding_tetromino
|
|
103
|
+
end
|
|
104
|
+
result
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Returns bottom-most blocks of a tetromino, which could be from multiple rows depending on shape (e.g. T)
|
|
108
|
+
def bottom_most_blocks
|
|
109
|
+
width.times.map do |column_index|
|
|
110
|
+
row_blocks_with_row_index = @blocks.each_with_index.to_a.reverse.detect do |row_blocks, row_index|
|
|
111
|
+
!row_blocks[column_index].clear?
|
|
112
|
+
end
|
|
113
|
+
bottom_most_block = row_blocks_with_row_index[0][column_index]
|
|
114
|
+
bottom_most_block_row = row_blocks_with_row_index[1]
|
|
115
|
+
{
|
|
116
|
+
block: bottom_most_block,
|
|
117
|
+
row_index: bottom_most_block_row,
|
|
118
|
+
column_index: column_index
|
|
119
|
+
}
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def bottom_most_block_for_column(column)
|
|
124
|
+
bottom_most_blocks.detect {|bottom_most_block| (@column + bottom_most_block[:column_index]) == column}
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def right_blocked?
|
|
128
|
+
(@column == game.playfield_width - width) ||
|
|
129
|
+
right_most_blocks.any? { |right_most_block|
|
|
130
|
+
(@row + right_most_block[:row_index]) >= 0 &&
|
|
131
|
+
playfield[@row + right_most_block[:row_index]][@column + right_most_block[:column_index] + 1].occupied?
|
|
132
|
+
}
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Returns right-most blocks of a tetromino, which could be from multiple columns depending on shape (e.g. T)
|
|
136
|
+
def right_most_blocks
|
|
137
|
+
@blocks.each_with_index.map do |row_blocks, row_index|
|
|
138
|
+
column_block_with_column_index = row_blocks.each_with_index.to_a.reverse.detect do |column_block, column_index|
|
|
139
|
+
!column_block.clear?
|
|
140
|
+
end
|
|
141
|
+
if column_block_with_column_index
|
|
142
|
+
right_most_block = column_block_with_column_index[0]
|
|
143
|
+
{
|
|
144
|
+
block: right_most_block,
|
|
145
|
+
row_index: row_index,
|
|
146
|
+
column_index: column_block_with_column_index[1]
|
|
147
|
+
}
|
|
148
|
+
end
|
|
149
|
+
end.compact
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def left_blocked?
|
|
153
|
+
(@column == 0) ||
|
|
154
|
+
left_most_blocks.any? { |left_most_block|
|
|
155
|
+
(@row + left_most_block[:row_index]) >= 0 &&
|
|
156
|
+
playfield[@row + left_most_block[:row_index]][@column + left_most_block[:column_index] - 1].occupied?
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Returns right-most blocks of a tetromino, which could be from multiple columns depending on shape (e.g. T)
|
|
161
|
+
def left_most_blocks
|
|
162
|
+
@blocks.each_with_index.map do |row_blocks, row_index|
|
|
163
|
+
column_block_with_column_index = row_blocks.each_with_index.to_a.detect do |column_block, column_index|
|
|
164
|
+
!column_block.clear?
|
|
165
|
+
end
|
|
166
|
+
if column_block_with_column_index
|
|
167
|
+
left_most_block = column_block_with_column_index[0]
|
|
168
|
+
{
|
|
169
|
+
block: left_most_block,
|
|
170
|
+
row_index: row_index,
|
|
171
|
+
column_index: column_block_with_column_index[1]
|
|
172
|
+
}
|
|
173
|
+
end
|
|
174
|
+
end.compact
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def width
|
|
178
|
+
@blocks[0].size
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def height
|
|
182
|
+
@blocks.size
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def down!(instant: false)
|
|
186
|
+
launch! if preview?
|
|
187
|
+
unless stopped?
|
|
188
|
+
block_count = 1
|
|
189
|
+
if instant
|
|
190
|
+
remaining_height, bottom_touching_block = remaining_height_and_bottom_touching_block
|
|
191
|
+
block_count = remaining_height - @row
|
|
192
|
+
end
|
|
193
|
+
new_row = @row + block_count
|
|
194
|
+
update_playfield(new_row, @column)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def left!
|
|
199
|
+
unless left_blocked?
|
|
200
|
+
new_column = @column - 1
|
|
201
|
+
update_playfield(@row, new_column)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def right!
|
|
206
|
+
unless right_blocked?
|
|
207
|
+
new_column = @column + 1
|
|
208
|
+
update_playfield(@row, new_column)
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Rotate in specified direcation, which can be :right (clockwise) or :left (counterclockwise)
|
|
213
|
+
def rotate!(direction)
|
|
214
|
+
return if stopped?
|
|
215
|
+
can_rotate = nil
|
|
216
|
+
new_blocks = nil
|
|
217
|
+
game.hypothetical do
|
|
218
|
+
hypothetical_rotated_tetromino = hypothetical_tetromino
|
|
219
|
+
new_blocks = hypothetical_rotated_tetromino.rotate_blocks(direction)
|
|
220
|
+
can_rotate = !hypothetical_rotated_tetromino.stopped? && !hypothetical_rotated_tetromino.right_blocked? && !hypothetical_rotated_tetromino.left_blocked?
|
|
221
|
+
end
|
|
222
|
+
if can_rotate
|
|
223
|
+
remove_from_playfield
|
|
224
|
+
self.orientation = ORIENTATIONS[ORIENTATIONS.rotate(direction == :right ? -1 : 1).index(@orientation)]
|
|
225
|
+
self.blocks = new_blocks
|
|
226
|
+
update_playfield(@row, @column)
|
|
227
|
+
end
|
|
228
|
+
rescue => e
|
|
229
|
+
puts e.full_message
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def rotate_blocks(direction)
|
|
233
|
+
new_blocks = Matrix[*@blocks].transpose.to_a
|
|
234
|
+
if direction == :right
|
|
235
|
+
new_blocks = new_blocks.map(&:reverse)
|
|
236
|
+
else
|
|
237
|
+
new_blocks = new_blocks.reverse
|
|
238
|
+
end
|
|
239
|
+
Matrix[*new_blocks].to_a
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def hypothetical_tetromino
|
|
243
|
+
clone.tap do |hypo_clone|
|
|
244
|
+
remove_from_playfield
|
|
245
|
+
hypo_clone.blocks = @blocks.map do |row_blocks|
|
|
246
|
+
row_blocks.map do |column_block|
|
|
247
|
+
column_block.clone
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def remaining_height_and_bottom_touching_block
|
|
254
|
+
playfield_remaining_heights = game.playfield_remaining_heights(self)
|
|
255
|
+
bottom_most_blocks.map do |bottom_most_block|
|
|
256
|
+
playfield_column = @column + bottom_most_block[:column_index]
|
|
257
|
+
[playfield_remaining_heights[playfield_column] - (bottom_most_block[:row_index] + 1), bottom_most_block]
|
|
258
|
+
end.min_by(&:first)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def default_blocks
|
|
262
|
+
case @letter
|
|
263
|
+
when :I
|
|
264
|
+
[
|
|
265
|
+
[block, block, block, block]
|
|
266
|
+
]
|
|
267
|
+
when :J
|
|
268
|
+
[
|
|
269
|
+
[block, block, block],
|
|
270
|
+
[empty, empty, block],
|
|
271
|
+
]
|
|
272
|
+
when :L
|
|
273
|
+
[
|
|
274
|
+
[block, block, block],
|
|
275
|
+
[block, empty, empty],
|
|
276
|
+
]
|
|
277
|
+
when :O
|
|
278
|
+
[
|
|
279
|
+
[block, block],
|
|
280
|
+
[block, block],
|
|
281
|
+
]
|
|
282
|
+
when :S
|
|
283
|
+
[
|
|
284
|
+
[empty, block, block],
|
|
285
|
+
[block, block, empty],
|
|
286
|
+
]
|
|
287
|
+
when :T
|
|
288
|
+
[
|
|
289
|
+
[block, block, block],
|
|
290
|
+
[empty, block, empty],
|
|
291
|
+
]
|
|
292
|
+
when :Z
|
|
293
|
+
[
|
|
294
|
+
[block, block, empty],
|
|
295
|
+
[empty, block, block],
|
|
296
|
+
]
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def color
|
|
301
|
+
LETTER_COLORS[@letter]
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def include_block?(block)
|
|
305
|
+
@blocks.flatten.include?(block)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
private
|
|
309
|
+
|
|
310
|
+
def block
|
|
311
|
+
Block.new(color)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def empty
|
|
315
|
+
Block.new
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def update_playfield_block(&updater)
|
|
319
|
+
@row.upto(@row + height - 1) do |playfield_row|
|
|
320
|
+
@column.upto(@column + width - 1) do |playfield_column|
|
|
321
|
+
row_index = playfield_row - @row
|
|
322
|
+
column_index = playfield_column - @column
|
|
323
|
+
updater.call(playfield_row, playfield_column, row_index, column_index)
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
end
|
data/examples/tetris.rb
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Copyright (c) 2021 Andy Maleh
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require 'glimmer-dsl-libui'
|
|
23
|
+
|
|
24
|
+
require_relative 'tetris/model/game'
|
|
25
|
+
|
|
26
|
+
class Tetris
|
|
27
|
+
include Glimmer
|
|
28
|
+
|
|
29
|
+
BLOCK_SIZE = 25
|
|
30
|
+
BEVEL_CONSTANT = 20
|
|
31
|
+
|
|
32
|
+
attr_reader :game
|
|
33
|
+
|
|
34
|
+
def initialize
|
|
35
|
+
@game = Model::Game.new
|
|
36
|
+
create_gui
|
|
37
|
+
register_observers
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def launch
|
|
41
|
+
@game.start!
|
|
42
|
+
@main_window.show
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def create_gui
|
|
46
|
+
@main_window = window('Glimmer Tetris', Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE) {
|
|
47
|
+
playfield(playfield_width: Model::Game::PLAYFIELD_WIDTH, playfield_height: Model::Game::PLAYFIELD_HEIGHT, block_size: BLOCK_SIZE)
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def register_observers
|
|
52
|
+
Glimmer::DataBinding::Observer.proc do |game_over|
|
|
53
|
+
if game_over
|
|
54
|
+
show_game_over_dialog
|
|
55
|
+
else
|
|
56
|
+
start_moving_tetrominos_down
|
|
57
|
+
end
|
|
58
|
+
end.observe(@game, :game_over)
|
|
59
|
+
|
|
60
|
+
Model::Game::PLAYFIELD_HEIGHT.times do |row|
|
|
61
|
+
Model::Game::PLAYFIELD_HEIGHT.times do |column|
|
|
62
|
+
Glimmer::DataBinding::Observer.proc do |new_color|
|
|
63
|
+
@blocks[row][column].fill = new_color
|
|
64
|
+
end.observe(@game.playfield[row][column], :color)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def playfield(playfield_width: , playfield_height: , block_size: )
|
|
70
|
+
area {
|
|
71
|
+
@blocks = playfield_height.times.map do |row|
|
|
72
|
+
playfield_width.times.map do |column|
|
|
73
|
+
block(row: row, column: column, block_size: block_size)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
on_key_down do |key_event|
|
|
78
|
+
case key_event
|
|
79
|
+
in ext_key: :down
|
|
80
|
+
game.down!
|
|
81
|
+
in ext_key: :up
|
|
82
|
+
case game.up_arrow_action
|
|
83
|
+
when :instant_down
|
|
84
|
+
game.down!(instant: true)
|
|
85
|
+
when :rotate_right
|
|
86
|
+
game.rotate!(:right)
|
|
87
|
+
when :rotate_left
|
|
88
|
+
game.rotate!(:left)
|
|
89
|
+
end
|
|
90
|
+
in ext_key: :left
|
|
91
|
+
game.left!
|
|
92
|
+
in ext_key: :right
|
|
93
|
+
game.right!
|
|
94
|
+
in modifier: :shift
|
|
95
|
+
game.rotate!(:right)
|
|
96
|
+
in modifier: :control
|
|
97
|
+
game.rotate!(:left)
|
|
98
|
+
else
|
|
99
|
+
# Do Nothing
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def block(row: , column: , block_size: )
|
|
106
|
+
path {
|
|
107
|
+
square(column * block_size, row * block_size, block_size)
|
|
108
|
+
|
|
109
|
+
fill Model::Block::COLOR_CLEAR
|
|
110
|
+
}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def start_moving_tetrominos_down
|
|
114
|
+
Glimmer::LibUI.timer(@game.delay) do
|
|
115
|
+
@game.down! if !@game.game_over? && !@game.paused?
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def show_game_over_dialog
|
|
120
|
+
msg_box('Game Over', "Score: #{@game.high_scores.first.score}")
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
Tetris.new.launch
|
data/glimmer-dsl-libui.gemspec
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: glimmer-dsl-libui
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.17
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Maleh
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-
|
|
11
|
+
date: 2021-11-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: glimmer
|
|
@@ -252,6 +252,11 @@ files:
|
|
|
252
252
|
- examples/method_based_custom_keyword.rb
|
|
253
253
|
- examples/midi_player.rb
|
|
254
254
|
- examples/simple_notepad.rb
|
|
255
|
+
- examples/tetris.rb
|
|
256
|
+
- examples/tetris/model/block.rb
|
|
257
|
+
- examples/tetris/model/game.rb
|
|
258
|
+
- examples/tetris/model/past_game.rb
|
|
259
|
+
- examples/tetris/model/tetromino.rb
|
|
255
260
|
- examples/timer.rb
|
|
256
261
|
- glimmer-dsl-libui.gemspec
|
|
257
262
|
- icons/glimmer.png
|