Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Create Power-Ups and Collections in Pygame

Learn about Pygame, including Create a Simple Game, key uses, practical steps, common issues, and answers to frequently asked questions.

Table of Contents

Pygame guide image 1

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 Simple Game - Pygame

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:

Create Power-Ups - Pygame

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:

Power-Up Timer Visualization - Pygame

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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.