pygame.time.get_ticks()
안에 while
루프를 사용하여 요소를 그릴시기를 제어 할 수 있습니다. 이렇게하면 지연이 다른 여러 요소를 그릴 수 있으며 키를 사용하여 중지 할 수 있습니다. 같은 지연의 모든 그릴해야하는 경우
import pygame
import random
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
SIDE = 600
# --- main ---
# - init -
pygame.init()
win = pygame.display.set_mode((SIDE, SIDE))
win.fill(WHITE)
pygame.display.update()
# - objects -
circles_number = 100
next_circle = pygame.time.get_ticks() + 500 # 500ms = 0.5s
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
running = False
if circles_number > 0:
# get current time
current = pygame.time.get_ticks()
# check if it is time to draw next circle
if current >= next_circle:
radius = random.randrange(2, 15)
r = random.randrange(256)
g = random.randrange(256)
b = random.randrange(256)
x = random.randrange(0 + radius, SIDE - radius)
y = random.randrange(0 + radius, SIDE - radius)
pygame.draw.circle(win, (r, g, b), (x, y), radius)
pygame.display.update()
# time for next circle
next_circle = pygame.time.get_ticks() + 500 # 500ms = 0.5s
# counting down circles
circles_number -= 1
clock.tick(25)
pygame.quit()
당신은 프레임 사이의 시간을 제어 할 수 clock.tick(2)
를 사용할 수 있습니다. 그러나 키도 동일한 지연으로 검사됩니다. 키가 너무 오래 지연되어 작동하기 때문에 딜레이가 너무 길면 문제가 될 수 있습니다.
import pygame
import random
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
SIDE = 600
# --- main ---
# - init -
pygame.init()
win = pygame.display.set_mode((SIDE, SIDE))
win.fill(WHITE)
pygame.display.update()
# - objects -
circles_number = 100
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
running = False
if circles_number > 0:
radius = random.randrange(2, 15)
r = random.randrange(256)
g = random.randrange(256)
b = random.randrange(256)
x = random.randrange(0 + radius, SIDE - radius)
y = random.randrange(0 + radius, SIDE - radius)
pygame.draw.circle(win, (r, g, b), (x, y), radius)
pygame.display.update()
# counting down circles
circles_number -= 1
# 2 frames per seconds = 2 circles per second
clock.tick(2)
pygame.quit()
또한 이벤트를 사용하여 주기적으로 코드를 그릴 수 있습니다.
import pygame
import random
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
SIDE = 600
DRAW_CIRCLE_EVENT = pygame.USEREVENT
# --- main ---
# - init -
pygame.init()
win = pygame.display.set_mode((SIDE, SIDE))
win.fill(WHITE)
pygame.display.update()
# - objects -
circles_number = 10
# run event periodically
pygame.time.set_timer(DRAW_CIRCLE_EVENT, 500) # 500ms = 0.5s
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
running = False
if event.type == DRAW_CIRCLE_EVENT:
if circles_number > 0:
radius = random.randrange(2, 15)
r = random.randrange(256)
g = random.randrange(256)
b = random.randrange(256)
x = random.randrange(0 + radius, SIDE - radius)
y = random.randrange(0 + radius, SIDE - radius)
pygame.draw.circle(win, (r, g, b), (x, y), radius)
pygame.display.update()
# counting down circles
circles_number -= 1
else:
# disable events
pygame.time.set_timer(DRAW_CIRCLE_EVENT, 0)
clock.tick(25)
pygame.quit()
"util.wait_for_quit()"은 무엇입니까? 'for event' 루프로 표준'while' 루프를 사용하는 편이 낫습니다. – furas