如何解决Pygame 中的文本未显示在屏幕上
我在 pygame 中做了一个 Pong 游戏,我有这个功能,将球设置为中心,并在玩家或对手进球时等待 2-3 秒
当球移到中心并等待 2-3 秒时,我添加了一个计时器 self.score_time = pygame.time.get_ticks()
,它将在进球时进行初始化。
在 ball_start() 中,我正在获取当前时间并检查持续时间并分别显示 3、2、1。
该功能可以很好地将球移到中心并停止 2-3 秒,但此 3,2,1 文本未显示在屏幕上。
使用的字体和颜色
self.light_grey = (200,200,200)
self.game_font = pygame.font.Font("freesansbold.ttf",50)
# when player or opponent scores,shift ball to center and randomise its initial direction
def ball_start(self):
self.ball.center = (self.screen_width / 2,self.screen_height / 2)
# checking after goal is scored if 2-3 secs are passed or not
current_time = pygame.time.get_ticks()
# 3
if current_time - self.score_time < 800:
num_3 = self.game_font.render("3",False,self.light_grey)
self.screen.blit(num_3,(self.screen_width / 2 - 10,self.screen_height / 2 + 20))
# 2
if current_time - self.score_time < 1600:
num_2 = self.game_font.render("2",self.light_grey)
self.screen.blit(num_2,self.screen_height / 2 + 20))
# 1
if current_time - self.score_time < 2400:
num_1 = self.game_font.render("1",self.light_grey)
self.screen.blit(num_1,self.screen_height / 2 + 20))
if current_time - self.score_time < 2400:
# making speeds 0 so ball won't move
self.ball_x_speed = 0
self.ball_y_speed = 0
else:
# if 2-3 secs are passed make ball move
self.ball_x_speed = 8 * random.choice((1,-1))
self.ball_y_speed = 8 * random.choice((1,-1))
self.score_time = None
解决方法
正如所料,问题不在您显示的代码中。
在您的主循环中,这是您拥有的渲染顺序:
if pong.score_time:
pong.ball_start()
pong.ball_animation()
pong.player_animation()
pong.opponent_animation()
pong.draw_objects()
这意味着列表中后面的所有内容都将覆盖前面的内容。
最值得注意的是,ball_start
位于底部。这可能不是什么大问题,但问题很大,因为您的背景填充位于 draw_objects
中,它位于 ball_start
之后。
要修复,只需将 ball_start
移动到 draw_objects
之后,或者(甚至更好的 IMO),将背景填充直接移动到主循环中。
pong.ball_animation()
pong.player_animation()
pong.opponent_animation()
pong.draw_objects()
if pong.score_time:
pong.ball_start()
,
文本被绘制到屏幕表面(blitted), 但未显示屏幕表面。
使用 pygame.display.flip() 或任何其他显示更新调用来显示文本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。