2 回答

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
您不是在計(jì)算自上次以來的時(shí)差powerup。time_difference始終是游戲開始后的時(shí)間。每次要重置計(jì)時(shí)器時(shí)都需要保存滴答聲,并與之進(jìn)行比較。
prev_time = pygame.time.get_ticks() #initialize tick counter
while running:
clock.tick(FPS)
time_difference = pygame.time.get_ticks() - prev_time
if time_difference >= 5000:
all_sprites.add(healthpack)
prev_time = pygame.time.get_ticks() # reset counter
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
draw_text(screen, str(time_difference), 18, WIDTH / 2, 55)
pygame.display.flip()

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
他對(duì) 的修復(fù)是正確的time_difference,但您的代碼也存在另一個(gè)問題。
另一個(gè)問題是healthpack. 你正在這樣做all_sprites.add(healthpack),這只會(huì)把舊的healthpack放回列表中all_sprites。那個(gè)精靈離開屏幕并下降。你需要一個(gè)新的實(shí)例HealthPack。所以你需要像這樣修改@Barmar的解決方案:
prev_time = pygame.time.get_ticks() #initialize tick counter
while running:
clock.tick(FPS)
time_difference = pygame.time.get_ticks() - prev_time
if time_difference >= 5000:
healthpack = HealthPack() # <----------------- add this line
all_sprites.add(healthpack)
prev_time = pygame.time.get_ticks() # reset counter
添加回答
舉報(bào)