Cách tạo Power-Ups và bộ sưu tập trong Pygame

Tạo dự án Pygame thú vị hơn bằng cách tích hợp power-up và bộ sưu tập. Dưới đây là hướng dẫn chi tiết.

Pygame

Power-up và bộ sưu tập đóng vai trò quan trọng trong việc nâng cao gameplay và làm game hấp dẫn hơn. Bằng cách thêm những thành phần này cho dự án Pygame, bạn có thể tạo ra những thử thách độc đáo, tăng động lực cho người chơi và mang tới cơ hội đưa ra quyết định có chiến lược.

Thật may, PyGame mang tới phương thức linh hoạt để triển khai power-up và bộ sưu tập tương đối dễ dàng.

Tạo game đơn giản

Trước khi bắt đầu, đảm bảo bạn đã cài pip trên hệ thống. Dùng lệnh này để cài thư viện pygame:

pip install pygame

Bắt đầu bằng cách thiết lập một game đơn giản tại nơi người chơi có thể di chuyển sang trái và phải trong khi tránh kẻ thù.

Điều này đóng vai trò là nền tảng để thêm power-up và bộ sưu tập. Đây là đoạn code mẫu:

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()

Kết quả:

Tạo game đơn giản

Tạo bộ sưu tập

Để tạo bộ sưu tập, thêm một đồ vật mà khi va chạm với người chơi, nó biến mất và game cộng thêm 10 điểm cho người chơi. Để làm việc này, bạn phải kiểm tra va chạm giữa người chơi và bộ sưu tập. Đây là phiên bản được udpate của code bao gồm bộ sưu tập. Tạo file mới tên collectibles.py và thêm code cùng những update sau:

# 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))

Kết quả:

Tạo bộ sưu tập

Tạo Power-Up

Giờ bạn có thể giới thiệu power-up cho game bằng logic sau. Khi người chơi va chạm với vật power-up, nó sẽ biến mất. Nếu người chơi va chạm với kẻ thù trong khi kích hoạt power-up, kẻ thù sẽ bị loại bỏ. Tạo file mới tên powerups.py và thêm code cùng những update bên dưới:

# 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

Kết quả:

Tạo Power-up trong Pygame

Thiết lập thời gian chạy power-up

Để làm power-up biến mất sau một khoảng thời gian nhất định và xuất hiện lại ở một vị trí ngẫu nhiên, bạn có thể dùng bộ hẹn giờ. Tạo file mới tên timer.py và thêm code với những update bên dưới:

# 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()

Trực quan hóa đồng hồ bấm giờ chạy power-up

Để cung cấp một đại diện trực quan cho đồng hồ bấm giờ power-up, bạn có thể vẽ một đối tượng hình chữ nhật mà biến mất dần dần theo thời gian. Tạo file mới tên bar.py và thêm code với những update bên dưới:

# 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)

Kết quả:

Hoàn thành game với power-up và bộ sưu tập

Trên đây là cách thiết kế và triển khai power-up và bộ sưu tập trong PyGame. Hi vọng bài viết hữu ích với các bạn.

Thứ Hai, 10/07/2023 16:35
51 👨 154
0 Bình luận
Sắp xếp theo
    ❖ Python