1
프레임 속도를 조절하기위한 클래스와 함께 다른 색상을 통해주기를 그리는 작은 프로그램을 작성했습니다. 어떤 이유로 나는 그것을 돌릴 때마다 무작위로 깜빡이고 왜 나는 모릅니다. 그것은 매우 간단하여 화면이 충분히 빠르게 업데이트되는 것과 관련이 있다는 것을 의심합니다. 어떤 제안이라도 고맙게 생각합니다.파이 게임에서 깜박임
import pygame, time, random
w, h = 640, 480
screen = pygame.display.set_mode((w, h))
running = 1
inc = 20
m = [1, 1, 1]
c = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]
class FrameRate():
def __init__(self, rate = 60):
self.frame_rate = rate
self.refresh_time = 1.0/self.frame_rate
self.cur_time = time.time()
self.prev_time = time.time()
self.elapsed_time = 0
def update(self):
temp = self.cur_time
self.cur_time = time.time()
self.elapsed_time = self.cur_time - self.prev_time
self.prev_time = temp
def regulate_frame_rate(self):
if self.elapsed_time < self.refresh_time:
time.sleep(self.refresh_time - self.elapsed_time)
fr = FrameRate()
pygame.init()
while running:
fr.update()
fr.regulate_frame_rate()
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = 0
screen.fill((0, 0, 0))
# Reverse color direction
for i, p in enumerate(c):
if c[i] > 255 or c[i] < 0:
m[i] = -m[i]
c[i] += m[i]
for i in range(0,w/inc):
try:
pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, 0), (0, h - i*inc))
pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, 0), (w, h - i*inc))
pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, h), (0, i*inc))
pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, h), (w, i*inc))
except TypeError:
pass
pygame.display.flip()
pygame.quit()
깜빡임을 방지하는 가장 쉬운 방법은이 애플리케이션에서 필요로하지 않는 것처럼'screen.fill()'호출을 제거하거나 루프 밖으로 이동하는 것입니다. –
'time'에'pygame.time.get_ticks()'와'pygame.time.Clock'을 사용해야한다고 생각합니다. – ninMonkey