glimmer-dsl-swt 4.22.1.1 → 4.22.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/LICENSE.txt +1 -1
- data/README.md +15 -12
- data/RUBY_VERSION +1 -1
- data/VERSION +1 -1
- data/bin/girb +1 -1
- data/bin/girb_runner.rb +1 -1
- data/bin/glimmer +1 -1
- data/bin/glimmer-setup +1 -1
- data/docs/reference/GLIMMER_COMMAND.md +20 -6
- data/docs/reference/GLIMMER_CONFIGURATION.md +1 -1
- data/docs/reference/GLIMMER_GIRB.md +2 -2
- data/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md +646 -94
- data/docs/reference/GLIMMER_PACKAGING_AND_DISTRIBUTION.md +13 -32
- data/docs/reference/GLIMMER_SAMPLES.md +25 -9
- data/docs/reference/GLIMMER_STYLE_GUIDE.md +6 -0
- data/glimmer-dsl-swt.gemspec +0 -0
- data/lib/ext/glimmer/config.rb +1 -1
- data/lib/ext/glimmer.rb +4 -4
- data/lib/ext/rouge/themes/glimmer.rb +21 -0
- data/lib/glimmer/Rakefile +6 -6
- data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
- data/lib/glimmer/data_binding/observable_widget.rb +1 -1
- data/lib/glimmer/data_binding/table_items_binding.rb +1 -1
- data/lib/glimmer/data_binding/tree_items_binding.rb +1 -1
- data/lib/glimmer/data_binding/widget_binding.rb +1 -1
- data/lib/glimmer/dsl/swt/animation_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/async_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/auto_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/bind_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/block_property_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/c_tab_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/checkbox_group_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/color_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/column_properties_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/combo_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/cursor_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/custom_shape_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/custom_widget_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dialog_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/display_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dnd_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dsl.rb +2 -2
- data/lib/glimmer/dsl/swt/exec_expression.rb +2 -3
- data/lib/glimmer/dsl/swt/expand_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/font_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/image_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/layout_data_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/layout_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/list_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/listener_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/menu_bar_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/menu_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/message_box_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/multiply_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/observe_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/pixel_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/property_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/radio_group_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/rgb_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/rgba_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/shape_expression.rb +2 -1
- data/lib/glimmer/dsl/swt/shell_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/shine_data_binding_expression.rb +4 -9
- data/lib/glimmer/dsl/swt/swt_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/sync_call_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/sync_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tab_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/table_items_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/timer_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/transform_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tray_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tray_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tree_items_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tree_properties_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/widget_expression.rb +1 -1
- data/lib/glimmer/launcher.rb +2 -2
- data/lib/glimmer/rake_task/list.rb +1 -1
- data/lib/glimmer/rake_task/package.rb +1 -1
- data/lib/glimmer/rake_task/scaffold.rb +2 -3
- data/lib/glimmer/rake_task.rb +1 -1
- data/lib/glimmer/swt/c_tab_item_proxy.rb +1 -1
- data/lib/glimmer/swt/color_proxy.rb +1 -1
- data/lib/glimmer/swt/combo_proxy.rb +1 -1
- data/lib/glimmer/swt/cursor_proxy.rb +1 -1
- data/lib/glimmer/swt/custom/animation.rb +1 -1
- data/lib/glimmer/swt/custom/checkbox_group.rb +1 -1
- data/lib/glimmer/swt/custom/code_text.rb +1 -1
- data/lib/glimmer/swt/custom/drawable.rb +1 -1
- data/lib/glimmer/swt/custom/radio_group.rb +1 -1
- data/lib/glimmer/swt/custom/shape/arc.rb +1 -1
- data/lib/glimmer/swt/custom/shape/cubic.rb +1 -1
- data/lib/glimmer/swt/custom/shape/focus.rb +1 -1
- data/lib/glimmer/swt/custom/shape/image.rb +1 -1
- data/lib/glimmer/swt/custom/shape/line.rb +1 -1
- data/lib/glimmer/swt/custom/shape/oval.rb +1 -1
- data/lib/glimmer/swt/custom/shape/path.rb +1 -1
- data/lib/glimmer/swt/custom/shape/path_segment.rb +1 -1
- data/lib/glimmer/swt/custom/shape/point.rb +1 -1
- data/lib/glimmer/swt/custom/shape/polygon.rb +1 -1
- data/lib/glimmer/swt/custom/shape/polyline.rb +1 -1
- data/lib/glimmer/swt/custom/shape/quad.rb +1 -1
- data/lib/glimmer/swt/custom/shape/rectangle.rb +1 -1
- data/lib/glimmer/swt/custom/shape/text.rb +1 -1
- data/lib/glimmer/swt/custom/shape.rb +2 -1
- data/lib/glimmer/swt/date_time_proxy.rb +1 -1
- data/lib/glimmer/swt/dialog_proxy.rb +1 -1
- data/lib/glimmer/swt/display_proxy.rb +1 -1
- data/lib/glimmer/swt/dnd_proxy.rb +1 -1
- data/lib/glimmer/swt/expand_item_proxy.rb +1 -1
- data/lib/glimmer/swt/font_proxy.rb +1 -1
- data/lib/glimmer/swt/image_proxy.rb +1 -1
- data/lib/glimmer/swt/layout_data_proxy.rb +1 -1
- data/lib/glimmer/swt/layout_proxy.rb +1 -1
- data/lib/glimmer/swt/menu_proxy.rb +1 -1
- data/lib/glimmer/swt/message_box_proxy.rb +1 -1
- data/lib/glimmer/swt/packages.rb +1 -1
- data/lib/glimmer/swt/properties.rb +1 -1
- data/lib/glimmer/swt/proxy_properties.rb +1 -1
- data/lib/glimmer/swt/sash_form_proxy.rb +1 -1
- data/lib/glimmer/swt/scrolled_composite_proxy.rb +1 -1
- data/lib/glimmer/swt/shape_listener_proxy.rb +1 -1
- data/lib/glimmer/swt/shell_proxy.rb +2 -1
- data/lib/glimmer/swt/style_constantizable.rb +1 -1
- data/lib/glimmer/swt/styled_text_proxy.rb +1 -1
- data/lib/glimmer/swt/swt_proxy.rb +1 -1
- data/lib/glimmer/swt/tab_folder_proxy.rb +1 -1
- data/lib/glimmer/swt/tab_item_proxy.rb +1 -1
- data/lib/glimmer/swt/table_column_proxy.rb +1 -1
- data/lib/glimmer/swt/table_proxy.rb +1 -1
- data/lib/glimmer/swt/tool_bar_proxy.rb +1 -1
- data/lib/glimmer/swt/transform_proxy.rb +1 -1
- data/lib/glimmer/swt/tray_item_proxy.rb +1 -1
- data/lib/glimmer/swt/tray_proxy.rb +1 -1
- data/lib/glimmer/swt/tree_proxy.rb +1 -1
- data/lib/glimmer/swt/widget_listener_proxy.rb +1 -1
- data/lib/glimmer/swt/widget_proxy.rb +1 -1
- data/lib/glimmer/ui/custom_shape.rb +1 -1
- data/lib/glimmer/ui/custom_shell.rb +1 -1
- data/lib/glimmer/ui/custom_widget.rb +1 -1
- data/lib/glimmer/ui.rb +21 -0
- data/lib/glimmer/util/proc_tracker.rb +4 -4
- data/lib/glimmer-dsl-swt.rb +1 -1
- data/samples/elaborate/battleship/model/cell.rb +1 -1
- data/samples/elaborate/battleship/model/game.rb +1 -1
- data/samples/elaborate/battleship/model/grid.rb +1 -1
- data/samples/elaborate/battleship/model/ship.rb +1 -1
- data/samples/elaborate/battleship/model/ship_collection.rb +1 -1
- data/samples/elaborate/battleship/view/action_panel.rb +1 -1
- data/samples/elaborate/battleship/view/cell.rb +1 -1
- data/samples/elaborate/battleship/view/grid.rb +1 -1
- data/samples/elaborate/battleship/view/ship.rb +1 -1
- data/samples/elaborate/battleship/view/ship_collection.rb +1 -1
- data/samples/elaborate/battleship.rb +1 -1
- data/samples/elaborate/calculator.rb +1 -1
- data/samples/elaborate/clock.rb +1 -1
- data/samples/elaborate/connect4/model/grid.rb +1 -1
- data/samples/elaborate/connect4/model/slot.rb +1 -1
- data/samples/elaborate/connect4.rb +1 -1
- data/samples/elaborate/contact_manager/contact.rb +1 -1
- data/samples/elaborate/contact_manager/contact_manager_presenter.rb +1 -1
- data/samples/elaborate/contact_manager/contact_repository.rb +1 -1
- data/samples/elaborate/contact_manager.rb +1 -1
- data/samples/elaborate/game_of_life/model/cell.rb +1 -1
- data/samples/elaborate/game_of_life/model/grid.rb +1 -1
- data/samples/elaborate/game_of_life.rb +1 -1
- data/samples/elaborate/login.rb +1 -1
- data/samples/elaborate/mandelbrot_fractal.rb +1 -1
- data/samples/elaborate/meta_sample.rb +1 -1
- data/samples/elaborate/metronome.rb +1 -1
- data/samples/elaborate/parking/model/parking_floor.rb +1 -1
- data/samples/elaborate/parking/model/parking_presenter.rb +1 -1
- data/samples/elaborate/parking/model/parking_spot.rb +1 -1
- data/samples/elaborate/parking.rb +1 -1
- data/samples/elaborate/quarto/model/game.rb +1 -1
- data/samples/elaborate/quarto/model/piece/cube.rb +1 -1
- data/samples/elaborate/quarto/model/piece/cylinder.rb +1 -1
- data/samples/elaborate/quarto/model/piece.rb +1 -1
- data/samples/elaborate/quarto/view/available_pieces_area.rb +1 -1
- data/samples/elaborate/quarto/view/board.rb +1 -1
- data/samples/elaborate/quarto/view/cell.rb +1 -1
- data/samples/elaborate/quarto/view/cube.rb +1 -1
- data/samples/elaborate/quarto/view/cylinder.rb +1 -1
- data/samples/elaborate/quarto/view/message_box_panel.rb +1 -1
- data/samples/elaborate/quarto/view/piece.rb +1 -1
- data/samples/elaborate/quarto/view/selected_piece_area.rb +1 -1
- data/samples/elaborate/quarto.rb +1 -3
- data/samples/elaborate/snake/model/apple.rb +33 -0
- data/samples/elaborate/snake/model/game.rb +68 -0
- data/samples/elaborate/snake/model/snake.rb +95 -0
- data/samples/elaborate/snake/model/vertebra.rb +22 -0
- data/samples/elaborate/snake/presenter/cell.rb +27 -0
- data/samples/elaborate/snake/presenter/grid.rb +49 -0
- data/samples/elaborate/snake.rb +103 -0
- data/samples/elaborate/stock_ticker.rb +1 -1
- data/samples/elaborate/tetris/model/block.rb +1 -1
- data/samples/elaborate/tetris/model/game.rb +1 -1
- data/samples/elaborate/tetris/model/past_game.rb +1 -1
- data/samples/elaborate/tetris/model/tetromino.rb +1 -1
- data/samples/elaborate/tetris/view/bevel.rb +1 -1
- data/samples/elaborate/tetris/view/block.rb +1 -1
- data/samples/elaborate/tetris/view/high_score_dialog.rb +1 -1
- data/samples/elaborate/tetris/view/playfield.rb +1 -1
- data/samples/elaborate/tetris/view/score_lane.rb +1 -1
- data/samples/elaborate/tetris/view/tetris_menu_bar.rb +1 -1
- data/samples/elaborate/tetris.rb +8 -8
- data/samples/elaborate/tic_tac_toe/board.rb +1 -1
- data/samples/elaborate/tic_tac_toe/cell.rb +1 -1
- data/samples/elaborate/tic_tac_toe.rb +1 -1
- data/samples/elaborate/timer.rb +1 -1
- data/samples/elaborate/user_profile.rb +1 -1
- data/samples/elaborate/weather.rb +1 -1
- data/samples/hello/hello_arrow.rb +1 -1
- data/samples/hello/hello_browser.rb +1 -1
- data/samples/hello/hello_button.rb +1 -1
- data/samples/hello/hello_c_combo.rb +1 -1
- data/samples/hello/hello_c_tab.rb +1 -1
- data/samples/hello/hello_canvas.rb +1 -1
- data/samples/hello/hello_canvas_animation.rb +1 -1
- data/samples/hello/hello_canvas_animation_multi.rb +1 -1
- data/samples/hello/hello_canvas_data_binding.rb +8 -8
- data/samples/hello/hello_canvas_path.rb +1 -1
- data/samples/hello/hello_canvas_shape_listeners.rb +1 -1
- data/samples/hello/hello_checkbox.rb +1 -1
- data/samples/hello/hello_checkbox_group.rb +1 -1
- data/samples/hello/hello_code_text.rb +1 -1
- data/samples/hello/hello_color_dialog.rb +1 -1
- data/samples/hello/hello_combo.rb +1 -1
- data/samples/hello/hello_composite.rb +1 -1
- data/samples/hello/hello_computed.rb +1 -1
- data/samples/hello/hello_cool_bar.rb +1 -1
- data/samples/hello/hello_cursor.rb +1 -1
- data/samples/hello/hello_custom_shape.rb +1 -1
- data/samples/hello/hello_custom_shell.rb +1 -1
- data/samples/hello/hello_custom_widget.rb +1 -1
- data/samples/hello/hello_date_time.rb +1 -1
- data/samples/hello/hello_dialog.rb +1 -1
- data/samples/hello/hello_directory_dialog.rb +1 -1
- data/samples/hello/hello_drag_and_drop.rb +1 -1
- data/samples/hello/hello_expand_bar.rb +1 -1
- data/samples/hello/hello_file_dialog.rb +1 -1
- data/samples/hello/hello_font_dialog.rb +1 -1
- data/samples/hello/hello_group.rb +1 -1
- data/samples/hello/hello_label.rb +1 -1
- data/samples/hello/hello_layout.rb +1 -1
- data/samples/hello/hello_link.rb +1 -1
- data/samples/hello/hello_list_multi_selection.rb +1 -1
- data/samples/hello/hello_list_single_selection.rb +1 -1
- data/samples/hello/hello_menu_bar.rb +1 -1
- data/samples/hello/hello_message_box.rb +1 -1
- data/samples/hello/hello_pop_up_context_menu.rb +1 -1
- data/samples/hello/hello_print.rb +1 -1
- data/samples/hello/hello_print_dialog.rb +1 -1
- data/samples/hello/hello_progress_bar.rb +1 -1
- data/samples/hello/hello_radio.rb +1 -1
- data/samples/hello/hello_radio_group.rb +1 -1
- data/samples/hello/hello_sash_form.rb +1 -1
- data/samples/hello/hello_scale.rb +1 -1
- data/samples/hello/hello_scrolled_composite.rb +1 -1
- data/samples/hello/hello_shape.rb +1 -1
- data/samples/hello/hello_shell.rb +1 -1
- data/samples/hello/hello_slider.rb +1 -1
- data/samples/hello/hello_spinner.rb +1 -1
- data/samples/hello/hello_styled_text.rb +1 -1
- data/samples/hello/hello_tab.rb +1 -1
- data/samples/hello/hello_table.rb +1 -1
- data/samples/hello/hello_text.rb +1 -1
- data/samples/hello/hello_toggle.rb +1 -1
- data/samples/hello/hello_tool_bar.rb +1 -1
- data/samples/hello/hello_tray_item.rb +1 -1
- data/samples/hello/hello_tree.rb +1 -1
- data/samples/hello/hello_world.rb +1 -1
- metadata +27 -33
@@ -1,5 +1,5 @@
|
|
1
|
-
# Copyright (c) 2007-
|
2
|
-
#
|
1
|
+
# Copyright (c) 2007-2022 Andy Maleh
|
2
|
+
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
# a copy of this software and associated documentation files (the
|
5
5
|
# "Software"), to deal in the Software without restriction, including
|
@@ -7,10 +7,10 @@
|
|
7
7
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
8
|
# permit persons to whom the Software is furnished to do so, subject to
|
9
9
|
# the following conditions:
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# The above copyright notice and this permission notice shall be
|
12
12
|
# included in all copies or substantial portions of the Software.
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
15
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
16
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
data/lib/glimmer-dsl-swt.rb
CHANGED
@@ -107,7 +107,7 @@ class Calculator
|
|
107
107
|
def display_about_dialog
|
108
108
|
message_box(body_root) {
|
109
109
|
text 'About'
|
110
|
-
message "Glimmer - Calculator\n\nCopyright (c) 2007-
|
110
|
+
message "Glimmer - Calculator\n\nCopyright (c) 2007-2022 Andy Maleh"
|
111
111
|
}.open
|
112
112
|
end
|
113
113
|
|
data/samples/elaborate/clock.rb
CHANGED
data/samples/elaborate/login.rb
CHANGED
data/samples/elaborate/quarto.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2007-
|
1
|
+
# Copyright (c) 2007-2022 Andy Maleh
|
2
2
|
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
# a copy of this software and associated documentation files (the
|
@@ -46,8 +46,6 @@ class Quarto
|
|
46
46
|
COLOR_AREA = rgb(206, 188, 170)
|
47
47
|
COLOR_LIGHT_WOOD = rgb(254, 187, 120)
|
48
48
|
COLOR_DARK_WOOD = rgb(204, 108, 58)
|
49
|
-
MESSAGE_BOX_PANEL_WIDTH = 300
|
50
|
-
MESSAGE_BOX_PANEL_HEIGHT = 100
|
51
49
|
FILE_QUARTO_CONFIG = File.join(Dir.home, '.quarto')
|
52
50
|
|
53
51
|
before_body do
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Snake
|
2
|
+
module Model
|
3
|
+
class Apple
|
4
|
+
attr_reader :game
|
5
|
+
attr_accessor :row, :column
|
6
|
+
|
7
|
+
def initialize(game)
|
8
|
+
@game = game
|
9
|
+
end
|
10
|
+
|
11
|
+
# generates a new location from scratch or via dependency injection of what cell is (for testing purposes)
|
12
|
+
def generate(initial_row: nil, initial_column: nil)
|
13
|
+
if initial_row && initial_column
|
14
|
+
self.row, self.column = initial_row, initial_column
|
15
|
+
else
|
16
|
+
self.row, self.column = @game.height.times.zip(@game.width.times).reject do |row, column|
|
17
|
+
@game.snake.vertebrae.map {|v| [v.row, v.column]}.include?([row, column])
|
18
|
+
end.sample
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def remove
|
23
|
+
self.row = nil
|
24
|
+
self.column = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# inspect is overridden to prevent printing very long stack traces
|
28
|
+
def inspect
|
29
|
+
"#{super[0, 120]}... >"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
require_relative 'snake'
|
4
|
+
require_relative 'apple'
|
5
|
+
|
6
|
+
class Snake
|
7
|
+
module Model
|
8
|
+
class Game
|
9
|
+
WIDTH_DEFAULT = 20
|
10
|
+
HEIGHT_DEFAULT = 20
|
11
|
+
FILE_HIGH_SCORE = File.expand_path(File.join(Dir.home, '.glimmer-snake'))
|
12
|
+
|
13
|
+
attr_reader :width, :height
|
14
|
+
attr_accessor :snake, :apple, :over, :score, :high_score, :paused
|
15
|
+
alias over? over
|
16
|
+
alias paused? paused
|
17
|
+
|
18
|
+
def initialize(width = WIDTH_DEFAULT, height = HEIGHT_DEFAULT)
|
19
|
+
@width = width
|
20
|
+
@height = height
|
21
|
+
@snake = Snake.new(self)
|
22
|
+
@apple = Apple.new(self)
|
23
|
+
FileUtils.touch(FILE_HIGH_SCORE)
|
24
|
+
@high_score = File.read(FILE_HIGH_SCORE).to_i rescue 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def score=(new_score)
|
28
|
+
@score = new_score
|
29
|
+
self.high_score = @score if @score > @high_score
|
30
|
+
end
|
31
|
+
|
32
|
+
def high_score=(new_high_score)
|
33
|
+
@high_score = new_high_score
|
34
|
+
File.write(FILE_HIGH_SCORE, @high_score.to_s)
|
35
|
+
rescue => e
|
36
|
+
puts e.full_message
|
37
|
+
end
|
38
|
+
|
39
|
+
def start
|
40
|
+
self.over = false
|
41
|
+
self.score = 0
|
42
|
+
self.snake.generate
|
43
|
+
self.apple.generate
|
44
|
+
end
|
45
|
+
|
46
|
+
def pause
|
47
|
+
self.paused = true
|
48
|
+
end
|
49
|
+
|
50
|
+
def resume
|
51
|
+
self.paused = false
|
52
|
+
end
|
53
|
+
|
54
|
+
def toggle_pause
|
55
|
+
unless paused?
|
56
|
+
pause
|
57
|
+
else
|
58
|
+
resume
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# inspect is overridden to prevent printing very long stack traces
|
63
|
+
def inspect
|
64
|
+
"#{super[0, 75]}... >"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require_relative 'vertebra'
|
2
|
+
|
3
|
+
class Snake
|
4
|
+
module Model
|
5
|
+
class Snake
|
6
|
+
SCORE_EAT_APPLE = 50
|
7
|
+
RIGHT_TURN_MAP = {
|
8
|
+
north: :east,
|
9
|
+
east: :south,
|
10
|
+
south: :west,
|
11
|
+
west: :north
|
12
|
+
}
|
13
|
+
LEFT_TURN_MAP = RIGHT_TURN_MAP.invert
|
14
|
+
|
15
|
+
attr_accessor :collided
|
16
|
+
alias collided? collided
|
17
|
+
|
18
|
+
attr_reader :game
|
19
|
+
# vertebrae and joins are ordered from tail to head
|
20
|
+
attr_accessor :vertebrae
|
21
|
+
|
22
|
+
def initialize(game)
|
23
|
+
@game = game
|
24
|
+
end
|
25
|
+
|
26
|
+
# generates a new snake location and orientation from scratch or via dependency injection of what head_cell and orientation are (for testing purposes)
|
27
|
+
def generate(initial_row: nil, initial_column: nil, initial_orientation: nil)
|
28
|
+
self.collided = false
|
29
|
+
initial_vertebra = Vertebra.new(snake: self, row: initial_row, column: initial_column, orientation: initial_orientation)
|
30
|
+
self.vertebrae = [initial_vertebra]
|
31
|
+
end
|
32
|
+
|
33
|
+
def length
|
34
|
+
@vertebrae.length
|
35
|
+
end
|
36
|
+
|
37
|
+
def head
|
38
|
+
@vertebrae.last
|
39
|
+
end
|
40
|
+
|
41
|
+
def tail
|
42
|
+
@vertebrae.first
|
43
|
+
end
|
44
|
+
|
45
|
+
def remove
|
46
|
+
self.vertebrae.clear
|
47
|
+
self.joins.clear
|
48
|
+
end
|
49
|
+
|
50
|
+
def move
|
51
|
+
@old_tail = tail.dup
|
52
|
+
@new_head = head.dup
|
53
|
+
case @new_head.orientation
|
54
|
+
when :east
|
55
|
+
@new_head.column = (@new_head.column + 1) % @game.width
|
56
|
+
when :west
|
57
|
+
@new_head.column = (@new_head.column - 1) % @game.width
|
58
|
+
when :south
|
59
|
+
@new_head.row = (@new_head.row + 1) % @game.height
|
60
|
+
when :north
|
61
|
+
@new_head.row = (@new_head.row - 1) % @game.height
|
62
|
+
end
|
63
|
+
if @vertebrae.map {|v| [v.row, v.column]}.include?([@new_head.row, @new_head.column])
|
64
|
+
self.collided = true
|
65
|
+
@game.over = true
|
66
|
+
else
|
67
|
+
@vertebrae.append(@new_head)
|
68
|
+
@vertebrae.delete(tail)
|
69
|
+
if head.row == @game.apple.row && head.column == @game.apple.column
|
70
|
+
grow
|
71
|
+
@game.apple.generate
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def turn_right
|
77
|
+
head.orientation = RIGHT_TURN_MAP[head.orientation]
|
78
|
+
end
|
79
|
+
|
80
|
+
def turn_left
|
81
|
+
head.orientation = LEFT_TURN_MAP[head.orientation]
|
82
|
+
end
|
83
|
+
|
84
|
+
def grow
|
85
|
+
@game.score += SCORE_EAT_APPLE
|
86
|
+
@vertebrae.prepend(@old_tail)
|
87
|
+
end
|
88
|
+
|
89
|
+
# inspect is overridden to prevent printing very long stack traces
|
90
|
+
def inspect
|
91
|
+
"#{super[0, 150]}... >"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Snake
|
2
|
+
module Model
|
3
|
+
class Vertebra
|
4
|
+
ORIENTATIONS = %i[north east south west]
|
5
|
+
# orientation is needed for snake occuppied cells (but not apple cells)
|
6
|
+
attr_reader :snake
|
7
|
+
attr_accessor :row, :column, :orientation
|
8
|
+
|
9
|
+
def initialize(snake: , row: , column: , orientation: )
|
10
|
+
@row = row || rand(snake.game.height)
|
11
|
+
@column = column || rand(snake.game.width)
|
12
|
+
@orientation = orientation || ORIENTATIONS.sample
|
13
|
+
@snake = snake
|
14
|
+
end
|
15
|
+
|
16
|
+
# inspect is overridden to prevent printing very long stack traces
|
17
|
+
def inspect
|
18
|
+
"#{super[0, 150]}... >"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Snake
|
2
|
+
module Presenter
|
3
|
+
class Cell
|
4
|
+
COLOR_CLEAR = :white
|
5
|
+
COLOR_SNAKE = :green
|
6
|
+
COLOR_APPLE = :red
|
7
|
+
|
8
|
+
attr_reader :row, :column, :grid
|
9
|
+
attr_accessor :color
|
10
|
+
|
11
|
+
def initialize(grid: ,row: ,column: )
|
12
|
+
@row = row
|
13
|
+
@column = column
|
14
|
+
@grid = grid
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear
|
18
|
+
self.color = COLOR_CLEAR unless color == COLOR_CLEAR
|
19
|
+
end
|
20
|
+
|
21
|
+
# inspect is overridden to prevent printing very long stack traces
|
22
|
+
def inspect
|
23
|
+
"#{super[0, 150]}... >"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|