Разделить частоту обновления экрана и частоту обновления
Как мне можно поставить частоту цикла обновления мира фиксированную (120) и при этом не ограниченную частоту обновления экрана.
import pygame
import random
# settings
SC_W=1280
SC_H=720
PERFMON_SCALE = 1
BALL_RADIUS = 6
sc = pygame.display.set_mode((SC_W, SC_H))
clock = pygame.time.Clock()
pygame.init()
font = pygame.font.SysFont("arial", int(20 * perfmon_scale))
ball_count = 0
balls = []
class ballconstructor():
def __init__(self,x,y,dx,dy,color,r):
self.x = x
self.y = y
self.dx = dx
self.dy = dy
self.color = color
self.r = r
def draw(self,sc):
pygame.draw.circle(sc,self.color,(self.x,self.y),self.r)
for i in range(10):
balls.append(ballconstructor(random.randint(10, SC_W - 10), random.randint(10, SC_H - 10), random.randint(1, 3),random.randint(1, 3),(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),ball_radius))
ball_count +=1
while True:
keys = pygame.key.get_pressed()
sc.fill((45, 160, 185))
clock.tick(120)
fps = str(int(clock.get_fps()))
fps = font.render("FPS: "+fps, 1, (255, 255, 255))
ball_count_txt = font.render("BALLS: "+str(ball_count), 1, (255, 255, 255))
if keys[pygame.K_SPACE]:
for i in range(10):
balls.append(ballconstructor(random.randint(10, SC_W - 10), random.randint(10, SC_H - 10), random.randint(1, 3),random.randint(1, 3),(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),ball_radius))
ball_count +=1
for ball in balls:
if ball.x + ball.dx >= SC_W or ball.x + ball.dx <= 0:
ball.dx = -ball.dx
if ball.y + ball.dy >= SC_H or ball.y + ball.dy <= 0:
ball.dy = -ball.dy
ball.x += ball.dx
ball.y += ball.dy
ball.draw(sc)
perfbg = pygame.Surface((int(150*perfmon_scale),int(45*perfmon_scale)))
perfbg.set_alpha(100)
perfbg.fill((30,30,30))
sc.blit(perfbg,(1,int(3*perfmon_scale)))
sc.blit(fps, (3,int(3*perfmon_scale)))
sc.blit(ball_count_txt, (3, int(23*perfmon_scale)))
for a in pygame.event.get():
if a.type == pygame.QUIT:
quit()
pygame.display.update()
Ответы (1 шт):
Автор решения: alex9127
→ Ссылка
Ответ - никак. Дело в том, что clock.tick(120) устанавливает то, сколько итераций будет у цикла в секунду, а pygame.time.delay() слишком неэффективен из-за того, что неизвестно заранее, на каком компе будет это выполняться, и на разных машинах будет разный фпс. Возможно есть какое то решение, но я о нем не знаю. Если кто то еще дерзнет отвечать на этот вопрос, поделитесь пожалуйста этим решением, если оно есть.