Menu
CoddyTech

Chơi lại

Bài học 17 trên 41 trong khóa học Tạo trò chơi với Pygame của Coddy.

Sau khi trò chơi kết thúc, người chơi không cần khởi động lại chương trình. Khi nhấn R, hãy bắt đầu trò chơi mới bằng cách đưa mọi giá trị đã thay đổi về trạng thái ban đầu.

elif event.key == pygame.K_r and (lives == 0 or score == 10):
    score = 0
    lives = 3
    basket.x = 200
    block.y = -20
    block.x = random.randint(0, 460)

Điều kiện này rất quan trọng. Nếu không có (lives == 0 or score == 10), phím R sẽ xóa điểm giữa chừng trong trò chơi. Dấu ngoặc gom hai điều kiện kết thúc lại, nên and áp dụng cho cả hai.

Cho người chơi biết phím cần nhấn: hiển thị Press R to play again bên dưới kết quả.

challenge icon

Thử thách

Trung bình

Nhấn R để bắt đầu ván mới.

  1. Trong nhánh KEYDOWN, khi phím là pygame.K_r và trò chơi đã kết thúc, đặt điểm số thành 0, số mạng thành 3, basket.x thành 200, rồi thả một khối mới từ y = -20 tại một vị trí x ngẫu nhiên.
  2. Khi trò chơi đã kết thúc, hiển thị Press R to play again ở giữa tại (240, 200).

Nhấn nút "Chơi", để thua, rồi nhấn R: ván mới bắt đầu. Sau đó nhấn "Kiểm tra".

Tự làm thử

import random
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Catch the Blocks")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)

basket = pygame.Rect(200, 330, 80, 16)
block = pygame.Rect(220, 0, 20, 20)
score = 0
lives = 3
paused = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                paused = not paused

    if lives > 0 and score < 10 and not paused:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            basket.x -= 8
        if keys[pygame.K_RIGHT]:
            basket.x += 8
        basket.clamp_ip(screen.get_rect())

        block.y += 3
        if block.top > 360:
            block.y = -20
            block.x = random.randint(0, 460)
            lives -= 1
        if block.colliderect(basket):
            block.y = -20
            block.x = random.randint(0, 460)
            score += 1

    screen.fill((30, 30, 46))
    pygame.draw.rect(screen, (80, 160, 255), basket)
    pygame.draw.rect(screen, (235, 80, 80), block)
    score_text = font.render(f"Score: {score}", True, (240, 240, 240))
    screen.blit(score_text, (10, 10))
    lives_text = font.render(f"Lives: {lives}", True, (240, 240, 240))
    screen.blit(lives_text, (370, 10))
    if lives == 0:
        message = font.render("Game over", True, (240, 240, 240))
        screen.blit(message, message.get_rect(center=(240, 160)))
    elif score == 10:
        message = font.render("You win!", True, (240, 240, 240))
        screen.blit(message, message.get_rect(center=(240, 160)))
    if paused:
        message = font.render("Paused", True, (240, 240, 240))
        screen.blit(message, message.get_rect(center=(240, 160)))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Tất cả bài học trong Tạo trò chơi với Pygame

Tự luyện tập: Playground Pygame