절제해서 표현하기
Coddy Pygame 여정의 사운드와 게임의 손맛 섹션에 포함된 레슨. 23개 중 15번째.
게임의 손맛은 양념과 같습니다. 약간 흔들리면 착지감이 묵직해지고, 너무 많이 흔들리면 게임을 파악하기 어려워지며 플레이어가 메스꺼움을 느낄 수도 있습니다. 두 가지 습관으로 흔들림을 적절히 조절할 수 있습니다.
상한을 두세요. 효과가 중첩되면 끝없이 커질 수 있습니다. 여기서는 착지할 때마다 shake에 12를 더합니다. 상자 네 개가 동시에 착지하면 값이 40을 넘어 화면이 13픽셀씩 움직입니다. min()으로 상한을 설정하세요.
shake = min(shake + 12, 18)이제 상자가 계속 착지하면 흔들림이 더 오래 지속되지만, 화면은 18 // 3인 6픽셀보다 많이 움직이지 않습니다.
플레이어가 흔들림을 끌 수 있게 하세요. 화면이 움직이면 게임을 할 수 없는 사람도 있습니다. 설정 하나를 추가하는 데 변수 하나면 충분합니다.
shake_on = True
...
if shake_on:
offset = (random.randint(-amount, amount), random.randint(-amount, amount))월드를 블릿한 뒤, 화면에 바로 레이블을 그리세요. 인터페이스의 텍스트는 월드가 흔들리는 동안에도 움직이지 않아야 합니다.
챌린지
중급스페이스 키를 누르면 상자가 떨어지고, 착지할 때마다 shake가 12씩 증가합니다. 효과를 줄여 보세요:
- 착지 시
shake가 18을 넘지 않도록 변경하세요:shake = min(shake + 12, 18). - 루프 전에
shake_on = True로 설정하세요. S 키를 누르면 값이 전환됩니다. shake_on이 참일 때만 임의의 오프셋을 선택하세요.- 월드를 blit한 뒤, 화면의
(10, 10)위치에Shake: On또는Shake: Off를 그리세요.
직접 해보기
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Crate Drop")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 28)
thud_sound = pygame.mixer.Sound("assets/sounds/thud.ogg")
falling = []
landed = []
shake = 0
world = pygame.Surface((480, 360))
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]
world.fill((30, 30, 46))
pygame.draw.rect(world, (80, 80, 100), (0, 300, 480, 60))
pygame.draw.rect(world, (80, 160, 255), (60, 220, 30, 80))
for crate in falling + landed:
pygame.draw.rect(world, (170, 110, 60), crate)
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)
pygame.display.flip()
clock.tick(60)
pygame.quit()
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
사운드와 게임의 손맛의 모든 레슨
직접 연습해 보세요: Pygame 플레이그라운드