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,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Three, two, one, go. Each one drops onto the field just above the
5
+ # snake's head, so the eye finds the snake, while its shadow gathers under
6
+ # it; it squashes as it lands and springs back, holds a moment, then pops
7
+ # away in a dither. Each one is heard as it lands, and GO! brightest. It
8
+ # is placed in points and squashed in the pixels of the canvas, from the
9
+ # signs painted for its density.
10
+ class Countdown
11
+ BEAT = 0.5
12
+ FALL = 0.16
13
+ SETTLE = 0.2
14
+ LEAVE = 0.1
15
+ HEIGHT = 40
16
+
17
+ def initialize(speaker)
18
+ @speaker = speaker
19
+ @time = 0.0
20
+ @signs = [["3", :gold], ["2", :gold], ["1", :gold], ["GO!", :jade]].map { |text, style| Lettering.new(text, style) }
21
+ end
22
+
23
+ def update(dt)
24
+ before = @time
25
+ @time += dt
26
+ landed = @signs.each_index.find { |beat| (before...@time).cover?(beat * BEAT + FALL) }
27
+ @speaker.play(landed == @signs.size - 1 ? :go : :count) if landed
28
+ end
29
+
30
+ def done? = @time >= @signs.size * BEAT
31
+
32
+ # Whether GO! has landed, which is when the music starts.
33
+ def go? = @time >= (@signs.size - 1) * BEAT + FALL
34
+
35
+ # Ground is where the bottom of the sign comes to rest.
36
+ def draw(canvas, center, ground)
37
+ return if done?
38
+
39
+ density = canvas.density
40
+ pixels = canvas.pixels
41
+ beat = (@time / BEAT).floor
42
+ moment = @time - beat * BEAT
43
+ sign = @signs[beat].sign(density)
44
+ width_by, height_by = squash(moment)
45
+ sprite = sign.sprite.scale(width_by, height_by)
46
+ drop = (density.at(HEIGHT) * (1 - Ease.in_quad(Ease.progress(moment, 0.0, FALL)))).round
47
+ center = density.at(center)
48
+ ground = density.at(ground)
49
+ draw_shadow(pixels, density, center, ground, sign.width, Ease.progress(moment, 0.0, FALL))
50
+ x = center - sprite.width / 2
51
+ y = ground - sprite.height - drop
52
+ leaving = Ease.progress(moment, BEAT - LEAVE, LEAVE)
53
+ leaving.zero? ? pixels.draw(sprite, x, y) : pixels.draw_faded(sprite, x, y, ((1 - leaving) * 16).round)
54
+ end
55
+
56
+ private
57
+
58
+ # Wide and flat on landing, then a little tall, then itself; slightly
59
+ # grown as it leaves.
60
+ def squash(moment)
61
+ landed = Ease.progress(moment, FALL, SETTLE)
62
+ if moment < FALL
63
+ [0.85, 1.2]
64
+ elsif landed < 1
65
+ height_by = 1 - 0.3 * (1 - landed)**2 * Math.cos(landed * Math::PI * 2.2)
66
+ [1 + (1 - height_by) * 0.8, height_by]
67
+ else
68
+ grow = 1 + 0.25 * Ease.progress(moment, BEAT - LEAVE, LEAVE)
69
+ [grow, grow]
70
+ end
71
+ end
72
+
73
+ def draw_shadow(canvas, density, center, ground, width, closeness)
74
+ half = ((width / 2 - 2) * (0.35 + 0.65 * closeness)).round
75
+ inset = density.pick(2, 3)
76
+ canvas.shade(center - half, ground - 1, half * 2, 1, Palette::DARKER[1])
77
+ canvas.shade(center - half + inset, ground, half * 2 - inset * 2, 1, Palette::DARKER[1])
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,306 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The place itself: a field of pavers in dusk shade inside a low stone wall
5
+ # whose tops still catch the last light from the upper left. The stone is
6
+ # painted once for each density; only the grass on the wall moves. On the
7
+ # fine canvas the same stones are painted again with the wear the extra
8
+ # pixels can show, a dent or two in each face; the field stays plain, so
9
+ # nothing on it competes with the snake.
10
+ #
11
+ # In a game the wall starts below the HUD band. Behind the menus there is
12
+ # no band: the wall runs round the whole screen and the pavers carry on up
13
+ # to it. The game's rows are in the same place either way.
14
+ #
15
+ # The field is as many columns wide as it is laid for, in the middle of
16
+ # the scenes' frame: 27, or 29 where the terminal has room for a column
17
+ # more each side. A game's courtyard keeps the field its game is played
18
+ # on; one behind the menus has no field to keep, and is laid again for
19
+ # the room of whatever canvas it is drawn on. The side walls reach out
20
+ # to the canvas's edges, thicker on one wider than the field was laid
21
+ # for and cut off by the edges of one narrower, as when the terminal
22
+ # changes size in the middle of a game.
23
+ class Courtyard
24
+ TILE = 8
25
+ COLUMNS = 27
26
+ WIDE = COLUMNS + 2
27
+ ROWS = 12
28
+ SIDE_WALL = 10
29
+ END_WALL = 6
30
+ HUD = 14
31
+ TOP = HUD + END_WALL
32
+ HEIGHT = ROWS * TILE
33
+ BOTTOM = TOP + HEIGHT
34
+
35
+ # A stone of the wall: its slot in points, the seam before it included,
36
+ # and the color of its face.
37
+ Stone = Data.define(:x, :y, :width, :height, :face)
38
+
39
+ attr_reader :columns, :left, :right
40
+
41
+ # How many columns a canvas has room for between walls of their own
42
+ # thickness.
43
+ def self.room(canvas) = (canvas.density.point(canvas.pixels.width) - 2 * SIDE_WALL) / TILE
44
+
45
+ # The columns of a game's field, or none behind the menus.
46
+ def initialize(rng, hud: true, columns: nil)
47
+ @rng = rng
48
+ @hud = hud
49
+ @kept = columns
50
+ @top = hud ? HUD : 0
51
+ @ground = @top + END_WALL
52
+ @time = 0.0
53
+ lay(columns || COLUMNS)
54
+ end
55
+
56
+ # The point at the top left corner of a tile.
57
+ def corner(column, row) = [@left + column * TILE, TOP + row * TILE]
58
+
59
+ def update(dt)
60
+ @time += dt
61
+ end
62
+
63
+ # The painting for the canvas's density and edges, its frame on the
64
+ # canvas's frame.
65
+ def draw(canvas)
66
+ room = Courtyard.room(canvas)
67
+ lay(room) unless @kept || room == @columns
68
+ density = canvas.density
69
+ pixels = canvas.pixels
70
+ outer = [@left - SIDE_WALL, density.point(pixels.left)].min
71
+ painting = @painted[[density.name, outer]] ||= paint(density, outer)
72
+ pixels.paste(painting, painting.left, 0)
73
+ (@tufts[outer] ||= plant_tufts(outer)).each { |tuft| tuft.draw(canvas, @time) }
74
+ end
75
+
76
+ private
77
+
78
+ # The field so many columns wide in the middle of the frame, and the
79
+ # stones and the grass around it.
80
+ def lay(columns)
81
+ @columns = columns
82
+ @left = (App::WIDTH - columns * TILE) / 2
83
+ @right = @left + columns * TILE
84
+ @stones = lay_stones
85
+ @phases = tuft_spots(@left - SIDE_WALL).map { @rng.rand * Math::PI * 2 }
86
+ @painted = {}
87
+ @tufts = {}
88
+ end
89
+
90
+ # One ring of stones around the field in runs of chance lengths, the
91
+ # side walls as thick as the frame leaves them.
92
+ def lay_stones
93
+ h = App::HEIGHT
94
+ width = @columns * TILE
95
+ stones = []
96
+ along(@left, width, 12..22) { |x, length| stones << stone(x, @top + 1, length, END_WALL - 3) }
97
+ along(@left, width, 12..22) { |x, length| stones << stone(x, BOTTOM + 1, length, END_WALL - 2) }
98
+ [@left - SIDE_WALL + 1, @right + 1].each do |x|
99
+ along(@top + 1, h - @top - 2, 10..18) { |y, length| stones << stone(x, y, SIDE_WALL - 2, length) }
100
+ end
101
+ stones
102
+ end
103
+
104
+ # A last stone too short to be one goes into the stone before it.
105
+ def along(start, total, range)
106
+ lengths = []
107
+ lengths << [@rng.rand(range), total - lengths.sum].min while lengths.sum < total
108
+ stub = lengths.pop if lengths.size > 1 && lengths.last < range.min
109
+ lengths[-1] += stub if stub
110
+ lengths.each do |length|
111
+ yield start, length
112
+ start += length
113
+ end
114
+ end
115
+
116
+ def stone(x, y, width, height) = Stone.new(x, y, width, height, @rng.rand < 0.3 ? :steel : :ash)
117
+
118
+ # Outer is the point the left wall starts at, out at the canvas's edge
119
+ # or further.
120
+ def paint(density, outer)
121
+ Painting.new(density, outer:, left: @left, right: @right, top: @top, ground: @ground).paint(@stones)
122
+ end
123
+
124
+ def plant_tufts(outer)
125
+ tuft_spots(outer).zip(@phases).map { |(x, y), phase| Tuft.new(x, y, phase) }
126
+ end
127
+
128
+ # Placed by hand, where grass can catch the light and away from the
129
+ # HUD's words: on the left wall from its outer edge, on the right from
130
+ # the field, and up off the top wall only into the band above it.
131
+ def tuft_spots(outer)
132
+ (@hud ? [[96, @top - 3], [133, @top - 3]] : []) + [[outer + 1, 40], [outer + 1, 100], [@right + 2, 60]]
133
+ end
134
+
135
+ # The stone at one density, painted in its pixels: what is a point wide
136
+ # in the standard picture, a seam or an edge or an outline, is a pixel
137
+ # wide in the fine one too. The picture is as wide as the walls reach,
138
+ # its frame where the scenes' frame is.
139
+ class Painting
140
+ def initialize(density, outer:, left:, right:, top:, ground:)
141
+ @d = density
142
+ @outer = outer
143
+ @left = left
144
+ @right = right
145
+ @top = top
146
+ @ground = ground
147
+ @canvas = Canvas.new(density.at(App::WIDTH - 2 * outer), density.height, origin: -density.at(outer))
148
+ end
149
+
150
+ def paint(stones)
151
+ build_wall(stones)
152
+ lay_pavers
153
+ grow_moss
154
+ @canvas
155
+ end
156
+
157
+ private
158
+
159
+ def at(point) = @d.at(point)
160
+
161
+ def fill(x, y, width, height, color) = @canvas.fill(x, y, width, height, Palette[color])
162
+
163
+ # The top wall faces us, so under its stones we see a strip of their
164
+ # shaded fronts. The wall is outlined once outside and once along the
165
+ # field.
166
+ def build_wall(stones)
167
+ outer = at(@outer)
168
+ far = at(App::WIDTH - @outer)
169
+ height = @d.height
170
+ top = at(@top)
171
+ ground = at(@ground)
172
+ left = at(@left)
173
+ right = at(@right)
174
+ bottom = at(BOTTOM)
175
+ fill(outer, top, far - outer, height - top, :slate)
176
+ stones.each { |each| lay(widened(each)) }
177
+ fill(left, ground - 2, right - left, 1, :pewter)
178
+ fill(outer, top, far - outer, 1, :ink)
179
+ fill(outer, height - 1, far - outer, 1, :ink)
180
+ fill(outer, top, 1, height - top, :ink)
181
+ fill(far - 1, top, 1, height - top, :ink)
182
+ fill(left - 1, ground - 1, right - left + 2, 1, :ink)
183
+ fill(left - 1, bottom, right - left + 2, 1, :ink)
184
+ fill(left - 1, ground - 1, 1, bottom - ground + 2, :ink)
185
+ fill(right, ground - 1, 1, bottom - ground + 2, :ink)
186
+ end
187
+
188
+ # The side walls' stones were laid as thick as the frame leaves them;
189
+ # where the walls reach further out, so do the stones.
190
+ def widened(stone)
191
+ further = @left - SIDE_WALL - @outer
192
+ if stone.x < @left then stone.with(x: stone.x - further, width: stone.width + further)
193
+ elsif stone.x >= @right then stone.with(width: stone.width + further)
194
+ else stone
195
+ end
196
+ end
197
+
198
+ # A stone in its slot, leaving a one pixel seam before it: lit along
199
+ # its top and left, shaded along its bottom and right, corners worn
200
+ # round, and on the fine canvas pitted here and there.
201
+ def lay(stone)
202
+ across = stone.width > stone.height
203
+ x = at(stone.x) + (across ? 1 : 0)
204
+ y = at(stone.y) + (across ? 0 : 1)
205
+ width = at(stone.x + stone.width) - x
206
+ height = at(stone.y + stone.height) - y
207
+ face = stone.face
208
+ light = face == :ash ? :sand : :ash
209
+ fill(x, y, width, height, face)
210
+ dent(x, y, width, height, light) if @d.fine?
211
+ fill(x, y, width, 1, light)
212
+ fill(x, y, 1, height, light)
213
+ fill(x + 1, y + height - 1, width - 1, 1, :pewter)
214
+ fill(x + width - 1, y + 1, 1, height - 1, :pewter)
215
+ [[x, y], [x + width - 1, y], [x, y + height - 1], [x + width - 1, y + height - 1]].each do |corner_x, corner_y|
216
+ @canvas.plot(corner_x, corner_y, Palette[:slate])
217
+ end
218
+ end
219
+
220
+ # A dent or two in a stone's face: a pixel of shade with the light
221
+ # catching its far edge below and to the right. Where they go comes
222
+ # from where the stone is, so it always wears the same.
223
+ def dent(x, y, width, height, light)
224
+ return if width < 7 || height < 6
225
+
226
+ dice = Random.new(x * 997 + y)
227
+ (width * height / 90).clamp(1, 3).times do
228
+ dent_x = x + 2 + dice.rand(width - 5)
229
+ dent_y = y + 2 + dice.rand(height - 5)
230
+ @canvas.plot(dent_x, dent_y, Palette[:pewter])
231
+ @canvas.plot(dent_x + 1, dent_y + 1, Palette[light])
232
+ end
233
+ end
234
+
235
+ # Pavers are laid on the game's grid, so above its first row only the
236
+ # part of a row that reaches the wall shows.
237
+ def lay_pavers
238
+ left = at(@left)
239
+ ground = at(@ground)
240
+ bottom = at(BOTTOM)
241
+ above = ((TOP - @ground) / TILE.to_f).ceil
242
+ @canvas.clip(left, ground, at(@right) - left, bottom - ground) do
243
+ (-above...ROWS).each do |row|
244
+ ((@right - @left) / TILE).times { |column| paver(column, row) }
245
+ end
246
+ end
247
+ cast_shadow(left, ground, at(@right) - left, at(@ground + 3) - ground, at(@ground + 2) - ground)
248
+ cast_shadow(left, at(@ground + 3), at(@left + 4) - left, bottom - at(@ground + 3), at(@left + 3) - left)
249
+ end
250
+
251
+ def paver(column, row)
252
+ x = @left + column * TILE
253
+ y = TOP + row * TILE
254
+ left = at(x)
255
+ top = at(y)
256
+ size = at(x + TILE) - left
257
+ fill(left, top, size, size, (row + column).even? ? :cobble : :pewter)
258
+ end
259
+
260
+ # The low sun throws the top and left walls' shadows onto the field:
261
+ # solid near the wall, then a row of dither for a soft edge. Shade is
262
+ # the stone underneath one step darker, so the pavers still show.
263
+ def cast_shadow(x, y, width, height, solid)
264
+ shade = Palette::DARKER[1]
265
+ (y...y + height).each do |pixel_y|
266
+ (x...x + width).each do |pixel_x|
267
+ depth = width > height ? pixel_y - y : pixel_x - x
268
+ next if depth >= solid && (pixel_x + pixel_y).odd?
269
+
270
+ @canvas.plot(pixel_x, pixel_y, shade.getbyte(@canvas[pixel_x, pixel_y]))
271
+ end
272
+ end
273
+ end
274
+
275
+ # Placed by hand, where moss gathers in the shade low on the walls: on
276
+ # the left wall from its outer edge, on the right from the field, and
277
+ # on the top wall from its top edge.
278
+ def grow_moss
279
+ surface = @d.fine? ? Fine.new(@canvas) : @canvas
280
+ spots = [[@outer + 2, 70], [@right + 3, 98], [@right + 2, 27],
281
+ [46, BOTTOM + 2], [171, BOTTOM + 1], [118, @top + 2]]
282
+ spots.each_with_index { |(x, y), index| surface.draw(MOSS[index % MOSS.size], x, y) }
283
+ end
284
+ end
285
+
286
+ MOSS_COLORS = { "p" => :pine, "j" => :jade, "f" => :fern }.freeze
287
+ MOSS = [
288
+ Sprite.parse(<<~ART, MOSS_COLORS),
289
+ .fj..
290
+ fjjjp
291
+ .pjp.
292
+ ART
293
+ Sprite.parse(<<~ART, MOSS_COLORS),
294
+ ..fj.
295
+ .fjjp
296
+ fjpp.
297
+ .p...
298
+ ART
299
+ Sprite.parse(<<~ART, MOSS_COLORS)
300
+ .f.
301
+ fjp
302
+ .p.
303
+ ART
304
+ ].freeze
305
+ end
306
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module PixelSnake
6
+ # A day as the files keep it, 2026-11-17, and as the tables and the stats
7
+ # tell it, 17 NOV.
8
+ module Day
9
+ MONTHS = %w[JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC].freeze
10
+
11
+ # A day as it was saved, if it is one.
12
+ def self.read(saved)
13
+ Date.iso8601(saved.to_s).iso8601
14
+ rescue Date::Error
15
+ nil
16
+ end
17
+
18
+ def self.told(day)
19
+ _year, month, date = day.split("-").map(&:to_i)
20
+ "#{date} #{MONTHS[month - 1]}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The end of a run, played out: a beat of stillness, a flash, a shake
5
+ # that dies away, then the snake comes apart tile by tile from the head
6
+ # to the tail, each piece bursting into scales of its skin. However long
7
+ # the snake, the coming apart takes about a second. The bursts are heard
8
+ # too, the head loudest and then each a step lower down to the tail; a
9
+ # long snake bursts faster than the ear can follow, so then only some
10
+ # are.
11
+ class Death
12
+ STILL = 0.14
13
+ FLASH = 0.12
14
+ SHAKE = 0.34
15
+ APART = 0.95
16
+ PIECE = 0.07
17
+ LINGER = 0.55
18
+ PUFF = 0.08
19
+ POPS = 10
20
+ HEARD = 0.035
21
+
22
+ attr_reader :popped
23
+
24
+ def initialize(courtyard, tiles, particles, skin, speaker)
25
+ @courtyard = courtyard
26
+ @tiles = tiles
27
+ @particles = particles
28
+ @speaker = speaker
29
+ @scales = skin.flakes.first(5)
30
+ @head = [:cream, skin.eyes.last, *skin.flakes.first(4)]
31
+ @time = 0.0
32
+ @popped = 0
33
+ @stagger = [PIECE, APART / @tiles.size].min
34
+ @puffs = []
35
+ @heard = -HEARD
36
+ end
37
+
38
+ def update(dt)
39
+ @time += dt
40
+ pop while @popped < @tiles.size && @time >= first_pop + @popped * @stagger
41
+ end
42
+
43
+ def done? = @popped == @tiles.size && @time >= first_pop + @tiles.size * @stagger + LINGER
44
+
45
+ def stunned? = @time < first_pop
46
+
47
+ # How many ramp steps lighter the whole screen is: two for the first
48
+ # half of the flash, then one.
49
+ def flash
50
+ moment = @time - STILL
51
+ moment.between?(0, FLASH) ? 2 - (moment / FLASH * 2).floor : 0
52
+ end
53
+
54
+ # The camera offset, shaking hard at first and settling quickly.
55
+ def shake
56
+ moment = @time - STILL
57
+ return [0, 0] unless moment.between?(0, SHAKE)
58
+
59
+ strength = 3.0 * (1 - moment / SHAKE)**2
60
+ [(strength * Math.sin(moment * 95)).round, (strength * 0.6 * Math.cos(moment * 71)).round]
61
+ end
62
+
63
+ # A ring of light where each piece has just burst.
64
+ def draw(canvas)
65
+ @puffs.each do |x, y, at|
66
+ next if @time - at > PUFF
67
+
68
+ canvas.draw(@time - at < PUFF / 2 ? SMALL_PUFF : PUFF_RING, x - 4, y - 4)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def first_pop = STILL + SHAKE * 0.6
75
+
76
+ def pop
77
+ x, y = @courtyard.corner(@tiles[@popped].column, @tiles[@popped].row)
78
+ head = @popped.zero?
79
+ @particles.burst(x + 4, y + 4, head ? @head : @scales, count: head ? 14 : 6, speed: head ? 55 : 38)
80
+ @puffs << [x + 4, y + 4, @time]
81
+ sound(x + 4)
82
+ @popped += 1
83
+ end
84
+
85
+ def sound(x)
86
+ return @speaker.play(:burst, at: x) if @popped.zero?
87
+ return if @time - @heard < HEARD
88
+
89
+ @heard = @time
90
+ @speaker.play(:pop, (@popped - 1) * POPS / (@tiles.size - 1), at: x)
91
+ end
92
+
93
+ PUFF_RING = Sprite.parse(<<~ART, "c" => :cream)
94
+ ..cccc..
95
+ .c....c.
96
+ c......c
97
+ c......c
98
+ c......c
99
+ c......c
100
+ .c....c.
101
+ ..cccc..
102
+ ART
103
+
104
+ SMALL_PUFF = Sprite.parse(<<~ART, "c" => :cream, "w" => :white)
105
+ ........
106
+ ........
107
+ ...cc...
108
+ ..cwwc..
109
+ ..cwwc..
110
+ ...cc...
111
+ ........
112
+ ........
113
+ ART
114
+ end
115
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Frame rate, time spent per frame and bytes sent per frame, averaged over
5
+ # each second. Hidden until F3.
6
+ class DebugOverlay
7
+ def initialize
8
+ @shown = false
9
+ @lines = []
10
+ start_window(nil)
11
+ end
12
+
13
+ def toggle
14
+ @shown = !@shown
15
+ end
16
+
17
+ def record(now, work, bytes)
18
+ @window_start ||= now
19
+ @frames += 1
20
+ @work += work
21
+ @bytes += bytes
22
+ @peak = [@peak, bytes].max
23
+ return if now - @window_start < 1
24
+
25
+ @lines = [
26
+ "#{@frames} FPS",
27
+ format("%.1f MS", @work * 1000 / @frames),
28
+ "#{@bytes / @frames} B AVG",
29
+ "#{@peak} B MAX"
30
+ ]
31
+ start_window(now)
32
+ end
33
+
34
+ def draw(canvas)
35
+ return unless @shown
36
+
37
+ canvas.fill(canvas.left, 0, 44, @lines.size * 7 + 3, Palette[:ink])
38
+ @lines.each_with_index do |line, index|
39
+ Font::SMALL.draw(canvas, line, canvas.left + 2, 2 + index * 7, Palette[:lime])
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def start_window(now)
46
+ @window_start = now
47
+ @frames = 0
48
+ @work = 0.0
49
+ @bytes = 0
50
+ @peak = 0
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The attract demo behind the title: the real game on the real courtyard
5
+ # with the player's snake, played again from the player's own best games,
6
+ # one after another, best first, or by the autopilot until there are
7
+ # some. When the snake crashes it comes apart as it would in play,
8
+ # without the flash and shake that would pull the eye from the menu, and
9
+ # the next game begins. It plays in silence: the menu in front of it is
10
+ # what is heard.
11
+ #
12
+ # Only games played on a field as wide as the terminal has room for are
13
+ # shown, and when the room changes the next game begins at once.
14
+ class Demo
15
+ # The best this many games take turns.
16
+ SHOWN = 10
17
+
18
+ attr_reader :game
19
+
20
+ def initialize(stage, recordings = [])
21
+ @stage = stage
22
+ @rng = stage.rng
23
+ @skin = stage.preferences.skin
24
+ @recordings = recordings
25
+ @played = 0
26
+ @particles = Particles.new(@rng)
27
+ start
28
+ end
29
+
30
+ def update(dt)
31
+ start if @courtyard.columns != @stage.columns
32
+ [@courtyard, @snake, @fruit, @rare, @particles].each { |part| part.update(dt) }
33
+ @death&.update(dt)
34
+ start if @death&.done?
35
+ @metronome.advance(dt) do
36
+ @pilot.steer
37
+ play(@game.tick)
38
+ end
39
+ end
40
+
41
+ def draw(canvas)
42
+ @courtyard.draw(canvas)
43
+ @fruit.draw(canvas)
44
+ @rare.draw(canvas)
45
+ @snake.draw(canvas, @game.snake, @metronome.progress, @game.food,
46
+ popped: @death&.popped || 0, stunned: @death&.stunned? || false)
47
+ @death&.draw(canvas)
48
+ @particles.draw(canvas)
49
+ end
50
+
51
+ private
52
+
53
+ def start
54
+ columns = @stage.columns
55
+ fitting = @recordings.select { |recording| recording.columns == columns }.first(SHOWN)
56
+ recording = fitting[@played % fitting.size] unless fitting.empty?
57
+ @played += 1
58
+ lay(columns) unless @courtyard&.columns == columns
59
+ @game = recording&.game || Game.new(Board.new(columns, Courtyard::ROWS), @rng)
60
+ @pilot = recording ? Playback.new(@game, recording.turns) : Autopilot.new(@game, @rng)
61
+ @metronome = Metronome.new(@game)
62
+ @rare = RareFruit.new(@courtyard, @particles, Speaker::SILENT)
63
+ @death = nil
64
+ @fruit.drop(@game.food)
65
+ end
66
+
67
+ # The courtyard for a field so wide, and the snake and the fruit in it.
68
+ def lay(columns)
69
+ @courtyard = Courtyard.new(@rng, hud: false, columns:)
70
+ @snake = SnakeView.new(@rng, skin: @skin, origin: @courtyard.corner(0, 0))
71
+ @fruit = Fruit.new(@courtyard, @particles, Speaker::SILENT)
72
+ end
73
+
74
+ def play(event)
75
+ case event
76
+ in Game::Ate(kind: :fruit) then @game.over? ? @fruit.eaten : @fruit.drop(@game.food)
77
+ in Game::Ate then @rare.eaten
78
+ in Game::Crashed | Game::Moved then nil
79
+ end
80
+ @snake.swallow if event.is_a?(Game::Ate)
81
+ @death = Death.new(@courtyard, @game.snake.body, @particles, @skin, Speaker::SILENT) if @game.over?
82
+ @rare.follow(@game.rare, @game.rare_left.fdiv(Game::RARE_MOVES))
83
+ end
84
+ end
85
+ end