在 Pygame 秒后将间隔减少 n
Decrease interval by n after n seconds in Pygame
我有一个 pygame
事件(称为 bubble_spawn_event),它的间隔(称为 bubble_spawn_interval)为 1000。如何使该间隔下降 0.1每 10 秒秒?
我尝试创建一个间隔 (bubble_interval_change_interval) 为 10 秒的单独事件(称为 bubble_interval_change_event),当该事件发生时,减少 bubble_spawn_interval
0.1 秒,然后在同一事件上调用 pygame.time.set_timer()
。 (bubble_spawn_event)
我定义了泡泡生成事件和间隔如下:
bubble_spawn_event = USEREVENT
bubble_spawn_interval = 1000
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
区间变化事件和区间定义如下:
bubble_interval_change_event = USEREVENT + 1
bubble_interval_change_interval = 10000
我尝试更改泡泡生成间隔:
elif event.type == bubble_interval_change_event:
bubble_spawn_interval -= 200
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
我的完整主循环:
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == bubble_spawn_event:
bubbles.add(Bubble(screen, random.randint(100, screen.get_width() - 100), screen.get_height()))
elif event.type == bubble_interval_change_event:
pygame.time.set_timer(bubble_spawn_event, 0)
bubble_spawn_interval -= 200
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
screen.blit(bg_image, (0, 0))
bubbles.update()
for bubble in bubbles:
bubble.screen.blit(bubble.image, bubble.rect)
bubble.move()
pygame.display.update()
clock.tick(60)
pygame.quit()
问题是泡泡生成间隔根本没有减少,即使我将间隔减少 0.9 秒也是如此。
您显然没有重置计时器:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((700, 400))
spawn_bubble = pygame.USEREVENT
spawn_bubble_timer = 1000
pygame.time.set_timer(spawn_bubble, spawn_bubble_timer)
reduce_timer = spawn_bubble + 1
reduce_timer_timer = 5000
pygame.time.set_timer(reduce_timer, reduce_timer_timer)
run = True
while run:
pygame.time.Clock().tick(10)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == spawn_bubble:
pygame.draw.circle(screen, (255, 0, 0),
(random.randint(0, 700), random.randint(0, 400)), 5)
if event.type == reduce_timer:
pygame.time.set_timer(spawn_bubble, 0)
spawn_bubble_timer -= 200
if spawn_bubble_timer <= 0:
spawn_bubble_timer = 200
pygame.time.set_timer(spawn_bubble, spawn_bubble_timer)
pygame.display.update()
pygame.quit()
注意行 pygame.time.set_timer(spawn_bubble, 0)
,它从该事件中删除了计时器。如果你 运行 这段代码,你会看到随着时间的推移,点开始出现得越来越快。 (上限为 200 毫秒,这样它就不会自行取消或进入负数,实际上上限也可以取消 reduce_timer
事件调度)
P.S。这实际上是我第一次使用 .set_timer()
方法
我有一个 pygame
事件(称为 bubble_spawn_event),它的间隔(称为 bubble_spawn_interval)为 1000。如何使该间隔下降 0.1每 10 秒秒?
我尝试创建一个间隔 (bubble_interval_change_interval) 为 10 秒的单独事件(称为 bubble_interval_change_event),当该事件发生时,减少 bubble_spawn_interval
0.1 秒,然后在同一事件上调用 pygame.time.set_timer()
。 (bubble_spawn_event)
我定义了泡泡生成事件和间隔如下:
bubble_spawn_event = USEREVENT
bubble_spawn_interval = 1000
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
区间变化事件和区间定义如下:
bubble_interval_change_event = USEREVENT + 1
bubble_interval_change_interval = 10000
我尝试更改泡泡生成间隔:
elif event.type == bubble_interval_change_event:
bubble_spawn_interval -= 200
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
我的完整主循环:
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == bubble_spawn_event:
bubbles.add(Bubble(screen, random.randint(100, screen.get_width() - 100), screen.get_height()))
elif event.type == bubble_interval_change_event:
pygame.time.set_timer(bubble_spawn_event, 0)
bubble_spawn_interval -= 200
pygame.time.set_timer(bubble_spawn_event, bubble_spawn_interval)
screen.blit(bg_image, (0, 0))
bubbles.update()
for bubble in bubbles:
bubble.screen.blit(bubble.image, bubble.rect)
bubble.move()
pygame.display.update()
clock.tick(60)
pygame.quit()
问题是泡泡生成间隔根本没有减少,即使我将间隔减少 0.9 秒也是如此。
您显然没有重置计时器:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((700, 400))
spawn_bubble = pygame.USEREVENT
spawn_bubble_timer = 1000
pygame.time.set_timer(spawn_bubble, spawn_bubble_timer)
reduce_timer = spawn_bubble + 1
reduce_timer_timer = 5000
pygame.time.set_timer(reduce_timer, reduce_timer_timer)
run = True
while run:
pygame.time.Clock().tick(10)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == spawn_bubble:
pygame.draw.circle(screen, (255, 0, 0),
(random.randint(0, 700), random.randint(0, 400)), 5)
if event.type == reduce_timer:
pygame.time.set_timer(spawn_bubble, 0)
spawn_bubble_timer -= 200
if spawn_bubble_timer <= 0:
spawn_bubble_timer = 200
pygame.time.set_timer(spawn_bubble, spawn_bubble_timer)
pygame.display.update()
pygame.quit()
注意行 pygame.time.set_timer(spawn_bubble, 0)
,它从该事件中删除了计时器。如果你 运行 这段代码,你会看到随着时间的推移,点开始出现得越来越快。 (上限为 200 毫秒,这样它就不会自行取消或进入负数,实际上上限也可以取消 reduce_timer
事件调度)
P.S。这实际上是我第一次使用 .set_timer()
方法