Part 7 mini-project: Coin Collector

The first Part 7 project puts everything from Chapters 31-35 together: a pygame window, keyboard input, moving a player, and detecting when two objects overlap. A single coin sits somewhere on screen; collect it, a new one spawns, and your score goes up.

What to build

When the program runs:

  1. A window opens showing a blue square (the player) and a yellow circle (the coin).
  2. Arrow keys move the player around the screen.
  3. When the player overlaps the coin, the score increases by 1 and a new coin appears at a random position.
  4. The current score is displayed in the top-left corner at all times.
  5. Closing the window ends the game.

What the screen looks like

The background is dark grey. The player is a solid blue square (40x40 pixels). The coin is a solid yellow filled circle (radius 15). The score reads Score: 3 in white text in the top-left corner. There are no other decorations.

Files

projects/06-coin-collector/
    coin_collector.py

Everything fits in one file. There is no starter skeleton for this project — write it from scratch using the hints below.

Run with:

python projects/06-coin-collector/coin_collector.py

How to approach it

Work in this order:

  1. Open the window, fill the background, run the event loop — nothing moves yet.
  2. Draw the player square and move it with arrow keys.
  3. Draw a coin at a fixed position.
  4. Detect collection and increment the score.
  5. Spawn the new coin at a random position.
  6. Draw the score text.

Each step is testable on its own before moving to the next.

Hints

  • Use pygame.Rect for the player. Updating rect.x and rect.y each frame is enough to move it. Clamp with rect.clamp_ip(screen.get_rect()) to stop the player leaving the window.
  • Store the coin position as a plain (x, y) tuple. Redraw it each frame with pygame.draw.circle.
  • To check collection, build a small rect around the coin and test player_rect.colliderect(coin_rect), or use player_rect.collidepoint(coin_x, coin_y).
  • Spawn a new coin with random.randint(padding, screen_width - padding) for both x and y, where padding keeps the coin away from the edge (use the coin radius as the padding).
  • Render score text with font.render(f"Score: {score}", True, WHITE) and screen.blit it at (10, 10).

pygame.key.get_pressed() returns the state of every key each frame. Check keys[pygame.K_LEFT], keys[pygame.K_RIGHT], keys[pygame.K_UP], keys[pygame.K_DOWN] to move in each direction.

What you cannot use yet

Chapters 36 and beyond cover more advanced class design. For this project keep everything at the top level or in simple functions — no custom classes required.

Optional stretch goals

  • Speed up the player by one pixel per coin collected, so the game gets harder over time.
  • High score. Track the best score across rounds; display it alongside the current score.
  • Shrinking coin. After a coin spawns, reduce its radius by 1 each second until it disappears (missed coin, no point).

None of these are required to finish the project.

Done?

When the player can move, collect the coin, and see the score increase each time — Part 7 project one is complete. Move on to the second Part 7 project: Collect-All-Coins.