Collect Coins in Pygame

In this Collect Coins in Pygame tutorial, you will learn how to make your player collect coins.

In the previous lesson, we detected a collision between the player and the enemy. Now we will use the same idea to collect coins. When the player touches the coin:

  • The coin will be collected.
  • The coin will appear in a new position.
  • In the next lesson, we will count how many coins the player has collected.

By the end of this lesson, your game will feel much more interactive.


What Will You Learn?

In this lesson, you will learn:

  • How to add a coin image
  • How to detect when the player collects a coin
  • How to move the coin to a new position
  • How to prepare your game for a score system

Project Folder

Your project should look like this:

Lesson12
│
├── lesson12_collect_coins.py
│
└── images
      ├── background.png
      ├── player.png
      ├── enemy.png
      └── coin.png

Step 1: Import the Random Module

At the top of your program, add:

import random
import pygame

The random module helps us place the coin at different locations.


Step 2: Load the Coin Image

After loading the enemy image, add:

coin = pygame.image.load("images/coin.png")

Now your game knows about the coin image.


Step 3: Set the Coin Position

Add these variables:

coin_x = 200
coin_y = 200

These values decide where the coin appears.


Step 4: Draw the Coin

Inside the game loop, add:

screen.blit(coin, (coin_x, coin_y))

Run the program.

You should now see a coin on the screen.


Step 5: Create a Coin Rectangle

Inside the game loop, create a rectangle for the coin.

coin_rect = coin.get_rect(topleft=(coin_x, coin_y))

Step 6: Check if the Player Collects the Coin

Use the player’s rectangle from the previous lesson.

Now add:

if player_rect.colliderect(coin_rect):

    coin_x = random.randint(0, 736)
    coin_y = random.randint(0, 536)

    print("Coin Collected!")

Run the program.

Move the player over the coin.

The coin jumps to a new random position.

Great job! Your player has collected the coin.


Understanding the Random Position

Look at this line:

coin_x = random.randint(0, 736)

It means:

Choose a random number between 0 and 736.

Every time the coin is collected, it appears somewhere new.

The same happens for the Y position.

coin_y = random.randint(0, 536)

This keeps the game fun because the coin never appears in the same place every time.


Complete Program

import pygame
import random

# Initialize Pygame
pygame.init()

# Create Game Window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Collect Coins in Pygame")

# Load Images
background = pygame.image.load("images/background.png")
player = pygame.image.load("images/player.png")
enemy = pygame.image.load("images/enemy.png")
coin = pygame.image.load("images/coin.png")

# Player Position
player_x = 350
player_y = 250
player_speed = 5

# Enemy Position
enemy_x = 0
enemy_y = 100
enemy_speed = 3

# Coin Position
coin_x = random.randint(0, 736)
coin_y = random.randint(0, 536)

running = True

while running:

    # Draw Background
    screen.blit(background, (0, 0))

    # Keyboard Input
    keys = pygame.key.get_pressed()

    # Move Player
    if keys[pygame.K_RIGHT] and player_x < 736:
        player_x += player_speed

    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed

    if keys[pygame.K_UP] and player_y > 0:
        player_y -= player_speed

    if keys[pygame.K_DOWN] and player_y < 536:
        player_y += player_speed

    # Move Enemy
    enemy_x += enemy_speed

    if enemy_x > 800:
        enemy_x = -64

    # Create Rectangles
    player_rect = player.get_rect(topleft=(player_x, player_y))
    enemy_rect = enemy.get_rect(topleft=(enemy_x, enemy_y))
    coin_rect = coin.get_rect(topleft=(coin_x, coin_y))

    # Enemy Collision
    if player_rect.colliderect(enemy_rect):
        pygame.display.set_caption("Oops! Enemy Hit!")

    # Coin Collision
    if player_rect.colliderect(coin_rect):

        print("Coin Collected!")

        coin_x = random.randint(0, 736)
        coin_y = random.randint(0, 536)

    # Draw Player
    screen.blit(player, (player_x, player_y))

    # Draw Enemy
    screen.blit(enemy, (enemy_x, enemy_y))

    # Draw Coin
    screen.blit(coin, (coin_x, coin_y))

    # Update Screen
    pygame.display.update()

    # Close Window
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

pygame.quit()

Try Different Coin Positions

Experiment with these values.

Example 1

coin_x = 100
coin_y = 100

Example 2

coin_x = 600
coin_y = 400

Example 3

coin_x = random.randint(50, 700)
coin_y = random.randint(50, 500)

Observe how the coin appears in different places.


Mini Project

Create a game where:

  • The player moves using the arrow keys.
  • The coin appears on the screen.
  • When the player touches the coin, it moves to a new random position.

Now your game has a real objective: collect the coin!


Practice Challenge

Challenge 1

Change the coin image to a gem or a star.


Challenge 2

Make the coin appear only in the middle area of the screen.

Hint:

random.randint(150, 650)

Challenge 3

Add two coins.

Create:

coin2_x
coin2_y

Can you collect both coins?


Common Mistakes

The coin is not visible

Check:

  • The image file is named coin.png.
  • It is inside the images folder.
  • The file path is correct.

The coin never moves

Make sure the collision check is inside the game loop.

Also verify that you imported the random module.


The coin appears outside the screen

Use values that match your game window size.

For an 800 × 600 window:

random.randint(0, 736)
random.randint(0, 536)

Best Practice

Store the positions of every game object in separate variables.

Example:

player_x
player_y

enemy_x
enemy_y

coin_x
coin_y

This makes your code easier to understand and maintain.


What You Learned Today

Today you learned:

  • How to collect coins in Pygame
  • How to load a coin image
  • How to detect coin collection
  • How to place a coin at a random position
  • How to prepare for a scoring system

Quick Revision

  1. Which module is used to generate random positions?
  2. Which function loads the coin image?
  3. What does random.randint() do?
  4. Why do we create a rectangle for the coin?

What’s Next?

In Lesson 13: Display Score in Pygame, you will learn how to count the number of collected coins and display the score on the screen.

Your game will finally reward the player for collecting coins.


Frequently Asked Questions (SEO & AEO Optimized)

How do I collect coins in Pygame?

Detect a collision between the player and the coin using colliderect(). When they touch, move the coin to a new position and update the score.

How do I move a coin to a random position in Pygame?

Use Python’s random.randint() function to generate new X and Y coordinates.

Why do we use the random module in Pygame?

The random module helps place game objects, such as coins and enemies, in different positions, making the game more interesting.

Can I add multiple coins in Pygame?

Yes. You can create multiple coin variables or use a list to manage several coins in the game.

Why does my coin appear outside the screen?

The random values may be larger than the visible game area. Limit the X and Y values based on the window size and the coin’s dimensions.

How do games know when a coin is collected?

Games create invisible rectangles around the player and the coin. When these rectangles overlap, the game detects that the coin has been collected.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top