How to Create Power-Ups and Collections in Pygame
Power-ups and collections play an important role in enhancing gameplay and making the game more engaging. By adding these components to your Pygame project, you can create unique challenges, increase player motivation, and provide opportunities for strategic decision-making.
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.
You should read it
- How to create scrolling backgrounds in Pygame
- How to Program a Game in Python with Pygame
- Easier game development with Pygame Zero
- How to create a Start menu and a Game Over screen with PyGame
- How to add a scrolling camera to PyGame
- How to use sprites in Arcade to develop games
- How to add dialogue system in Python game using Arcade library
- How to make Hangman game in Python
- More than 100 Python exercises have solutions (sample code)
- Build a color game using Tkinter in Python
- 5 choose the best Python IDE for you
- Python online editor