在 pygame 中,有没有办法让我的代码的不同部分有不同的滴答率?
Is there a way to have different tick rates for differents parts of my code in pygame?
所以,我想让我的代码的一部分以特定速率刷新,而另一部分立即刷新。
import pygame
import random
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
PINK = (255,182,193)
INDIGO = (111,0,255)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
myText = "Score: 0"
myTextScore = 0
quitVar = True
FPS = 2
fpsClock = pygame.time.Clock()
while quitVar == True:
screen.fill(WHITE)
x = (random.randrange(1, 300))
y = (random.randrange(1, 300))
red_rect = pygame.draw.rect(screen, RED, (x, y, 100, 100))
#this is the code that i want to refresh slower
font = pygame.font.Font('./Roboto-Regular.ttf',20)
text = font.render(myText, True, GREEN)
textRect = text.get_rect(center = (200,350))
screen.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
if event.type == pygame.MOUSEBUTTONDOWN:
position = pygame.mouse.get_pos()
#i want the code below to refresh at a faster rate than what is above
if red_rect.collidepoint(position):
myTextScore += 1
myText = "Score " + str(myTextScore)
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
有没有办法针对不同的代码 fps/refreshing 不同的部分以不同的速率?我目前使用的 fps 会影响循环中的所有内容,但我希望它只影响特定部分,而另一部分速度更快,并以 10-20 fps 的速度刷新,而不是其他代码执行的 2 fps。我的目标是让一个正方形在屏幕上移动,当用户点击它时,他们的分数会更新。我正在尝试重新创建“打地鼠”
不行,没办法。另见 。
如果你想在 Pygame 中随着时间的推移控制某些东西,你有两个选择:
使用pygame.time.get_ticks()
测量时间并实现根据时间控制对象的逻辑。
使用定时器事件。在事件队列中使用 pygame.time.set_timer()
to repeatedly create a USEREVENT
。事件发生时更改对象状态。
有关示例,请参阅 or (稍加搜索即可找到更多示例)。
基于您的代码的最小示例:
import pygame
import random
WHITE, BLACK = (255,255,255), (0,0,0)
RED, GREEN, BLUE = (255,0,0), (0,255,0), (0,0,255)
YELLOW, PINK, INDIGO = (255,255,0), (255,182,193), (111,0,255)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
myTextScore = 0
#font = pygame.font.Font('./Roboto-Regular.ttf',20)
font = pygame.font.SysFont(None, 20)
text = font.render("Score " + str(myTextScore), True, GREEN)
change_event = pygame.USEREVENT
pygame.time.set_timer(change_event, 500) # 0.5 seconds
red_rect = pygame.Rect((random.randrange(1, 300)), (random.randrange(1, 300)), 100, 100)
FPS = 100
fpsClock = pygame.time.Clock()
quitVar = True
while quitVar == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
if event.type == change_event:
red_rect.x = (random.randrange(1, 300))
red_rect.y = (random.randrange(1, 300))
if event.type == pygame.MOUSEBUTTONDOWN:
if red_rect.collidepoint(event.pos):
myTextScore += 1
text = font.render("Score " + str(myTextScore), True, GREEN)
screen.fill(WHITE)
pygame.draw.rect(screen, RED, red_rect)
screen.blit(text, text.get_rect(center = (200,350)))
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
所以,我想让我的代码的一部分以特定速率刷新,而另一部分立即刷新。
import pygame
import random
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
PINK = (255,182,193)
INDIGO = (111,0,255)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
myText = "Score: 0"
myTextScore = 0
quitVar = True
FPS = 2
fpsClock = pygame.time.Clock()
while quitVar == True:
screen.fill(WHITE)
x = (random.randrange(1, 300))
y = (random.randrange(1, 300))
red_rect = pygame.draw.rect(screen, RED, (x, y, 100, 100))
#this is the code that i want to refresh slower
font = pygame.font.Font('./Roboto-Regular.ttf',20)
text = font.render(myText, True, GREEN)
textRect = text.get_rect(center = (200,350))
screen.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
if event.type == pygame.MOUSEBUTTONDOWN:
position = pygame.mouse.get_pos()
#i want the code below to refresh at a faster rate than what is above
if red_rect.collidepoint(position):
myTextScore += 1
myText = "Score " + str(myTextScore)
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
有没有办法针对不同的代码 fps/refreshing 不同的部分以不同的速率?我目前使用的 fps 会影响循环中的所有内容,但我希望它只影响特定部分,而另一部分速度更快,并以 10-20 fps 的速度刷新,而不是其他代码执行的 2 fps。我的目标是让一个正方形在屏幕上移动,当用户点击它时,他们的分数会更新。我正在尝试重新创建“打地鼠”
不行,没办法。另见
如果你想在 Pygame 中随着时间的推移控制某些东西,你有两个选择:
使用
pygame.time.get_ticks()
测量时间并实现根据时间控制对象的逻辑。使用定时器事件。在事件队列中使用
pygame.time.set_timer()
to repeatedly create aUSEREVENT
。事件发生时更改对象状态。
有关示例,请参阅
基于您的代码的最小示例:
import pygame
import random
WHITE, BLACK = (255,255,255), (0,0,0)
RED, GREEN, BLUE = (255,0,0), (0,255,0), (0,0,255)
YELLOW, PINK, INDIGO = (255,255,0), (255,182,193), (111,0,255)
pygame.init()
pygame.display.set_caption("Game TITLE")
screen = pygame.display.set_mode((400,400))
myTextScore = 0
#font = pygame.font.Font('./Roboto-Regular.ttf',20)
font = pygame.font.SysFont(None, 20)
text = font.render("Score " + str(myTextScore), True, GREEN)
change_event = pygame.USEREVENT
pygame.time.set_timer(change_event, 500) # 0.5 seconds
red_rect = pygame.Rect((random.randrange(1, 300)), (random.randrange(1, 300)), 100, 100)
FPS = 100
fpsClock = pygame.time.Clock()
quitVar = True
while quitVar == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitVar = False
if event.type == change_event:
red_rect.x = (random.randrange(1, 300))
red_rect.y = (random.randrange(1, 300))
if event.type == pygame.MOUSEBUTTONDOWN:
if red_rect.collidepoint(event.pos):
myTextScore += 1
text = font.render("Score " + str(myTextScore), True, GREEN)
screen.fill(WHITE)
pygame.draw.rect(screen, RED, red_rect)
screen.blit(text, text.get_rect(center = (200,350)))
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()