Python-bloggers

Coin Collector: A Fun Pygame for Beginners

This article was first published on python Archives - Hi! I am Nagdev , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

Pygame is a Python module that enables the creation of video games and multimedia applications. It provides a framework for developing 2D games and interactive programs using Python code, and includes features such as graphics rendering, sound playback, and user input handling. Pygame is built on top of the SDL library and is cross-platform, meaning it can be used on a variety of operating systems. It also has an active community and a wealth of documentation and tutorials available online, making it a popular choice for hobbyists and professional game developers alike.

Coin Collector is a simple, yet fun game built using Pygame, a popular Python library for game development. The objective of the game is to collect as many coins as possible while avoiding the bombs. With colorful graphics, engaging sound effects, and easy gameplay, Coin Collector is a great way to pass the time and challenge your hand-eye coordination skills.

The game begins with a black background and the title “Coin Collector” at the top. The player, represented by a blue box, moves horizontally along the bottom of the screen using the left and right arrow keys. Coins, represented by yellow circles, randomly appear at the top of the screen and fall vertically towards the bottom. The player must collect as many coins as possible by touching them with their circle. Each coin collected earns the player one point.

However, the game becomes challenging as bombs, represented by red bars, also randomly appear at the top of the screen and fall towards the bottom. If the player touches a bomb with their square, the game is over. The player’s score is displayed at the end of the game along with the message “Game Over.”

The game is also a great way to introduce beginners to Pygame development. The code is easy to follow, and the game can be customized and modified to create new games with different objectives and mechanics. The use of simple shapes and colors for the game objects makes it easy for beginners to understand the code and make changes.

In addition to being a fun game, Coin Collector also demonstrates some important programming concepts. The game uses loops to continuously update the position of the coins and bombs, collision detection to check if the player has touched a coin or bomb, and event handling to detect when the player has pressed a key.

Coin Collector can also be expanded and improved in many ways. For example, the game could include power-ups that give the player additional points or make them temporarily invincible to bombs.

Overall, Coin Collector is a fun and engaging game that demonstrates important programming concepts and is a great way to introduce beginners to Pygame development.

Here is the code to create this coin collector game using pygame.

import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the window
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Coin Collector")

# Set up the game clock
clock = pygame.time.Clock()

# Set up the game objects
player_width = 50
player_height = 50
player_x = window_width / 2 - player_width / 2
player_y = window_height - player_height - 50
player_speed = 5

coin_radius = 15
coin_x = random.randint(coin_radius, window_width - coin_radius)
coin_y = -coin_radius
coin_speed = 3

obstacle_width = 100
obstacle_height = 20
obstacle_x = random.randint(0, window_width - obstacle_width)
obstacle_y = -obstacle_height
obstacle_speed = 5

score = 0
font = pygame.font.SysFont(None, 30)

game_over = False

# Set up the background
background_color = (0, 0, 0)

# Set up the title
title_font = pygame.font.SysFont(None, 50)
title_text = title_font.render("Coin Collector", True, (255, 255, 255))
title_x = window_width / 2 - title_text.get_width() / 2
title_y = 10

# Game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Move the player
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_x -= player_speed
    elif keys[pygame.K_RIGHT]:
        player_x += player_speed
    
    # Update the game objects
    if not game_over:
        coin_y += coin_speed
        if coin_y > window_height:
            coin_x = random.randint(coin_radius, window_width - coin_radius)
            coin_y = -coin_radius
            score += 1
        
        obstacle_y += obstacle_speed
        if obstacle_y > window_height:
            obstacle_x = random.randint(0, window_width - obstacle_width)
            obstacle_y = -obstacle_height
        
        # Check for collisions
        if player_x < coin_x + coin_radius and player_x + player_width > coin_x - coin_radius and player_y < coin_y + coin_radius and player_y + player_height > coin_y - coin_radius:
            coin_x = random.randint(coin_radius, window_width - coin_radius)
            coin_y = -coin_radius
            score += 1
        
        if player_x < obstacle_x + obstacle_width and player_x + player_width > obstacle_x and player_y < obstacle_y + obstacle_height and player_y + player_height > obstacle_y:
            game_over = True
    
    # Draw the game objects
    window.fill(background_color)
    pygame.draw.rect(window, (255, 0, 0), (obstacle_x, obstacle_y, obstacle_width, obstacle_height))
    pygame.draw.circle(window, (255, 255, 0), (coin_x, coin_y), coin_radius)
    pygame.draw.rect(window, (0, 0, 255), (player_x, player_y, player_width, player_height))
    score_text = font.render("Score: " + str(score), True, (255, 255, 255))
    window.blit(score_text, (10, 10))
    
    if game_over:
        game_over_text = font.render("Game Over!", True, (255, 0, 0))
        window.blit(game_over_text, (window_width / 2 - game_over_text.get_width() / 2, window_height / 2 - game_over_text.get_height() / 2))
    
    window.blit(title_text, (title_x, title_y))
    pygame.display.update()
    
    # Limit the frame rate
    clock.tick(60)

# Clean up
pygame.quit()

More instructions are available on my github

The post Coin Collector: A Fun Pygame for Beginners appeared first on Hi! I am Nagdev.

To leave a comment for the author, please follow the link and comment on their blog: python Archives - Hi! I am Nagdev .

Want to share your content on python-bloggers? click here.
Exit mobile version