Menu
CoddyTech

화면 흔들림

Coddy Pygame 여정의 사운드와 게임의 손맛 섹션에 포함된 레슨. 23개 중 12번째.

무거운 상자가 땅에 부딪히면 화면 전체가 잠시 덜컥 흔들립니다. 이것이 바로 화면 흔들림입니다. 몇 프레임 동안 모든 것이 매 프레임 조금씩 다른 방향으로 몇 픽셀씩 어긋나게 그려집니다.

모든 것을 한꺼번에 움직이려면 화면에 직접 그리는 대신 별도의 Surface에 월드를 그린 다음, 오프셋을 적용해 해당 Surface를 화면에 블리트하세요.

world = pygame.Surface((480, 360))

루프 안에서 screen을 사용하던 그리기 호출을 이제 world로 바꿉니다. 그런 다음 오프셋을 정합니다.

offset = (0, 0)
if shake > 0:
    amount = shake // 3
    offset = (random.randint(-amount, amount), random.randint(-amount, amount))
    shake -= 1
screen.fill((0, 0, 0))
screen.blit(world, offset)

shake는 흔들림이 남은 프레임 수를 셉니다. 착지하면 이 값을 12로 설정합니다. amount가 shake // 3이므로, 처음 몇 프레임에는 최대 4픽셀 움직이고 그다음에는 3, 2, 1픽셀, 마지막에는 움직이지 않습니다. 덜컥거림이 갑자기 멈추는 대신 점차 잦아듭니다. 월드가 4픽셀 움직이면 가장자리에 가느다란 빈틈이 생기므로 먼저 화면을 검은색으로 채우세요.

challenge icon

챌린지

중급
  1. 루프 전에 world = pygame.Surface((480, 360))를 만드세요.
  2. 그리기 코드 다섯 줄에서 screen을 world로 바꾸세요.
  3. shake가 0보다 큰 동안 amount = shake // 3과 random.randint(-amount, amount)를 사용해 x 및 y 오프셋을 정한 다음, shake를 1 줄이세요.
  4. 화면을 검은색으로 채우고 오프셋 위치에 world를 블릿하세요.

‘플레이’를 누른 다음 스페이스바를 누르세요. 착지할 때 화면이 흔들립니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

import random
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Crate Drop")
clock = pygame.time.Clock()

thud_sound = pygame.mixer.Sound("assets/sounds/thud.ogg")
falling = []
landed = []
shake = 0

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            falling.append(pygame.Rect(random.randint(140, 400), -40, 40, 40))

    for crate in falling:
        crate.y += 8
        if crate.bottom >= 300:
            crate.bottom = 300
            landed.append(crate)
            thud_sound.play()
            shake = 12
    falling = [crate for crate in falling if crate.bottom < 300]

    screen.fill((30, 30, 46))
    pygame.draw.rect(screen, (80, 80, 100), (0, 300, 480, 60))
    pygame.draw.rect(screen, (80, 160, 255), (60, 220, 30, 80))
    for crate in falling + landed:
        pygame.draw.rect(screen, (170, 110, 60), crate)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

사운드와 게임의 손맛의 모든 레슨

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