为什么是我的方程式渲染的毫秒和消失

问题描述 投票:0回答:2
因此,我正在制作一个项目,在某个时间点,需要将方程式渲染到屏幕上,我完成了这一点。但是我遇到了一个问题,即方程仅用于毫秒,然后消失。我将问题追溯到'屏幕。((102,178,255))。当这件代码在主循环中时,方程式仅呈现我所说的。移动。因此,在主循环中,是否有一种方法来解决此问题。 从代码中删除eq_done变量将无法使用,因为我只需要一个方程式,如果您删除eq_done变量,则随机方程将开始在PyGame窗口上闪烁 thanks!

import pygame import random pygame.init() screen = pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() done = False equations = ['2 + 2', '3 + 1', '4 + 4', '7 - 4'] font = pygame.font.SysFont("comicsansms", 72) tks = pygame.time.get_ticks() cloud1 = pygame.image.load('cloud.png') cloud1_X, cloud1_Y = 100, 50 cloud1_Y_change = 0 def cloud1_display(x, y): screen.blit(cloud1, (x, y)) def display_equation(): text = font.render(random.choice(list(equations)), True, (0, 128, 0)) screen.blit(text, (320 - text.get_width() // 2, 240 - text.get_height() // 2)) rocket_up = False eq_done = False while not done: screen.fill((102, 178, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: done = True tks = pygame.time.get_ticks() if tks > 5000 and not eq_done: display_equation() eq_done = True # only render once cloud1_display(cloud1_X, cloud1_Y) pygame.display.update() clock.tick(60)

您需要重新绘制每个帧的完整屏幕。

像绘制云一样绘制方程式。

if pygame.time.get_ticks() > 5000: display_equation()
    
python python-3.x function pygame 2d
2个回答
1
投票
如果您想在一段时间内渲染方程,则必须删除

eq_done

,但是必须测试当前时间是否小于某个时间点。例如:

while not done: # [...] tks = pygame.time.get_ticks() if 5000 < tks < 6000: display_equation() # [...]
    

1
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.