Menu
CoddyTech

시간이 지날수록 더 빠르게

Coddy Pygame 여정의 게임 루프 섹션에 포함된 레슨. 32개 중 32번째.

게임은 진행될수록 더 어려워집니다. 여기서는 블록이 10초마다 조금씩 더 빠르게 떨어집니다.

pygame.time.set_timer(event_type, ms)는 ms밀리초마다 해당 유형의 이벤트를 게시합니다. pygame.USEREVENT를 사용해 직접 이벤트 유형을 만들 수 있습니다. 이 값은 pygame이 여러분을 위해 비워 둔 숫자입니다.

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

이 이벤트는 다른 이벤트와 함께 이벤트 루프에 10초마다 한 번씩 들어옵니다. 그러면 블록은 고정값 3 대신 block.y += fall_speed에 따라 떨어집니다. 새 게임은 다시 느린 속도로 시작하므로, 재시작할 때도 fall_speed = 3으로 설정합니다.

challenge icon

챌린지

중급
  1. font 아래에 SPEED_UP = pygame.USEREVENT + 1을 설정하고, 10000 ms마다 이 이벤트가 발생하도록 타이머를 시작하세요.
  2. 루프 전에 fall_speed = 3으로 설정하고 낙하 속도에 이 값을 사용하세요.
  3. SPEED_UP 이벤트가 발생하면 fall_speed에 1을 더하세요.
  4. R 키를 눌러 새 게임을 시작할 때 값을 다시 3으로 설정하세요.

직접 해보기

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 icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

게임 루프의 모든 레슨

직접 연습해 보세요: Pygame 플레이그라운드