절제해서 표현하기
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 = min(shake + 12, 18)로 변경하세요. - 루프 전에
shake_on = True로 설정하세요. S 키를 누르면 값이 전환됩니다.shake_on이 참일 때만 임의의 오프셋을 선택하세요. - world를 blit한 후 화면의
(10, 10)위치에Shake: On또는Shake: Off를 그리세요.
‘플레이’를 누른 다음 S 키를 누르세요. 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 플레이그라운드