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
- 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
May be interested
- Easier game development with Pygame Zeropygame zero simplifies game development by doing a lot of the work for you.
- Invite download Power-user, create professional Slide for PowerPoint in 1 minutepower-user add-in will provide you with a huge number of templates (template) accompanied by a variety of designs, designs to help you unleash creativity, save time and effort when making silde for powerpoint presentations.
- Set up a separate power source for Windows 7 on Laptop?if you are downloading a large packet of data and get up to sleep, it is likely that the download process is interrupted due to the intervention of customizations in power options. therefore, it is best to reset or create yourself a personal setting in the power options to activate each time you need it.
- How to Access Your Personal Power to Create Successpersonal power can make the difference between promotion and stagnation. learn how to find and own your power to achieve more.
- Compare Microsoft Excel and Power BIexcel and power bi are two very popular tools. both offer a range of visualization and analysis tools to help you create dashboards and reports.
- How much power is the PC consuming?as people become more aware of the impact people have on the environment, one issue to consider is how much power your computer is using. you may be interested in how much it will cost you.
- How to enable low power mode on iPhoneinstead of letting the iphone turn on low power mode when the battery drops to 20%, users can set up a task to automatically turn on low power mode when it reaches a certain battery % level that you want.
- How to enable / disable Show Suggestions from Pinterest in Collections in Microsoft Edge Chromiumthis tutorial will show you how to enable or disable show suggestions from pinterest in collections in chromium-based microsoft edge.
- How to create the Gallery collection on iPhonethe iphone maps application helps you create location collections according to your different needs.
- 4 things to note before choosing to buy a PSU power supply for a PCamong the computer's components, the pc power supply (or psu - short for power supply unit) plays an extremely important role. if the psu operates stably and provides adequate power, the system will operate more smoothly and enduringly.