pixelsnake 1.0.0

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.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +21 -0
  4. data/README.md +122 -0
  5. data/bin/pixelsnake +8 -0
  6. data/lib/pixelsnake/abacus.rb +102 -0
  7. data/lib/pixelsnake/app.rb +130 -0
  8. data/lib/pixelsnake/art/big_font.rb +196 -0
  9. data/lib/pixelsnake/art/heads.rb +68 -0
  10. data/lib/pixelsnake/art/small_font.rb +73 -0
  11. data/lib/pixelsnake/autopilot.rb +34 -0
  12. data/lib/pixelsnake/award.rb +67 -0
  13. data/lib/pixelsnake/board.rb +36 -0
  14. data/lib/pixelsnake/canvas.rb +189 -0
  15. data/lib/pixelsnake/choice.rb +92 -0
  16. data/lib/pixelsnake/clockwork.rb +169 -0
  17. data/lib/pixelsnake/controls.rb +26 -0
  18. data/lib/pixelsnake/countdown.rb +80 -0
  19. data/lib/pixelsnake/courtyard.rb +306 -0
  20. data/lib/pixelsnake/day.rb +23 -0
  21. data/lib/pixelsnake/death.rb +115 -0
  22. data/lib/pixelsnake/debug_overlay.rb +53 -0
  23. data/lib/pixelsnake/demo.rb +85 -0
  24. data/lib/pixelsnake/density.rb +47 -0
  25. data/lib/pixelsnake/ease.rb +27 -0
  26. data/lib/pixelsnake/fade.rb +30 -0
  27. data/lib/pixelsnake/feats.rb +86 -0
  28. data/lib/pixelsnake/fine.rb +78 -0
  29. data/lib/pixelsnake/fireflies.rb +63 -0
  30. data/lib/pixelsnake/floating_text.rb +36 -0
  31. data/lib/pixelsnake/font.rb +62 -0
  32. data/lib/pixelsnake/fruit.rb +149 -0
  33. data/lib/pixelsnake/game.rb +294 -0
  34. data/lib/pixelsnake/gear.rb +132 -0
  35. data/lib/pixelsnake/high_scores.rb +91 -0
  36. data/lib/pixelsnake/hud.rb +217 -0
  37. data/lib/pixelsnake/iris.rb +76 -0
  38. data/lib/pixelsnake/jukebox.rb +143 -0
  39. data/lib/pixelsnake/keyboard.rb +96 -0
  40. data/lib/pixelsnake/laurel.rb +76 -0
  41. data/lib/pixelsnake/leaf.rb +73 -0
  42. data/lib/pixelsnake/lettering.rb +111 -0
  43. data/lib/pixelsnake/medal.rb +29 -0
  44. data/lib/pixelsnake/medal_view.rb +428 -0
  45. data/lib/pixelsnake/menu.rb +213 -0
  46. data/lib/pixelsnake/meter.rb +108 -0
  47. data/lib/pixelsnake/metronome.rb +24 -0
  48. data/lib/pixelsnake/name.rb +22 -0
  49. data/lib/pixelsnake/name_field.rb +156 -0
  50. data/lib/pixelsnake/odometer.rb +76 -0
  51. data/lib/pixelsnake/palette.rb +64 -0
  52. data/lib/pixelsnake/panel.rb +45 -0
  53. data/lib/pixelsnake/particles.rb +188 -0
  54. data/lib/pixelsnake/piece.rb +82 -0
  55. data/lib/pixelsnake/playback.rb +22 -0
  56. data/lib/pixelsnake/preferences.rb +96 -0
  57. data/lib/pixelsnake/rare_fruit.rb +268 -0
  58. data/lib/pixelsnake/recording.rb +44 -0
  59. data/lib/pixelsnake/ribbon.rb +110 -0
  60. data/lib/pixelsnake/save_file.rb +51 -0
  61. data/lib/pixelsnake/scenes/game_over.rb +249 -0
  62. data/lib/pixelsnake/scenes/medal_case.rb +268 -0
  63. data/lib/pixelsnake/scenes/pause.rb +171 -0
  64. data/lib/pixelsnake/scenes/play.rb +326 -0
  65. data/lib/pixelsnake/scenes/scores.rb +420 -0
  66. data/lib/pixelsnake/scenes/settings.rb +344 -0
  67. data/lib/pixelsnake/scenes/size_notice.rb +38 -0
  68. data/lib/pixelsnake/scenes/statistics.rb +420 -0
  69. data/lib/pixelsnake/scenes/title.rb +152 -0
  70. data/lib/pixelsnake/screen.rb +163 -0
  71. data/lib/pixelsnake/shed.rb +40 -0
  72. data/lib/pixelsnake/skin.rb +36 -0
  73. data/lib/pixelsnake/snake.rb +53 -0
  74. data/lib/pixelsnake/snake_view.rb +393 -0
  75. data/lib/pixelsnake/sound/band.rb +354 -0
  76. data/lib/pixelsnake/sound/instruments.rb +143 -0
  77. data/lib/pixelsnake/sound/mixer.rb +60 -0
  78. data/lib/pixelsnake/sound/music.rb +15 -0
  79. data/lib/pixelsnake/sound/pulse.rb +149 -0
  80. data/lib/pixelsnake/sound/song.rb +22 -0
  81. data/lib/pixelsnake/sound/sounds.rb +225 -0
  82. data/lib/pixelsnake/sound/speaker.rb +199 -0
  83. data/lib/pixelsnake/sound/synth.rb +277 -0
  84. data/lib/pixelsnake/sound/tune.rb +145 -0
  85. data/lib/pixelsnake/sound/tunes/dusk.rb +154 -0
  86. data/lib/pixelsnake/sound/tunes/fireflies.rb +106 -0
  87. data/lib/pixelsnake/sound/tunes/jade.rb +100 -0
  88. data/lib/pixelsnake/sound/tunes/lanterns.rb +126 -0
  89. data/lib/pixelsnake/sound/tunes.rb +40 -0
  90. data/lib/pixelsnake/sprite.rb +58 -0
  91. data/lib/pixelsnake/stage.rb +95 -0
  92. data/lib/pixelsnake/stats.rb +131 -0
  93. data/lib/pixelsnake/terminal.rb +84 -0
  94. data/lib/pixelsnake/terrarium.rb +206 -0
  95. data/lib/pixelsnake/tile.rb +32 -0
  96. data/lib/pixelsnake/toolbox.rb +251 -0
  97. data/lib/pixelsnake/trophy.rb +99 -0
  98. data/lib/pixelsnake/tuft.rb +55 -0
  99. data/lib/pixelsnake/upscale.rb +60 -0
  100. data/lib/pixelsnake/version.rb +5 -0
  101. data/lib/pixelsnake.rb +143 -0
  102. metadata +146 -0
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "io/console"
4
+
5
+ module PixelSnake
6
+ # The one object that touches the real terminal. Whatever it changes on the
7
+ # way in, it puts back on the way out, however the way out happens.
8
+ class Terminal
9
+ # Private modes as [number, state while playing]. Leaving puts each one
10
+ # back to the terminal's default, in reverse order.
11
+ MODES = [
12
+ [1049, true], # alternate screen
13
+ [25, false], # visible cursor
14
+ [7, false], # auto-wrap, so drawing the bottom-right cell never scrolls
15
+ [1004, true], # focus in and out reports
16
+ [1003, true], # mouse motion reports
17
+ [1006, true] # mouse coordinates in SGR form
18
+ ].freeze
19
+ # How long the last reports are waited for on the way out.
20
+ SETTLE = 0.03
21
+
22
+ def self.open(input = $stdin, output = $stdout)
23
+ terminal = new(input, output)
24
+ terminal.enter
25
+ yield terminal
26
+ ensure
27
+ terminal&.leave
28
+ end
29
+
30
+ def initialize(input, output)
31
+ @input = input
32
+ @output = output
33
+ @resized = true
34
+ end
35
+
36
+ def enter
37
+ @mode = @input.console_mode
38
+ @input.raw!
39
+ @winch = Signal.trap("WINCH") { @resized = true }
40
+ write(MODES.map { |number, on| switch(number, on) }.join)
41
+ end
42
+
43
+ # Reports the terminal sent before it heard they were switched off, a
44
+ # mouse still moving above all, would land in the shell as stray
45
+ # characters: they get a moment to arrive, and are dropped unread. The
46
+ # terminal's own mode is put back even if that last write fails.
47
+ def leave
48
+ write("\e[0m" + MODES.reverse.map { |number, on| switch(number, !on) }.join)
49
+ sleep(SETTLE)
50
+ @input.iflush
51
+ ensure
52
+ @input.console_mode = @mode
53
+ Signal.trap("WINCH", @winch)
54
+ end
55
+
56
+ def size
57
+ lines, columns = @output.winsize
58
+ [columns, lines]
59
+ end
60
+
61
+ # True once after each change of size, starting with the first call.
62
+ def resized?
63
+ resized = @resized
64
+ @resized = false
65
+ resized
66
+ end
67
+
68
+ # Whatever bytes have arrived, without waiting. Checking first and then
69
+ # reading, rather than using read_nonblock, leaves the descriptor's
70
+ # blocking flag alone; the shell shares it with us.
71
+ def read
72
+ @input.sysread(4096) if @input.wait_readable(0)
73
+ end
74
+
75
+ def write(bytes)
76
+ @output.write(bytes)
77
+ @output.flush
78
+ end
79
+
80
+ private
81
+
82
+ def switch(number, on) = "\e[?#{number}#{on ? "h" : "l"}"
83
+ end
84
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A little glass case beside the settings, where the player's snake goes
5
+ # round and round a fruit in the chosen skin at the speed a game would
6
+ # start at, so a choice can be seen before it is played. A new skin
7
+ # gleams down the snake while flakes of the old one fall away, and
8
+ # turning the shake on gives the case a jolt. Brass brackets hold the
9
+ # glass at its corners.
10
+ #
11
+ # What is inside the glass is drawn on its own small canvas, with the
12
+ # snake's tiles counted from its corner, and laid into the frame. On the
13
+ # fine canvas the glass has a fine canvas of its own, its pavers twelve
14
+ # pixels across like the courtyard's.
15
+ class Terrarium
16
+ TILE = Courtyard::TILE
17
+ COLUMNS = 7
18
+ ROWS = 7
19
+ LENGTH = 8
20
+ WIDTH = COLUMNS * TILE + 4
21
+ HEIGHT = ROWS * TILE + 4
22
+ FOOD = Tile.new(3, 3)
23
+ JOLT = 0.32
24
+
25
+ # The tiles round the edge of the floor, clockwise from the top left.
26
+ PATH = [
27
+ *(0...COLUMNS).map { |column| Tile.new(column, 0) },
28
+ *(1...ROWS).map { |row| Tile.new(COLUMNS - 1, row) },
29
+ *(0...COLUMNS - 1).reverse_each.map { |column| Tile.new(column, ROWS - 1) },
30
+ *(1...ROWS - 1).reverse_each.map { |row| Tile.new(0, row) }
31
+ ].freeze
32
+
33
+ def initialize(rng, x:, y:, skin:, speed:)
34
+ @x = x
35
+ @y = y
36
+ @skin = skin
37
+ @pace = Game::SPEEDS.fetch(speed)
38
+ @view = SnakeView.new(rng, skin:, origin: [0, 0])
39
+ @particles = Particles.new(rng)
40
+ @snake = Snake.new(PATH.first(LENGTH).reverse, PATH[LENGTH - 2].toward(PATH[LENGTH - 1]))
41
+ @step = LENGTH - 1
42
+ @floors = {}
43
+ @glasses = {}
44
+ @elapsed = 0.0
45
+ @time = 0.0
46
+ @jolted = -JOLT
47
+ end
48
+
49
+ # The old skin flakes off every tile as the new one gleams.
50
+ def wear(skin)
51
+ flakes = @skin.flakes.first(4)
52
+ @snake.body.each do |tile|
53
+ @particles.burst(tile.column * TILE + 4, tile.row * TILE + 4, flakes, count: 3, speed: 28, life: 0.35..0.7)
54
+ end
55
+ @skin = skin
56
+ @view.wear(skin)
57
+ end
58
+
59
+ # Keeps the snake where it is between tiles while its pace changes.
60
+ def speed=(speed)
61
+ pace = Game::SPEEDS.fetch(speed)
62
+ @elapsed *= pace / @pace
63
+ @pace = pace
64
+ end
65
+
66
+ def jolt
67
+ @jolted = @time
68
+ end
69
+
70
+ def update(dt)
71
+ @time += dt
72
+ @elapsed += dt
73
+ while @elapsed >= interval
74
+ @elapsed -= interval
75
+ creep
76
+ end
77
+ @view.update(dt)
78
+ @particles.update(dt)
79
+ end
80
+
81
+ def draw(canvas)
82
+ density = canvas.density
83
+ pixels = canvas.pixels
84
+ draw_frame(pixels, density)
85
+ glass = @glasses[density.name] ||= Canvas.new(density.at(COLUMNS * TILE), density.at(ROWS * TILE))
86
+ glass.copy(@floors[density.name] ||= lay_floor(density))
87
+ inside = density.fine? ? Fine.new(glass) : glass
88
+ draw_fruit(inside)
89
+ @view.draw(inside, @snake, @elapsed / interval, FOOD)
90
+ @particles.draw(inside)
91
+ dx, dy = shake.map { |offset| (offset * density.scale).round }
92
+ left = density.at(@x + 2)
93
+ top = density.at(@y + 2)
94
+ pixels.clip(left, top, glass.width, glass.height) do
95
+ pixels.fill(left, top, glass.width, glass.height, Palette[:ink])
96
+ pixels.paste(glass, left + dx, top + dy)
97
+ end
98
+ draw_glare(pixels, density)
99
+ draw_brackets(canvas)
100
+ end
101
+
102
+ private
103
+
104
+ def interval = Game::SLOWEST * @pace
105
+
106
+ def creep
107
+ @step += 1
108
+ @snake.turn(@snake.head.toward(PATH[@step % PATH.size]))
109
+ @snake.advance
110
+ end
111
+
112
+ # Pavers like the courtyard's, in the shade of the case's own rim: a
113
+ # solid band along the top and left, and a dither beyond it.
114
+ def lay_floor(density)
115
+ tile = density.at(TILE)
116
+ floor = Canvas.new(COLUMNS * tile, ROWS * tile)
117
+ ROWS.times do |row|
118
+ COLUMNS.times do |column|
119
+ floor.fill(column * tile, row * tile, tile, tile, Palette[(row + column).even? ? :cobble : :pewter])
120
+ end
121
+ end
122
+ shade = Palette::DARKER[1]
123
+ band = density.at(2)
124
+ floor.shade(0, 0, floor.width, band, shade)
125
+ floor.shade(0, band, band, floor.height - band, shade)
126
+ (band...floor.width).step(2) { |x| floor.shade(x, band, 1, 1, shade) }
127
+ (band + 2...floor.height).step(2) { |y| floor.shade(band, y, 1, 1, shade) }
128
+ floor
129
+ end
130
+
131
+ def draw_fruit(glass)
132
+ x = FOOD.column * TILE
133
+ y = FOOD.row * TILE
134
+ glass.draw(Fruit::SHADOW, x + 2, y + 6)
135
+ glass.draw(Fruit::SPRITE, x, y)
136
+ glass.draw(Fruit::SPARK, x + 1, y + 2) if @time % Fruit::GLINT_EVERY < 0.12
137
+ end
138
+
139
+ # Sunk into the panel: the rim is in shadow along its top and left and
140
+ # catches the light along its bottom and right.
141
+ def draw_frame(canvas, density)
142
+ x = density.at(@x)
143
+ y = density.at(@y)
144
+ width = density.at(@x + WIDTH) - x
145
+ height = density.at(@y + HEIGHT) - y
146
+ canvas.fill(x, y, width, height, Palette[:violet])
147
+ canvas.fill(x, y, width - 1, height - 1, Palette[:ink])
148
+ canvas.fill(x + 1, y + 1, width - 2, height - 2, Palette[:night])
149
+ end
150
+
151
+ # Two faint streaks of light on the glass near its top left corner, as
152
+ # long again on the fine canvas.
153
+ def draw_glare(canvas, density)
154
+ x = density.at(@x)
155
+ y = density.at(@y)
156
+ [[3, 7], [3, 11]].each do |start, length|
157
+ start = density.at(start)
158
+ length = density.at(length)
159
+ length.times { |step| canvas.shade(x + start + step, y + start + length - step, 1, 1, Palette::LIGHTER[1]) }
160
+ end
161
+ end
162
+
163
+ def draw_brackets(canvas)
164
+ right = @x + WIDTH - 3
165
+ bottom = @y + HEIGHT - 3
166
+ [[@x - 1, @y - 1], [right, @y - 1], [@x - 1, bottom], [right, bottom]].zip(BRACKETS) do |(x, y), bracket|
167
+ canvas.draw(bracket, x, y)
168
+ end
169
+ end
170
+
171
+ # The case's own small shake, settling quickly.
172
+ def shake
173
+ since = @time - @jolted
174
+ return [0, 0] if since >= JOLT
175
+
176
+ strength = 2.0 * (1 - since / JOLT)**2
177
+ [(strength * Math.sin(since * 95)).round, (strength * 0.6 * Math.cos(since * 71)).round]
178
+ end
179
+
180
+ LEGEND = { "g" => :gold, "a" => :amber, "o" => :orange, "C" => :cream }.freeze
181
+
182
+ # Top left, top right, bottom left and bottom right, all lit from the
183
+ # top left, each with a rivet.
184
+ BRACKETS = [<<~NW, <<~NE, <<~SW, <<~SE].map { |art| Sprite.parse(art, LEGEND) }.freeze
185
+ ggga
186
+ gCao
187
+ ga..
188
+ ao..
189
+ NW
190
+ gggo
191
+ gaCo
192
+ ..go
193
+ ..ao
194
+ NE
195
+ ga..
196
+ ga..
197
+ gCgg
198
+ oooo
199
+ SW
200
+ ..go
201
+ ..go
202
+ ggCo
203
+ oooo
204
+ SE
205
+ end
206
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # One step on the board.
5
+ Direction = Data.define(:dx, :dy)
6
+
7
+ class Direction
8
+ UP = new(0, -1)
9
+ DOWN = new(0, 1)
10
+ LEFT = new(-1, 0)
11
+ RIGHT = new(1, 0)
12
+ NAMED = { up: UP, down: DOWN, left: LEFT, right: RIGHT }.freeze
13
+
14
+ def self.named(name) = NAMED.fetch(name)
15
+
16
+ def opposite?(other) = dx == -other.dx && dy == -other.dy
17
+
18
+ def reverse = Direction.new(-dx, -dy)
19
+ end
20
+
21
+ # A square of the board, counted from the top left.
22
+ Tile = Data.define(:column, :row) do
23
+ def +(direction) = Tile.new(column + direction.dx, row + direction.dy)
24
+
25
+ def distance(other) = (column - other.column).abs + (row - other.row).abs
26
+
27
+ def neighbors = Direction::NAMED.values.map { |direction| self + direction }
28
+
29
+ # The direction of a neighboring tile.
30
+ def toward(other) = Direction.new(other.column - column, other.row - row)
31
+ end
32
+ end
@@ -0,0 +1,251 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A small tool beside each setting, in the gray of the stone around it
5
+ # until its row is chosen, when it takes its colors and comes alive. The
6
+ # brush, its bristles in the snake's own colors, dabs; the pencil writes;
7
+ # the stopwatch's hand ticks round at the pace a game starts at; the
8
+ # hammer taps while the shake is on; the bell swings and rings, faster
9
+ # the louder the sound; the notes bob, quicker the louder the music; the
10
+ # broom sweeps; and the arrow nods the way out. On the pause panel the
11
+ # bell and the notes keep their colors for as long as there is sound or
12
+ # music, so they show what is on at a glance, and on the music page a
13
+ # single note bobs beside the song that is playing.
14
+ class Toolbox
15
+ def self.brightness(index) = Palette.rgb(index).zip([0.3, 0.59, 0.11]).sum { |value, weight| value * weight }
16
+
17
+ STONE = %i[slate cobble pewter steel ash].map { |name| Palette[name] }.freeze
18
+ # Each color as the stone color nearest it in brightness, taken down
19
+ # enough that a tool keeps its light and shade in gray.
20
+ ASLEEP = Palette.map { |index| STONE.min_by { |stone| (brightness(stone) - brightness(index) * 0.6).abs } }
21
+
22
+ def initialize(preferences)
23
+ @preferences = preferences
24
+ @time = 0.0
25
+ @worn = {}
26
+ end
27
+
28
+ def update(dt)
29
+ @time += dt
30
+ end
31
+
32
+ # The tool for a row, standing at x and centered on text whose top is
33
+ # at y: at work while lit, and in its colors while awake, which unless
34
+ # said otherwise is while lit. Writing keeps the pencil going while a
35
+ # name is being typed.
36
+ def draw(canvas, row, x, y, lit:, awake: lit, writing: false)
37
+ sprite, nudge_x, nudge_y = lit || writing ? moving(row, writing) : [STILL.fetch(row), 0, 0]
38
+ canvas.draw(sprite, x + nudge_x, y + 2 - sprite.height / 2 + nudge_y, colors(row, awake))
39
+ end
40
+
41
+ private
42
+
43
+ # A tool at work, as its frame and how far it has moved.
44
+ def moving(row, writing)
45
+ case row
46
+ when "SNAKE" then [BRUSH, 0, -beat(0.9, 0.3)]
47
+ when "NAME" then [PENCIL[beat(writing ? 0.24 : 1.2, writing ? 0.12 : 0.36, 4) % 2], 0, 0]
48
+ when "SPEED" then [WATCH[(@time / pace).floor % WATCH.size], 0, 0]
49
+ when "SHAKE" then [HAMMER[@preferences.shake? ? beat(0.9, 0.12) : 0], 0, 0]
50
+ when "SOUND" then [BELL[ringing], 0, 0]
51
+ when "MUSIC" then [NOTES, 0, -bobbing]
52
+ when "SONG" then [NOTE, 0, -bobbing]
53
+ when "CLEAR RECORDS" then [BROOM[(@time / 0.22).floor % 2], 0, 0]
54
+ when "BACK" then [ARROW, -beat(0.8, 0.25), 0]
55
+ end
56
+ end
57
+
58
+ def colors(row, awake)
59
+ if row == "SNAKE" then skin(awake)
60
+ elsif awake then Palette::IDENTITY
61
+ else ASLEEP
62
+ end
63
+ end
64
+
65
+ # One for the first part of every period, for a nod, a dab or a tap;
66
+ # counted in strokes when a stroke is given.
67
+ def beat(period, part, strokes = 1)
68
+ into = @time % period
69
+ return 0 if into >= part
70
+
71
+ strokes == 1 ? 1 : (into / part * strokes).floor + 1
72
+ end
73
+
74
+ # The snake's first move takes this long, a tick of the watch.
75
+ def pace = Game::SLOWEST * Game::SPEEDS.fetch(@preferences.speed)
76
+
77
+ # Left, upright, right, upright: quicker the louder the sound, and still
78
+ # with it off.
79
+ def ringing
80
+ volume = Speaker::VOLUMES.keys.index(@preferences.volume)
81
+ return 1 if volume.zero?
82
+
83
+ [0, 1, 2, 1][(@time * (2 + volume * 2)).floor % 4]
84
+ end
85
+
86
+ # Up a pixel and down, quicker the louder the music, and still with it
87
+ # off.
88
+ def bobbing
89
+ volume = Speaker::VOLUMES.keys.index(@preferences.music)
90
+ volume.zero? ? 0 : beat(1.2 - volume * 0.25, 0.18)
91
+ end
92
+
93
+ def skin(awake)
94
+ worn = @preferences.skin.map
95
+ @worn[[@preferences.skin, awake]] ||= awake ? worn : worn.tr(Palette::IDENTITY, ASLEEP)
96
+ end
97
+
98
+ LEGEND = {
99
+ "k" => :ink, "h" => :ash, "n" => :sand, "t" => :steel, "p" => :pewter,
100
+ "P" => :pine, "J" => :jade, "F" => :fern, "L" => :leaf, "M" => :lime,
101
+ "w" => :wine, "o" => :orange, "a" => :amber, "g" => :gold, "C" => :cream, "B" => :blush
102
+ }.freeze
103
+
104
+ def self.art(*grids) = grids.map { |grid| Sprite.parse(grid, LEGEND) }
105
+
106
+ BRUSH, = art(<<~ART)
107
+ .....LM
108
+ ....FLF
109
+ ...JFJ.
110
+ ..hPJ..
111
+ .nh....
112
+ ao.....
113
+ w......
114
+ ART
115
+
116
+ PENCIL = art(<<~UP, <<~DOWN)
117
+ .....BB
118
+ ....hBB
119
+ ...gah.
120
+ ..gaa..
121
+ .gaa...
122
+ Cga....
123
+ kC.....
124
+ UP
125
+ .......
126
+ .....BB
127
+ ....hBB
128
+ ...gah.
129
+ ..gaa..
130
+ .Cga...
131
+ kkC....
132
+ DOWN
133
+
134
+ # The hand at each eighth of a turn, clockwise from twelve.
135
+ WATCH = art(*[[3, 2], [4, 2], [4, 3], [4, 4], [3, 4], [2, 4], [2, 3], [2, 2]].map do |hand_x, hand_y|
136
+ face = <<~ART.lines(chomp: true).map(&:dup)
137
+ ...a...
138
+ ..hhh..
139
+ .hCCCh.
140
+ hCCCCCh
141
+ hCCkCCh
142
+ hCCCCCh
143
+ .hCCCh.
144
+ ..hhh..
145
+ ART
146
+ face[hand_y + 1][hand_x] = "k"
147
+ face.join("\n")
148
+ end)
149
+
150
+ HAMMER = art(<<~RAISED, <<~DOWN)
151
+ .hhhh..
152
+ .ttttp.
153
+ ...o...
154
+ ...a...
155
+ ...o...
156
+ ...a...
157
+ .......
158
+ RAISED
159
+ .......
160
+ .hhhh..
161
+ .ttttp.
162
+ g..o..g
163
+ ...a...
164
+ ...o...
165
+ ...a...
166
+ DOWN
167
+
168
+ # Swung left, hanging, swung right.
169
+ BELL = art(<<~LEFT, <<~HANGING, <<~RIGHT)
170
+ ...g...
171
+ ..ga...
172
+ .gaao..
173
+ .gaao..
174
+ gaaaao.
175
+ oooooo.
176
+ ...w...
177
+ .......
178
+ LEFT
179
+ ...g...
180
+ ..gaa..
181
+ .gaaao.
182
+ .gaaao.
183
+ gaaaaao
184
+ ooooooo
185
+ ...w...
186
+ .......
187
+ HANGING
188
+ ...g...
189
+ ...aa..
190
+ ..gaao.
191
+ ..gaao.
192
+ .gaaaao
193
+ .oooooo
194
+ ...w...
195
+ .......
196
+ RIGHT
197
+
198
+ NOTES, = art(<<~ART)
199
+ ...gggg
200
+ ..gaaoo
201
+ ..a...o
202
+ ..a...o
203
+ .ga..go
204
+ gaao.ao
205
+ .oo.oo.
206
+ ART
207
+
208
+ NOTE, = art(<<~ART)
209
+ ..gg...
210
+ ..gaa..
211
+ ..a..o.
212
+ ..a..o.
213
+ .ga....
214
+ gaao...
215
+ .oo....
216
+ ART
217
+
218
+ BROOM = art(<<~LEFT, <<~RIGHT)
219
+ ...o...
220
+ ...a...
221
+ ...o...
222
+ ..waw..
223
+ .nnnnn.
224
+ nnnnnn.
225
+ n.n.n..
226
+ LEFT
227
+ ...o...
228
+ ...a...
229
+ ...o...
230
+ ..waw..
231
+ .nnnnn.
232
+ .nnnnnn
233
+ ..n.n.n
234
+ RIGHT
235
+
236
+ ARROW, = art(<<~ART)
237
+ .......
238
+ ..g....
239
+ .gg....
240
+ ggggggg
241
+ .gg....
242
+ ..g....
243
+ .......
244
+ ART
245
+
246
+ STILL = { "SNAKE" => BRUSH, "NAME" => PENCIL[0], "SPEED" => WATCH[0], "SHAKE" => HAMMER[0], "SOUND" => BELL[1],
247
+ "MUSIC" => NOTES, "SONG" => NOTE, "CLEAR RECORDS" => BROOM[0], "BACK" => ARROW }.freeze
248
+
249
+ private_class_method :brightness, :art
250
+ end
251
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A golden cup on a stone plinth, to stand beside the high scores' title.
5
+ # It rises from behind the panel as the scores open and, as it reaches
6
+ # the top, throws a handful of sparkles out of its mouth. After that the
7
+ # light glints across it every so often, as it does across the logo. It
8
+ # is placed in points and drawn in the canvas's pixels, the glint across
9
+ # the cup as it is drawn there.
10
+ class Trophy
11
+ WIDTH = 16
12
+ HEIGHT = 15
13
+ # The rows of gold above the plinth, where the light can glint.
14
+ GOLD = 12
15
+ RISE_FROM = 0.15
16
+ RISE = 0.5
17
+ BELOW = HEIGHT + 2
18
+ # Out of its mouth when it first reaches the top on its way up.
19
+ POP = RISE_FROM + RISE * 0.6
20
+ SPARKLES = %i[white cream gold amber].freeze
21
+ GLINT = 0.5
22
+ GLINT_EVERY = 4.0
23
+ GLINT_STEPS = [1, 2, 1].freeze
24
+
25
+ # Standing on the ground at y, with its first glint at a moment of its
26
+ # own so that two cups take turns.
27
+ def initialize(x, ground, particles, glint_from:)
28
+ @x = x
29
+ @y = ground - HEIGHT + 1
30
+ @particles = particles
31
+ @glint_from = glint_from
32
+ @time = 0.0
33
+ end
34
+
35
+ def update(dt)
36
+ before = @time
37
+ @time += dt
38
+ pop if (before...@time).cover?(POP)
39
+ end
40
+
41
+ # Behind the panel, so it can rise from behind it.
42
+ def draw(canvas)
43
+ density = canvas.density
44
+ cup = density.pick(CUP, CUP.fine)
45
+ x = density.at(@x)
46
+ top = density.at(@y + ((1 - Ease.out_back(Ease.progress(@time, RISE_FROM, RISE))) * BELOW).round)
47
+ canvas.pixels.draw(cup, x, top)
48
+ draw_glint(canvas.pixels, cup, x, top, density.at(GOLD))
49
+ end
50
+
51
+ private
52
+
53
+ def pop
54
+ @particles.burst(@x + WIDTH / 2, @y + 1, SPARKLES, count: 9, speed: 34, gravity: 90.0, life: 0.5..0.9, sparkle: true)
55
+ end
56
+
57
+ # A slanted band of light crossing the gold, and only the gold: not the
58
+ # stone, the dark of the cup's mouth or what is behind it. Gold is how
59
+ # many of the cup's rows, from its top, the band crosses.
60
+ def draw_glint(pixels, cup, x, top, gold)
61
+ moment = (@time - @glint_from) % GLINT_EVERY
62
+ return if @time < @glint_from || moment > GLINT
63
+
64
+ front = (Ease.in_out_sine(moment / GLINT) * (cup.width + gold)).round
65
+ gold.times do |row|
66
+ GLINT_STEPS.each_with_index do |steps, behind|
67
+ column = front - row - behind
68
+ next unless column.between?(0, cup.width - 1) && GLINTING.include?(cup[column, row])
69
+
70
+ pixels.shade(x + column, top + row, 1, 1, Palette::LIGHTER[steps])
71
+ end
72
+ end
73
+ end
74
+
75
+ LEGEND = {
76
+ "C" => :cream, "g" => :gold, "a" => :amber, "o" => :orange, "w" => :wine,
77
+ "b" => :cobble, "p" => :pewter, "t" => :steel, "h" => :ash
78
+ }.freeze
79
+ GLINTING = %i[cream gold amber orange].map { |name| Palette[name] }.freeze
80
+
81
+ CUP = Sprite.parse(<<~ART, LEGEND)
82
+ ..gggggggggggg..
83
+ ggCwwwwwwwwwwagg
84
+ g.gCaaaaaaaaao.o
85
+ g.gCaaaaaaaaao.o
86
+ .ggCaaaaaaaaaoo.
87
+ ...gCaaaaaaao...
88
+ ....gaaaaaaao...
89
+ .....gaaaaao....
90
+ .......gao......
91
+ .......gao......
92
+ ......gaaao.....
93
+ .....gaaaaao....
94
+ ....httttttbb...
95
+ ...htttttttttb..
96
+ ...pppppppppppb.
97
+ ART
98
+ end
99
+ end