程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Pygame 中的文本未显示在屏幕上大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Pygame 中的文本未显示在屏幕上?

开发过程中遇到Pygame 中的文本未显示在屏幕上的问题如何解决?下面主要结合日常开发的经验,给出你关于Pygame 中的文本未显示在屏幕上的解决方法建议,希望对你解决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() 或任何其他显示更新调用来显示文本。

大佬总结

以上是大佬教程为你收集整理的Pygame 中的文本未显示在屏幕上全部内容,希望文章能够帮你解决Pygame 中的文本未显示在屏幕上所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。