tinyarcade 0.1.0__py3-none-any.whl

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.
tinyarcade/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """TinyArcade: a small collection of terminal mini-games."""
2
+
3
+ __version__ = "0.1.0"
@@ -0,0 +1,495 @@
1
+ import os
2
+ import random
3
+ import time
4
+
5
+ # =====================================
6
+ # COLOUR MEMORY CHALLENGE
7
+ # =====================================
8
+
9
+ GAME_NAME = "COLOUR MEMORY CHALLENGE"
10
+ MAX_STAGE = 20
11
+ HEADER_WIDTH = 50
12
+
13
+ # ANSI colours
14
+ COLOURS = {
15
+ "r": ("\033[91m", "Red"),
16
+ "b": ("\033[94m", "Blue"),
17
+ "g": ("\033[92m", "Green"),
18
+ "y": ("\033[93m", "Yellow"),
19
+ "w": ("\033[97m", "White"),
20
+ "p": ("\033[95m", "Purple"),
21
+ }
22
+
23
+ RESET = "\033[0m"
24
+
25
+ NEW_COLOURS = {
26
+ 6: ("y", "Yellow"),
27
+ 11: ("w", "White"),
28
+ 16: ("p", "Purple"),
29
+ }
30
+
31
+
32
+ # =====================================
33
+ # UTILITIES
34
+ # =====================================
35
+
36
+ def clear_screen():
37
+ os.system("cls" if os.name == "nt" else "clear")
38
+
39
+
40
+ def colour_text(letter):
41
+ return f"{COLOURS[letter][0]}{letter.upper()}{RESET}"
42
+
43
+
44
+ def colour_letter(letter):
45
+ return f"{COLOURS[letter][0]}{letter.upper()}{RESET}"
46
+
47
+
48
+ def colour_name_text(letter):
49
+ return f"{COLOURS[letter][0]}{COLOURS[letter][1]}{RESET}"
50
+
51
+
52
+ def colour_sequence_text(sequence):
53
+ if not sequence:
54
+ return ""
55
+
56
+ coloured = []
57
+
58
+ for letter in sequence:
59
+ if letter.lower() in COLOURS:
60
+ coloured.append(colour_letter(letter.lower()))
61
+ else:
62
+ coloured.append(letter)
63
+
64
+ return " ".join(coloured)
65
+
66
+
67
+ def print_header(stage=None):
68
+
69
+ if stage is None:
70
+ print(GAME_NAME)
71
+ print("=" * HEADER_WIDTH)
72
+ return
73
+
74
+ stage_text = f"STAGE {stage}"
75
+
76
+ spaces = max(
77
+ 1,
78
+ HEADER_WIDTH - len(GAME_NAME) - len(stage_text)
79
+ )
80
+
81
+ print(GAME_NAME + (" " * spaces) + stage_text)
82
+ print("=" * HEADER_WIDTH)
83
+
84
+
85
+ # =====================================
86
+ # GAME DATA
87
+ # =====================================
88
+
89
+ def available_colours(stage):
90
+
91
+ colours = ["r", "b", "g"]
92
+
93
+ if stage >= 6:
94
+ colours.append("y")
95
+
96
+ if stage >= 11:
97
+ colours.append("w")
98
+
99
+ if stage >= 16:
100
+ colours.append("p")
101
+
102
+ return colours
103
+
104
+
105
+ def choose_next_colour(sequence, available):
106
+
107
+ weights = []
108
+
109
+ for colour in available:
110
+
111
+ weight = 100
112
+
113
+ # Slightly discourage repeats
114
+ if sequence and colour == sequence[-1]:
115
+ weight = 50
116
+
117
+ # Triple repeats are allowed but uncommon
118
+ if len(sequence) >= 2:
119
+ if colour == sequence[-1] == sequence[-2]:
120
+ weight = 20
121
+
122
+ # Never allow four identical colours in a row
123
+ if len(sequence) >= 3:
124
+ if (
125
+ colour ==
126
+ sequence[-1] ==
127
+ sequence[-2] ==
128
+ sequence[-3]
129
+ ):
130
+ weight = 0
131
+
132
+ # Reduce chance if colour appeared frequently recently
133
+ recent = sequence[-5:]
134
+ weight -= recent.count(colour) * 10
135
+
136
+ weights.append(max(0, weight))
137
+
138
+ return random.choices(
139
+ available,
140
+ weights=weights,
141
+ k=1
142
+ )[0]
143
+
144
+
145
+ def build_master_sequence():
146
+
147
+ sequence = []
148
+
149
+ for stage in range(1, MAX_STAGE + 1):
150
+
151
+ available = available_colours(stage)
152
+
153
+ next_colour = choose_next_colour(
154
+ sequence,
155
+ available
156
+ )
157
+
158
+ sequence.append(next_colour)
159
+
160
+ return sequence
161
+
162
+
163
+ # =====================================
164
+ # INSTRUCTIONS
165
+ # =====================================
166
+
167
+ def show_instructions():
168
+
169
+ clear_screen()
170
+
171
+ print_header()
172
+
173
+ print("\nOBJECTIVE")
174
+ print("Memorise the sequence and enter it back")
175
+ print("using letter keys only.")
176
+
177
+ print("\nCOLOUR KEYS")
178
+
179
+ print(f"{colour_text('r')} = Red")
180
+ print(f"{colour_text('b')} = Blue")
181
+ print(f"{colour_text('g')} = Green")
182
+
183
+ print("\nMORE COLOURS WILL UNLOCK")
184
+
185
+ print(f"Stage 6 → {colour_text('y')} Yellow")
186
+ print(f"Stage 11 → {colour_text('w')} White")
187
+ print(f"Stage 16 → {colour_text('p')} Purple")
188
+
189
+ print("\nRULES")
190
+
191
+ print("• Sequence grows every stage.")
192
+ print("• Letters only.")
193
+ print("• Spaces are ignored.")
194
+ print("• Case-insensitive.")
195
+ print("• Q quits to Game Selection.")
196
+
197
+ print("\nExample")
198
+ print(
199
+ f"{colour_text('r')} → "
200
+ f"{colour_text('b')} → "
201
+ f"{colour_text('g')}"
202
+ )
203
+ print("Answer: RBG")
204
+
205
+ choice = input(
206
+ "\nPress ENTER to start or Q to quit: "
207
+ ).strip()
208
+
209
+ if choice.upper() == "Q":
210
+ return False
211
+
212
+ return True
213
+
214
+
215
+ # =====================================
216
+ # COLOUR KEYS
217
+ # =====================================
218
+
219
+ def show_colour_keys(stage):
220
+
221
+ print("\nCOLOUR KEYS")
222
+
223
+ print(f"{colour_text('r')} = Red")
224
+ print(f"{colour_text('b')} = Blue")
225
+ print(f"{colour_text('g')} = Green")
226
+
227
+ if stage >= 6:
228
+ print(f"{colour_text('y')} = Yellow")
229
+
230
+ if stage >= 11:
231
+ print(f"{colour_text('w')} = White")
232
+
233
+ if stage >= 16:
234
+ print(f"{colour_text('p')} = Purple")
235
+
236
+
237
+ # =====================================
238
+ # VISUALS
239
+ # =====================================
240
+
241
+ def show_dot(letter):
242
+
243
+ colour_code = COLOURS[letter][0]
244
+ colour_name = COLOURS[letter][1].upper()
245
+
246
+ print("\n")
247
+
248
+ for _ in range(8):
249
+ print(
250
+ " " * 15 +
251
+ colour_code +
252
+ "ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ" +
253
+ RESET
254
+ )
255
+
256
+ print(
257
+ "\n" +
258
+ " " * 20 +
259
+ colour_code +
260
+ colour_name +
261
+ RESET +
262
+ "\n"
263
+ )
264
+
265
+
266
+ def countdown(stage):
267
+
268
+ for i in range(3, 0, -1):
269
+
270
+ clear_screen()
271
+
272
+ print_header(stage)
273
+
274
+ print(f"\nStarting in {i}...")
275
+
276
+ time.sleep(1)
277
+
278
+
279
+ def show_new_colour_message(stage):
280
+
281
+ if stage not in NEW_COLOURS:
282
+ return
283
+
284
+ letter, name = NEW_COLOURS[stage]
285
+
286
+ clear_screen()
287
+
288
+ print_header(stage)
289
+
290
+ print("\nā˜… NEW COLOUR UNLOCKED ā˜…")
291
+
292
+ print(
293
+ f"\n{colour_text(letter)} "
294
+ f"{name.upper()}"
295
+ )
296
+
297
+ print("\nThis colour can now appear in sequences.")
298
+
299
+ input("\nPress ENTER to continue...")
300
+
301
+
302
+ def show_sequence(sequence, stage):
303
+
304
+ for colour in sequence:
305
+
306
+ clear_screen()
307
+
308
+ print_header(stage)
309
+
310
+ show_dot(colour)
311
+
312
+ time.sleep(1.5)
313
+
314
+ clear_screen()
315
+
316
+ print_header(stage)
317
+
318
+ time.sleep(0.35)
319
+
320
+
321
+ # =====================================
322
+ # INPUT
323
+ # =====================================
324
+
325
+ def get_player_input(stage):
326
+
327
+ clear_screen()
328
+
329
+ print_header(stage)
330
+
331
+ show_colour_keys(stage)
332
+
333
+ print("\nEnter the sequence.")
334
+ print("Q = Return to Game Selection")
335
+
336
+ answer = input("\nAnswer: ").strip()
337
+
338
+ if answer.upper() == "Q":
339
+ return "QUIT"
340
+
341
+ return answer.replace(" ", "").lower()
342
+
343
+
344
+ # =====================================
345
+ # END MENU
346
+ # =====================================
347
+
348
+ def show_end_menu(result):
349
+
350
+ while True:
351
+
352
+ clear_screen()
353
+
354
+ print_header()
355
+
356
+ if result["status"] == "WIN":
357
+
358
+ print("\nšŸ† CONGRATULATIONS!")
359
+ print("You completed all 20 stages!")
360
+
361
+ elif result["status"] == "LOSE":
362
+
363
+ print("\nāŒ GAME OVER")
364
+ print(
365
+ f"You reached Stage "
366
+ f"{result['stage']}."
367
+ )
368
+
369
+ print("\nYour answer:")
370
+ print(
371
+ colour_sequence_text(
372
+ result["player_answer"]
373
+ )
374
+ )
375
+
376
+ print("\nCorrect answer:")
377
+ print(
378
+ colour_sequence_text(
379
+ result["correct_answer"]
380
+ )
381
+ )
382
+
383
+ print("\nColour sequence:")
384
+
385
+ coloured_names = []
386
+
387
+ for colour in result["sequence"]:
388
+ coloured_names.append(
389
+ colour_name_text(colour)
390
+ )
391
+
392
+ print(" → ".join(coloured_names))
393
+
394
+ print("\n[1] Play Again")
395
+ print("[2] Return to Menu")
396
+
397
+ choice = input("\nChoose: ").strip()
398
+
399
+ if choice in ("1", "2"):
400
+ return choice
401
+
402
+
403
+ # =====================================
404
+ # GAME LOOP
405
+ # =====================================
406
+
407
+ def play_game():
408
+
409
+ master_sequence = build_master_sequence()
410
+
411
+ for stage in range(1, MAX_STAGE + 1):
412
+
413
+ show_new_colour_message(stage)
414
+
415
+ current_sequence = master_sequence[:stage]
416
+
417
+ countdown(stage)
418
+
419
+ show_sequence(current_sequence, stage)
420
+
421
+ player_answer = get_player_input(stage)
422
+
423
+ if player_answer == "QUIT":
424
+ return {
425
+ "status": "QUIT"
426
+ }
427
+
428
+ correct_answer = "".join(current_sequence)
429
+
430
+ if player_answer != correct_answer:
431
+
432
+ return {
433
+ "status": "LOSE",
434
+ "stage": stage,
435
+ "player_answer": player_answer,
436
+ "correct_answer": correct_answer,
437
+ "sequence": current_sequence
438
+ }
439
+
440
+ clear_screen()
441
+
442
+ print_header(stage)
443
+
444
+ print("\nāœ… Correct!")
445
+
446
+ if stage < MAX_STAGE:
447
+ print(
448
+ f"Preparing Stage "
449
+ f"{stage + 1}..."
450
+ )
451
+
452
+ time.sleep(1.5)
453
+
454
+ return {
455
+ "status": "WIN",
456
+ "stage": MAX_STAGE
457
+ }
458
+
459
+
460
+ # =====================================
461
+ # MAIN GAME
462
+ # =====================================
463
+
464
+ def colour_memory_game():
465
+
466
+ should_start = show_instructions()
467
+
468
+ if not should_start:
469
+ return
470
+
471
+ while True:
472
+
473
+ result = play_game()
474
+
475
+ if result["status"] == "QUIT":
476
+ return
477
+
478
+ choice = show_end_menu(result)
479
+
480
+ if choice == "1":
481
+ continue
482
+
483
+ if choice == "2":
484
+ return
485
+
486
+
487
+ # =====================================
488
+ # START
489
+ # =====================================
490
+
491
+ if __name__ == "__main__":
492
+ colour_memory_game()
493
+
494
+ def play():
495
+ colour_memory_game()