PATHIK GHUGARE
PATHIK GHUGARE

Reputation: 155

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

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 .

In ball_start(), I am taking current time and checking the duration and respectively displaying 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.

font and color used

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

function code :

    # 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

Upvotes: 0

Views: 54

Answers (1)

MegaIng
MegaIng

Reputation: 7886

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. This might no be a big problem, but it is, because you background fill is in draw_objects, which comes after ball_start.

To fix, just move ball_start after draw_objects, or (even better IMO), move the background fill directly into the main loop.

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

Upvotes: 1

Related Questions