Menu
CoddyTech

Nhanh dần theo thời gian

Thuộc phần Vòng lặp trò chơi trong hành trình Pygame của Coddy. Bài học 35 trên 35.

Trò chơi sẽ khó hơn khi tiếp diễn. Ở đây, các khối rơi nhanh hơn một chút sau mỗi 10 giây.

pygame.time.set_timer(event_type, ms) gửi một sự kiện thuộc loại đó sau mỗi ms mili giây. Tạo loại sự kiện của riêng bạn bằng pygame.USEREVENT, một số mà pygame để dành cho bạn:

SPEED_UP = pygame.USEREVENT + 1
pygame.time.set_timer(SPEED_UP, 10000)
fall_speed = 3

for event in pygame.event.get():
    if event.type == SPEED_UP:
        fall_speed += 1

Sự kiện xuất hiện trong vòng lặp sự kiện cùng với các sự kiện khác, cứ mỗi 10 giây một lần. Sau đó, khối rơi với block.y += fall_speed thay vì luôn rơi với tốc độ 3. Trò chơi mới bắt đầu chậm lại, vì vậy khi khởi động lại, hãy đặt fall_speed = 3.

challenge icon

Thử thách

Trung bình

Làm cho các khối rơi nhanh hơn sau mỗi 10 giây.

  1. Bên dưới phông chữ, tạo SPEED_UP = pygame.USEREVENT + 1 và đặt bộ hẹn giờ chạy mỗi 10000 mili giây.
  2. Đặt fall_speed = 3 trước vòng lặp và dùng biến này để điều khiển tốc độ rơi.
  3. Tăng fall_speed thêm 1 mỗi khi có sự kiện SPEED_UP và đặt lại thành 3 khi nhấn R để bắt đầu trò chơi mới.

Nhấn Chơi rồi đợi 10 giây: các khối sẽ rơi nhanh hơn. 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
            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)

    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 lives == 0 or score == 10:
        again = font.render("Press R to play again", True, (240, 240, 240))
        screen.blit(again, again.get_rect(center=(240, 200)))
    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()
quiz iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

Tất cả bài học trong Vòng lặp trò chơi

Tự luyện tập: Playground Pygame