0

I'm making a recreation of Pong, and I'm trying to add a text that displays the score.

I have tried so many ways to add a text, but they never worked. I just need someone to help me and teach me what I probably did wrong.

This is my code:

# Set up the display surface
DISPLAYSURF = pygame.display.set_mode((500, 400))

# Set up the clock
fpsClock = pygame.time.Clock()

# Set up the colors
BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
BLU = pygame.Color(0,0,255)
RED = pygame.Color(255,0,0)

#!!!ADD TEXT SCORE HERE!!!

while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.locals.QUIT:
            pygame.quit()
            sys.exit()


    # Check for ball scoring
    if ball_x - ball_radius < 0:
        paddle2_score += 1
        ball_x = 250
        ball_y = 200
        ball_dx = 3
        print("Red: ",paddle2_score)
    elif ball_x + ball_radius > 500:
        paddle1_score += 1
        ball_x = 250
        ball_y = 200
        ball_dx = -3
        print("Blu: ",paddle1_score)

#!!!UPDATE TEXT SCORE HERE!!!

    # Keep the paddles on the screen
    if paddle1_y < 0:
        paddle1_y = 0
    elif paddle1_y + paddle1_h > 400:
        paddle1_y = 400 - paddle1_h
    if paddle2_y < 0:
        paddle2_y = 0
    elif paddle2_y + paddle2_h > 400:
        paddle2_y = 400 - paddle2_h

    # Update the display
    pygame.display.flip()

    # Control the frame rate
    fpsClock.tick(30)

This is only part of the code.

leetloser
  • 9
  • 3
  • pygame doesn't do text. Instead, you create a font and a bitmap, draw the text into the bitmap, then blit the bitmap to the screen. https://pygame.readthedocs.io/en/latest/4_text/text.html – Tim Roberts Jan 08 '23 at 01:17
  • However, for the genuine Pong experience, perhaps you should draw your text using that 7-segment block display the original game used. That's not hard to draw with rectangles. – Tim Roberts Jan 08 '23 at 01:17
  • @TimRoberts you can post your first comment as an answer since it worked. – leetloser Jan 08 '23 at 15:27

1 Answers1

0

Try putting this function before the while loop.

font = pygame.font.Font("freesansbold.ttf", size)

def text(x, y, txt, size, txtcolor):
    display_text = font.render(txt, True, txtcolor)
    display.blit(display_text, (x, y))

From there, you will be able to call the function anywhere in the while loop and it will display whichever text you want. Make sure you give it correct parameters

  • x = The x coordinate of the text.
  • y = The y coordinate of the text.
  • txt = The text that you want to display (in this case, the score).
  • size = The size of the text.
  • txtcolor = The colour for the text (use your colour variables for this).

PS: you can try changing the font to something else if you want to, but I've tried that before and it didn't work. So you'll have to experiment on your own (Sorry).

edit: Moved the font out of the function for better performance.

  • Do not create a new `pygame.font.Font` object every time you draw a text. Creating a `pygame.font.Font` object is a very time-consuming operation. See [How to render/blit text in pygame for good performance](https://stackoverflow.com/questions/64563528/how-to-render-blit-text-in-pygame-for-good-performance/64563594#64563594). – Rabbid76 Jan 08 '23 at 07:49
  • It still doesn't display... – leetloser Jan 08 '23 at 15:10
  • @leetloser Does it state any errors? And are you sure you gave it correct parameters? – yellowway360 Jan 08 '23 at 19:52
  • No, @TimRoberts solved it if you haven't seen already. – leetloser Jan 08 '23 at 20:56