Table of Contents

This practical guide explains how to create power-ups and collections in pygame with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.
Fortunately, PyGame provides a flexible way to make power-ups and collections relatively easy to deploy.
Create a Simple Game
Before you start, make sure you have pip installed on your system. Use this command to install the pygame library:
pip install pygame
Start by setting up a simple game where the player can move left and right while avoiding enemies.
This serves as the foundation for adding power-ups and collections. Here is the sample code:
import pygame import random # Kh?i t?o Pygame pygame.init() # Thi?t l?p c?a s? game window_width = 800 window_height = 600 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("My Game") # Thi?t l?p ng??i ch?i player_width = 50 player_height = 50 player_x = (window_width - player_width) // 2 player_y = window_height - player_height - 10 player_speed = 5 # Thi?t l?p k? thù enemy_width = 50 enemy_height = 50 enemy_x = random.randint(0, window_width - enemy_width) enemy_y = 50 enemy_speed = 3 # Game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Di chuy?n c?a ng??i ch?i keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= player_speed if keys[pygame.K_RIGHT] and player_x < window_width - player_width: player_x += player_speed # Chuy?n ??ng c?a k? thù enemy_y += enemy_speed if enemy_y > window_height: enemy_x = random.randint(0, window_width - enemy_width) enemy_y = 0 # Phát hi?n va ch?m if (player_x < enemy_x + enemy_width) and (player_x + player_width > enemy_x) and (player_y < enemy_y + enemy_height) and (player_y + player_height > enemy_y): running = False # Xóa màn hình window.fill((0, 0, 0)) player_pos = (player_x, player_y, player_width, player_height) enemy_pos = (enemy_x, enemy_y, enemy_width, enemy_height) # V? ng??i ch?i pygame.draw.rect(window, (255, 255, 255), player_pos) # V? k? thù pygame.draw.rect(window, (255, 0, 0), enemy_pos) # Update màn hình pygame.display.update() # Thoát game pygame.quit()
Result:

Create a Collection
To create a collection, add an item that, when collided with the player, disappears and the game adds 10 points to the player. For this to work, you must check for collisions between the player and the collection. Here is the udpate version of the code that includes the collection. Create a new file named collectibles.py and add the following code and updates:
# Thi?t l?p b? s?u t?p collectible_width = 30 collectible_height = 30 collectible_x = random.randint(0, window_width - collectible_width) collectible_y = 50 # Thi?t l?p ?i?m s? score = 0 font = pygame.font.Font(None, 36) # . # Phát hi?n va ch?m v?i ?? s?u t?m if (player_x < collectible_x + collectible_width) and (player_x + player_width > collectible_x) and (player_y < collectible_y + collectible_height) and (player_y + player_height > collectible_y): collectible_x = random.randint(0, window_width - collectible_width) collectible_y = 50 score += 10 # . collectible_pos = (collectible_x, collectible_y) # V? ?? s?u t?m pygame.draw.circle(window, (0, 255, 0), collectible_pos, collectible_width) # Hi?n ?i?m s? score_text = font.render("Score: " + str(score), True, (255, 255, 255)) window.blit(score_text, (10, 10))
Result:
Create Power-Ups
You can now introduce power-ups to the game using the following logic. When the player collides with the power-up object, it will disappear. If the player collides with an enemy while activating the power-up, the enemy will be eliminated. Create a new file named powerups.py and add the code and updates below:
# Thi?t l?p power-up powerup_width = 40 powerup_height = 40 powerup_x = random.randint(0, window_width - powerup_width) powerup_y = 50 shield_active = False shield_timer = 0 # . # Phát hi?n va ch?m v?i power-up collision_powerup = (player_x < powerup_x + powerup_width) and (player_x + player_width > powerup_x) and (player_y < powerup_y + powerup_height) and (player_y + player_height > powerup_y) if collision_powerup: powerup_x = random.randint(0, window_width - powerup_width) powerup_y = 50 shield_active = True shield_timer = pygame.time.get_ticks() # . # Ki?m tra h?n gi? lá ch?n if shield_active: current_time = pygame.time.get_ticks() if current_time - shield_timer > 5000: shield_active = False # . # Xác ??nh các ??nh c?a tam giác x1 = powerup_x + powerup_width / 2 y1 = powerup_y x2 = powerup_x y2 = powerup_y + powerup_height x3 = powerup_x + powerup_width y3 = powerup_y + powerup_height # V? hình tam giác pygame.draw.polygon(window, (255, 255, 0), [(x1, y1), (x2, y2), (x3, y3)]) # . # Phát hi?n va ch?m v?i lá ch?n ???c kích ho?t collision_shield = shield_active and (player_x < enemy_x + enemy_width) and (player_x + player_width > enemy_x) and (player_y < enemy_y + enemy_height) and (player_y + player_height > enemy_y) if collision_shield: enemy_x = random.randint(0, window_width - enemy_width) enemy_y = 0
Result:

Set the Power-Up Run Time
To make a power-up disappear after a certain amount of time and reappear at a random location, you can use a timer. Create a new file named timer.py and add the code with the updates below:
# Thi?t l?p ??ng h? b?m gi? xu?t hi?n l?i power-up powerup_respawn_timer = 0 # Ki?m tra ??ng h? b?m gi? power-up if not shield_active: current_time = pygame.time.get_ticks() if current_time - powerup_respawn_timer > 3000: powerup_x = random.randint(0, window_width - powerup_width) powerup_y = 50 powerup_respawn_timer = pygame.time.get_ticks()
Power-Up Timer Visualization
To provide a visual representation of the power-up timer, you can draw a rectangular object that fades away over time. Create a new file named bar.py and add the code with the updates below:
# Thi?t l?p thanh power-up bar_width = 100 bar_height = 10 bar_x = window_width - bar_width - 10 bar_y = 10 # . # Tính toán ti?n trình h?n gi? ch?y power-up if shield_active: current_time = pygame.time.get_ticks() elapsed_time = current_time - shield_timer timer_progress = (5000 - elapsed_time) / 5000 # Hi?n thanh power-up bar_rect = pygame.Rect(bar_x, bar_y, bar_width * timer_progress, bar_height) pygame.draw.rect(window, (0, 255, 255), bar_rect)
Result:

Here's how to design and implement power-ups and collections in PyGame . Hope the article is useful to you.
Conclusion
Understanding Pygame makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.
FAQ
How do you create power-ups and collections in pygame?
Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.
Is it safe to create power-ups and collections in pygame?
It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.
What should you do if the process does not work?
Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.
Reader Comments 0
Sign in with email or Google to join the discussion.