glimmer-dsl-gtk 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,167 @@
1
+ require 'glimmer-dsl-gtk'
2
+
3
+ require_relative 'tetris/model/game'
4
+
5
+ class Tetris
6
+ include Glimmer
7
+
8
+ BLOCK_SIZE = 25
9
+ BEVEL_CONSTANT = 20
10
+ COLOR_GRAY = [192, 192, 192]
11
+
12
+ def initialize
13
+ @game = Model::Game.new
14
+ end
15
+
16
+ def launch
17
+ create_gui
18
+ register_observers
19
+ @game.start!
20
+ @main_window.show
21
+ end
22
+
23
+ def create_gui
24
+ @main_window = window {
25
+ title 'Glimmer Tetris'
26
+ default_size Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE # + 98
27
+
28
+ box(:vertical) {
29
+ @playfield_blocks = playfield(playfield_width: @game.playfield_width, playfield_height: @game.playfield_height, block_size: BLOCK_SIZE)
30
+ }
31
+
32
+ on(:key_press_event) do |widget, key_event|
33
+ case key_event.keyval
34
+ when 65364 # down arrow
35
+ @game.down!
36
+ when 32 # space
37
+ @game.down!(instant: true)
38
+ when 65362 # up arrow
39
+ case @game.up_arrow_action
40
+ when :instant_down
41
+ @game.down!(instant: true)
42
+ when :rotate_right
43
+ @game.rotate!(:right)
44
+ when :rotate_left
45
+ @game.rotate!(:left)
46
+ end
47
+ when 65361 # left arrow
48
+ @game.left!
49
+ when 65363 # right arrow
50
+ @game.right!
51
+ when 65506 # right shift
52
+ @game.rotate!(:right)
53
+ when 65505 # left shift
54
+ @game.rotate!(:left)
55
+ else
56
+ # Do Nothing
57
+ end
58
+ end
59
+ }
60
+ end
61
+
62
+ def register_observers
63
+ observe(@game, :game_over) do |game_over|
64
+ if game_over
65
+ show_game_over_dialog
66
+ else
67
+ start_moving_tetrominos_down
68
+ end
69
+ end
70
+
71
+ @game.playfield_height.times do |row|
72
+ @game.playfield_width.times do |column|
73
+ observe(@game.playfield[row][column], :color) do |new_color|
74
+ color = new_color
75
+ block = @playfield_blocks[row][column]
76
+ block[:background_square].fill = color
77
+ block[:top_bevel_edge].fill = [color[0] + 4*BEVEL_CONSTANT, color[1] + 4*BEVEL_CONSTANT, color[2] + 4*BEVEL_CONSTANT]
78
+ block[:right_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
79
+ block[:bottom_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
80
+ block[:left_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
81
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
82
+ block[:drawing_area].queue_draw
83
+ false
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def playfield(playfield_width: , playfield_height: , block_size: , &extra_content)
90
+ blocks = []
91
+ box(:vertical) {
92
+ playfield_height.times.map do |row|
93
+ blocks << []
94
+ box(:horizontal) {
95
+ playfield_width.times.map do |column|
96
+ blocks.last << block(row: row, column: column, block_size: block_size)
97
+ end
98
+ }
99
+ end
100
+
101
+ extra_content&.call
102
+ }
103
+ blocks
104
+ end
105
+
106
+ def block(row: , column: , block_size: , &extra_content)
107
+ block = {}
108
+ bevel_pixel_size = 0.16 * block_size.to_f
109
+ color = Model::Block::COLOR_CLEAR
110
+ block[:drawing_area] = drawing_area {
111
+ size_request block_size, block_size
112
+
113
+ block[:background_square] = square(0, 0, block_size) {
114
+ fill *color
115
+ }
116
+
117
+ block[:top_bevel_edge] = polygon(0, 0, block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size, bevel_pixel_size) {
118
+ fill color[0] + 4*BEVEL_CONSTANT, color[1] + 4*BEVEL_CONSTANT, color[2] + 4*BEVEL_CONSTANT
119
+ }
120
+
121
+ block[:right_bevel_edge] = polygon(block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size, block_size) {
122
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
123
+ }
124
+
125
+ block[:bottom_bevel_edge] = polygon(block_size, block_size, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size) {
126
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
127
+ }
128
+
129
+ block[:left_bevel_edge] = polygon(0, 0, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size) {
130
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
131
+ }
132
+
133
+ block[:border_square] = square(0, 0, block_size) {
134
+ stroke *COLOR_GRAY
135
+ }
136
+
137
+ extra_content&.call
138
+ }
139
+ block
140
+ end
141
+
142
+ def start_moving_tetrominos_down
143
+ unless @tetrominos_start_moving_down
144
+ @tetrominos_start_moving_down = true
145
+ GLib::Timeout.add(@game.delay*1000) do
146
+ @game.down! if !@game.game_over? && !@game.paused?
147
+ true
148
+ end
149
+ end
150
+ end
151
+
152
+ def show_game_over_dialog
153
+ message_dialog(@main_window) { |md|
154
+ title 'Game Over!'
155
+ text "Score: #{@game.high_scores.first.score}\nLines: #{@game.high_scores.first.lines}\nLevel: #{@game.high_scores.first.level}"
156
+
157
+ on(:response) do
158
+ md.destroy
159
+ end
160
+ }.show
161
+
162
+ @game.restart!
163
+ false
164
+ end
165
+ end
166
+
167
+ Tetris.new.launch
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-gtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-25 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.5.4
19
+ version: 2.6.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.5.4
26
+ version: 2.6.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: os
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -161,6 +161,7 @@ files:
161
161
  - glimmer-dsl-gtk.gemspec
162
162
  - lib/glimmer-dsl-gtk.rb
163
163
  - lib/glimmer/dsl/gtk/dsl.rb
164
+ - lib/glimmer/dsl/gtk/observe_expression.rb
164
165
  - lib/glimmer/dsl/gtk/on_expression.rb
165
166
  - lib/glimmer/dsl/gtk/operation_expression.rb
166
167
  - lib/glimmer/dsl/gtk/property_expression.rb
@@ -175,6 +176,7 @@ files:
175
176
  - lib/glimmer/gtk/shape/polyline.rb
176
177
  - lib/glimmer/gtk/shape/rectangle.rb
177
178
  - lib/glimmer/gtk/shape/rounded_rectangle.rb
179
+ - lib/glimmer/gtk/shape/square.rb
178
180
  - lib/glimmer/gtk/shape/triangle.rb
179
181
  - lib/glimmer/gtk/widget_proxy.rb
180
182
  - lib/glimmer/gtk/widget_proxy/application_proxy.rb
@@ -182,6 +184,11 @@ files:
182
184
  - lib/glimmer/gtk/widget_proxy/drawing_area_proxy.rb
183
185
  - lib/glimmer/gtk/widget_proxy/message_dialog_proxy.rb
184
186
  - lib/glimmer/gtk/widget_proxy/window_proxy.rb
187
+ - samples/elaborate/tetris.rb
188
+ - samples/elaborate/tetris/model/block.rb
189
+ - samples/elaborate/tetris/model/game.rb
190
+ - samples/elaborate/tetris/model/past_game.rb
191
+ - samples/elaborate/tetris/model/tetromino.rb
185
192
  - samples/elaborate/widget_gallery.rb
186
193
  - samples/hello/hello_application.rb
187
194
  - samples/hello/hello_button.rb