简体   繁体   English

Pygame 中的文本未显示在屏幕上

[英]Text not getting displayed on screen in Pygame

I was making a Pong game in pygame where I had this function which sets the ball to center and waits for 2-3 secs when player or opponent has scored a goal我在 pygame 中进行 Pong 游戏,我有这个 function 将球设置为中心并在玩家或对手进球时等待 2-3 秒

When the ball shifts to center and waits for 2-3 secs, I added a timer self.score_time = pygame.time.get_ticks() which will get initialised when the goal was made.当球移动到中心并等待 2-3 秒时,我添加了一个计时器self.score_time = pygame.time.get_ticks()将在进球时初始化。

In ball_start(), I am taking current time and checking the duration and respectively displaying 3, 2, 1.在 ball_start() 中,我正在获取当前时间并检查持续时间并分别显示 3、2、1。

The function is working fine for shifting ball to center and stopping for 2-3 secs but this 3, 2, 1 text is not getting displayed on the screen. function 工作正常,可以将球移到中心并停止 2-3 秒,但屏幕上没有显示这 3、2、1 文本。

font and color used使用的字体和颜色

self.light_grey = (200, 200, 200)
self.game_font = pygame.font.Font("freesansbold.ttf", 50)

function code: function 代码:

    # 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", False, self.light_grey)
            self.screen.blit(num_2, (self.screen_width / 2 - 10, self.screen_height / 2 + 20))

        # 1
        if current_time - self.score_time < 2400:
            num_1 = self.game_font.render("1", False, self.light_grey)
            self.screen.blit(num_1, (self.screen_width / 2 - 10, 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

As Expected, the problem is not inside the code you were showing.正如预期的那样,问题不在您显示的代码中。

In you main loop, this is the rending order you have:在您的主循环中,这是您拥有的渲染顺序:

    if pong.score_time:
        pong.ball_start()
    pong.ball_animation()
    pong.player_animation()
    pong.opponent_animation()
    pong.draw_objects()

This means that everything futher down in the list will override what comes before.这意味着列表中后面的所有内容都将覆盖之前的内容。

Most notably, ball_start is in the bottom.最值得注意的是, ball_start位于底部。 This might no be a big problem, but it is, because you background fill is in draw_objects , which comes after ball_start .这可能不是一个大问题,但它确实是,因为你的背景填充在draw_objects中,它在ball_start之后。

To fix, just move ball_start after draw_objects , or (even better IMO), move the background fill directly into the main loop.要修复,只需在ball_start之后移动draw_objects ,或者(甚至更好的 IMO),将背景填充直接移动到主循环中。

    pong.ball_animation()
    pong.player_animation()
    pong.opponent_animation()
    pong.draw_objects()
    if pong.score_time:
        pong.ball_start()

The text is being drawn to the screen surface (blitted), but the screen surface is not being shown.文本被绘制到屏幕表面(blitted),但屏幕表面没有显示。

Use pygame.display.flip() or any other display update call to show the text.使用 pygame.display.flip() 或任何其他显示更新调用来显示文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM