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,326 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A game in progress: a countdown, then the snake on its own clock, which
5
+ # quickens as it eats, and at the end the death sequence before the game
6
+ # over panel. Between ticks the snake is drawn part of the way to where it
7
+ # is going. The speed, the snake's skin and whether a crash shakes the
8
+ # screen come from the player's preferences.
9
+ #
10
+ # Each fruit rings a step higher up the scale than the one before, until
11
+ # the multiplier rises with a run of bells and the steps begin again.
12
+ # Each rare fruit bursts in its own colors with its own sound: the plum
13
+ # sheds the end of the tail in puffs of scales, and while a pepper or a
14
+ # pear works, embers or silver drift up behind the snake and the music
15
+ # runs at its new pace, until it wears off with a sound of its own.
16
+ #
17
+ # The game goes into the stats the moment it ends, or when the player
18
+ # gives it up from the pause panel, if the snake had moved by then.
19
+ #
20
+ # Medals are earned as the game goes, kept at once, and let down into
21
+ # the band one after another.
22
+ #
23
+ # A game from the high scores can be watched again here too, steered by
24
+ # its recording just as it was played, with its name and day in the
25
+ # band. Right plays it faster, up to four times, and left slower again;
26
+ # Space, Enter, P or a click holds it still and lets it go, the music
27
+ # behind the door meanwhile; Esc, or the end of the game, goes back to
28
+ # the table. Nothing that happens in it counts for the stats.
29
+ class Play
30
+ FASTEST_WATCHING = 4
31
+ JUICE = %i[cream gold amber orange red crimson wine].freeze
32
+ # Each rare fruit's burst, and the color of the points it rises with.
33
+ BURSTS = {
34
+ golden: %i[white cream gold amber orange], plum: %i[white blush orchid mauve plum],
35
+ pepper: %i[white gold amber orange red], pear: %i[white cream sand ash steel]
36
+ }.freeze
37
+ POINTS = { golden: :cream, plum: :blush, pepper: :amber, pear: :white }.freeze
38
+ EMBERS = %i[white gold amber orange red].freeze
39
+ SILVER = %i[white white cream sand ash].freeze
40
+
41
+ attr_reader :game
42
+
43
+ # A new game at the player's speed, with dice of its own so it can be
44
+ # recorded; or a game given to be played; or a game from the high
45
+ # scores to be watched again.
46
+ def initialize(stage, game: nil, watching: nil)
47
+ @stage = stage
48
+ @preferences = stage.preferences
49
+ @speaker = stage.speaker
50
+ @watching = watching
51
+ given = watching&.replay&.game || game
52
+ @courtyard = Courtyard.new(stage.rng, columns: given&.board&.columns || stage.columns)
53
+ @seed = stage.rng.rand(1 << 30) unless given
54
+ @game = given || Game.new(Board.new(@courtyard.columns, Courtyard::ROWS), Random.new(@seed), speed: @preferences.speed)
55
+ @pilot = watching && Playback.new(@game, watching.replay.turns)
56
+ @feats = Feats.new(@game) unless watching
57
+ @awards = []
58
+ @rate = 1
59
+ @held = false
60
+ @metronome = Metronome.new(@game)
61
+ @particles = Particles.new(stage.rng)
62
+ @snake = SnakeView.new(stage.rng, skin: @preferences.skin, origin: @courtyard.corner(0, 0))
63
+ @fruit = Fruit.new(@courtyard, @particles, @speaker)
64
+ @fruit.drop(@game.food)
65
+ @rare = RareFruit.new(@courtyard, @particles, @speaker)
66
+ @shed = Shed.new(@courtyard, @particles, @preferences.skin)
67
+ @rare.follow(@game.rare, 1.0)
68
+ @effect = nil
69
+ @hud = Hud.new(stage.scores.best(@game.speed))
70
+ @hud.caption = "#{watching.name.empty? ? "-" : watching.name} #{Day.told(watching.date)}" if watching
71
+ @fireflies = Fireflies.new(stage.rng, 4)
72
+ @floating = []
73
+ @chase = chase
74
+ @waiting = Music::NONE
75
+ count_down
76
+ end
77
+
78
+ # The snake's head, where transitions close and open.
79
+ def focus
80
+ x, y = @courtyard.corner(@game.snake.head.column, @game.snake.head.row)
81
+ [x + Courtyard::TILE / 2, y + Courtyard::TILE / 2]
82
+ end
83
+
84
+ def typing? = false
85
+
86
+ # Nothing until GO!, then the chase, quickening with the snake and
87
+ # fuller as the multiplier climbs. Back from the pause panel it is
88
+ # still behind the door until GO! opens it again. A crash ends it.
89
+ def music
90
+ case @phase
91
+ when :running then @held ? @chase.muffle : @chase
92
+ when :countdown then @countdown.go? ? @chase : @waiting
93
+ else Music::NONE
94
+ end
95
+ end
96
+
97
+ # Coming back from the pause panel: count down again so the player can
98
+ # find the snake before it moves.
99
+ def resume
100
+ @waiting = @chase.muffle
101
+ count_down
102
+ @stage.cut_to(self)
103
+ end
104
+
105
+ def abandon
106
+ @stage.stats.record(@game.run) if @game.moves.positive?
107
+ end
108
+
109
+ # The game as it has been played, to be played again, if it was played
110
+ # here with dice of its own.
111
+ def recording = @seed && Recording.of(@game, @seed)
112
+
113
+ def update(dt)
114
+ [@courtyard, @snake, @fruit, @rare, @shed, @hud, @fireflies, @particles].each { |part| part.update(dt) }
115
+ @floating.each { |afloat| afloat.update(dt) }.reject!(&:done?)
116
+ @awards.first&.update(dt)
117
+ @awards.shift if @awards.first&.done?
118
+ case @phase
119
+ when :countdown then advance_countdown(dt)
120
+ when :running then advance_game(dt)
121
+ when :dying then advance_death(dt)
122
+ end
123
+ end
124
+
125
+ def draw(canvas)
126
+ @courtyard.draw(canvas)
127
+ @hud.draw(canvas)
128
+ @awards.first&.draw(canvas)
129
+ @fruit.draw(canvas)
130
+ @rare.draw(canvas)
131
+ @snake.draw(canvas, @game.snake, @metronome.progress, @game.food,
132
+ popped: @death&.popped || 0, stunned: @death&.stunned? || false)
133
+ @death&.draw(canvas)
134
+ @particles.draw(canvas)
135
+ @floating.each { |afloat| afloat.draw(canvas) }
136
+ @fireflies.draw(canvas)
137
+ @countdown.draw(canvas, *countdown_spot) if @phase == :countdown
138
+ shake_and_flash(canvas) if @death
139
+ end
140
+
141
+ def press(action)
142
+ return watch(action) if @watching
143
+ return unless %i[countdown running].include?(@phase)
144
+
145
+ case action
146
+ when :up, :down, :left, :right then @game.steer(Direction.named(action))
147
+ when :pause, :back, :blur then pause
148
+ end
149
+ end
150
+
151
+ # Watching, a click holds the game or lets it go.
152
+ def point(_x, _y, kind)
153
+ hold if @watching && kind == :click
154
+ end
155
+
156
+ private
157
+
158
+ def pause
159
+ @speaker.play(:pause)
160
+ @stage.cut_to(Pause.new(@stage, self))
161
+ end
162
+
163
+ def watch(action)
164
+ case action
165
+ when :right, :left then faster(action == :right ? 2 : 0.5)
166
+ when :confirm, :pause then hold
167
+ when :blur then hold unless @held
168
+ when :back then stop_watching
169
+ end
170
+ end
171
+
172
+ def hold
173
+ return unless @phase == :running
174
+
175
+ @held = !@held
176
+ @hud.held = @held
177
+ @speaker.play(@held ? :pause : :press)
178
+ end
179
+
180
+ def faster(by)
181
+ rate = (@rate * by).round.clamp(1, FASTEST_WATCHING)
182
+ return if rate == @rate
183
+
184
+ @rate = rate
185
+ @hud.rate = rate
186
+ @chase = chase
187
+ @speaker.play(:tick, by > 1 ? 2 : 0)
188
+ end
189
+
190
+ def stop_watching
191
+ return if @phase == :over
192
+
193
+ @phase = :over
194
+ @speaker.play(:back)
195
+ @stage.show(Scores.new(@stage, chosen: @watching), Iris)
196
+ end
197
+
198
+ def count_down
199
+ @phase = :countdown
200
+ @countdown = Countdown.new(@speaker)
201
+ end
202
+
203
+ # The chase at the snake's pace now, against the slowest start, and as
204
+ # much faster as the game is being watched.
205
+ def chase = @preferences.song.in_a_game(pace: Game::SLOWEST / @game.interval * @rate, level: @game.level)
206
+
207
+ def advance_countdown(dt)
208
+ @countdown.update(dt)
209
+ @phase = :running if @countdown.done?
210
+ end
211
+
212
+ def advance_game(dt)
213
+ return if @held
214
+
215
+ @metronome.advance(dt * @rate) do
216
+ @pilot&.steer
217
+ react(@game.tick)
218
+ end
219
+ end
220
+
221
+ # A game being watched goes back to the table once it has come apart.
222
+ def advance_death(dt)
223
+ @death.update(dt)
224
+ return unless @death.done?
225
+ return stop_watching if @watching
226
+
227
+ @phase = :over
228
+ @stage.cut_to(GameOver.new(@stage, self, score: @game.score, length: @game.snake.length, won: @game.won?))
229
+ end
230
+
231
+ def react(event)
232
+ case event
233
+ in Game::Ate(kind: :fruit, tile:, points:) then eat(tile, points)
234
+ in Game::Ate then savour(event)
235
+ in Game::Crashed then crash
236
+ in Game::Moved then nil
237
+ end
238
+ @rare.follow(@game.rare, @game.rare_left.fdiv(Game::RARE_MOVES))
239
+ work
240
+ @feats&.after(event)&.each { |medal| award(medal) }
241
+ end
242
+
243
+ # A medal the player has not got yet is kept and shown.
244
+ def award(medal)
245
+ @awards << Award.new(medal, @particles, @speaker) if @stage.stats.award(medal)
246
+ end
247
+
248
+ def eat(tile, points)
249
+ x, y = @courtyard.corner(tile.column, tile.row)
250
+ @particles.burst(x + 4, y + 4, JUICE, count: 12, speed: 58, sparkle: true)
251
+ @floating << FloatingText.new("+#{points}", x + 4, y - 3, :gold) << Leaf.new(x + 6, y + 1)
252
+ @speaker.play(:bite, (@game.eaten - 1) % Game::FRUIT_PER_LEVEL, at: x + 4)
253
+ @speaker.play(:level, at: x + 4) if @game.level > Game.level_after(@game.eaten - 1)
254
+ @snake.swallow
255
+ @hud.score = @game.score
256
+ @hud.level = @game.level
257
+ return @fruit.drop(@game.food) unless @game.over?
258
+
259
+ @fruit.eaten
260
+ die
261
+ end
262
+
263
+ # A rare fruit goes up in a burst of its own colors, the golden one's
264
+ # the biggest, worth a handful of fruit.
265
+ def savour(event)
266
+ x, y = @courtyard.corner(event.tile.column, event.tile.row)
267
+ golden = event.kind == :golden
268
+ @rare.eaten
269
+ @particles.burst(x + 4, y + 4, BURSTS.fetch(event.kind), count: golden ? 24 : 16, speed: golden ? 75 : 62, sparkle: true)
270
+ @floating << FloatingText.new("+#{event.points}", x + 4, y - 3, POINTS.fetch(event.kind))
271
+ @speaker.play(golden ? :feast : event.kind, at: x + 4)
272
+ @shed.add(event.shed)
273
+ @snake.swallow
274
+ @hud.score = @game.score
275
+ end
276
+
277
+ # Whatever a pepper or a pear is doing after each move: a trail behind
278
+ # the snake while it works, its badge in the band, the music at the
279
+ # snake's pace, and a sound when it wears off.
280
+ def work
281
+ effect = @game.effect
282
+ @speaker.play(:cool, Game::WORKS.keys.index(@effect), at: focus.first) if @effect && effect != @effect
283
+ @effect = effect
284
+ trail(effect) if effect && !@game.over?
285
+ @hud.effect(effect, @game.effect_share)
286
+ @chase = chase
287
+ end
288
+
289
+ # Embers fly up from the pepper's heat behind the head, and silver
290
+ # drifts up slowly from the pear's.
291
+ def trail(effect)
292
+ x, y = @courtyard.corner(@game.snake.body[1].column, @game.snake.body[1].row)
293
+ if effect == :pepper
294
+ @particles.burst(x + 4, y + 3, EMBERS, count: 3, speed: 24, gravity: -90.0, life: 0.25..0.5)
295
+ else
296
+ @particles.burst(x + 4, y + 3, SILVER, count: 2, speed: 9, gravity: -14.0, life: 0.7..1.1)
297
+ end
298
+ end
299
+
300
+ def crash
301
+ @speaker.play(:crash, at: focus.first)
302
+ die
303
+ end
304
+
305
+ def die
306
+ @phase = :dying
307
+ @death = Death.new(@courtyard, @game.snake.body, @particles, @preferences.skin, @speaker)
308
+ @stage.stats.record(@game.run) unless @watching
309
+ end
310
+
311
+ # Just above the head, or just below it when the head is near the top.
312
+ def countdown_spot
313
+ x, y = focus
314
+ [x.clamp(@courtyard.left + 20, @courtyard.right - 20), y < Courtyard::TOP + 30 ? y + 22 : y - 7]
315
+ end
316
+
317
+ # The flash always comes; the shake only if the player wants it.
318
+ def shake_and_flash(canvas)
319
+ canvas.recolor(Palette::LIGHTER[@death.flash]) if @death.flash.positive?
320
+ return unless @preferences.shake?
321
+
322
+ dx, dy = @death.shake
323
+ canvas.shift(dx, dy) unless dx.zero? && dy.zero?
324
+ end
325
+ end
326
+ end