Complete the game-over and high-score system. Fill each blank.
import json, os
SAVE_FILE = "snake_scores.json"
def load_scores():
if os.path.exists(▢1):
with open(SAVE_FILE) as f:
return json.▢2(f)
return {"high": ▢3}
def save_scores(data):
with open(SAVE_FILE, ▢4) as f:
json.dump(data, ▢5)
# In the game update:
scores = load_scores()
if score > scores[▢6]:
scores["high"] = score
save_scores(▢7)
# Game-over screen function:
def show_game_over(screen, score, high):
overlay = pygame.Surface(screen.get_size(), pygame.▢8)
overlay.fill((0, 0, 0, ▢9))
screen.blit(overlay, (0, 0))
# render text centred on screen
pygame.display.▢10()
S________l____"___"_____s_____S_______1__f___The game-over / high-score code below has 3 bugs. Type the corrected line for each.
import json, os
def load_scores():
if os.path.exists("snake_scores.json"):
with open("snake_scores.json") as f:
return json.loads(f) # Bug 1 – wrong method
def save_scores(data):
with open("snake_scores.json") as f: # Bug 2 – wrong file mode
json.dump(data, f)
scores = load_scores()
if score > scores["high"]:
scores["high"] = score
save_scores(scores)
score = 0 # Bug 3 – resets score before showing it
Bug 1
Buggy line:
Your fix:
Hint: `json.loads()` parses a string. `json.load()` reads directly from a file object.
Bug 2
Buggy line:
Your fix:
Hint: Writing requires "w" mode. Without it, `open()` defaults to read-only and `json.dump()` will raise an error.
Bug 3
Buggy line:
Your fix:
Hint: Resetting score here wipes the value before it can be displayed on the game-over screen. Reset at game restart instead.
Build a **game-over overlay**: a semi-transparent black surface drawn over the screen, showing "GAME OVER", the final score, and the high score. Pressing SPACE or ENTER restarts.
def show_game_over(screen, score, high_score):
"""Draw a game-over overlay and wait for SPACE/ENTER to restart."""
overlay = pygame.Surface(screen.get_size(), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 180))
screen.blit(overlay, (0, 0))
# YOUR CODE: render "GAME OVER", score, high_score centred on screen
# YOUR CODE: call pygame.display.flip() then wait for SPACE/ENTER key
pass
**Challenge:** Package the complete game — Snake class, Game class, game-over screen, persistent high score — into a single clean `snake.py` file. Write comments explaining each major section. Paste the full file or a detailed outline with key code snippets.
Answer all 5 questions, then the Reflective Journal will unlock.
1. Replacing the literal 30 with CELL_SIZE = 30 throughout the code is better because:
2. What does a # TODO: add sound comment indicate?
3. Moving snake-drawing code into a draw_snake() function is an example of:
4. A helper function grid_to_pixel(col, row) is useful because:
5. Which of these is NOT a benefit of using named constants?
Answer each prompt thoughtfully. There are no right or wrong answers — this is your own reflection.