David Safro
David Safro

Reputation: 41

Why does the text not render on canvas Pygame

So I'm working on a pong copy using pygame. So far everything was working fine with pygame and everything was rendering. I wanted to add the player score to the screen, and I can't get it up. I'm not actually familiar with pygame that much so I pretty muched copied the text code from the docs and other sources. However they would not render, but when I opened a standalone, python file, it worked fine. Here is my main.py file where I render text and a link to the demo using replit: https://replit.com/@Glitchez/Pong-demo?v=1

import pygame
from paddles import *
from controls import *
from ball import *
pygame.font.init() 
# SCREEN INIT
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")

# VARIABLES
ball_radius = 7
paddle_speed = 5
paddle_length = 100
FPS = 60

# DEFINE OBJECTS
paddle1 = Paddle(paddle_length, 15, 100, paddle_speed, True, WHITE)
paddle2 = Paddle(paddle_length, 970, 100, paddle_speed, False, WHITE)
ball = Ball(WIDTH // 2, HEIGHT // 2, ball_radius)
font = pygame.font.SysFont(None, 24)


# COLLISION MANAGEMENT
def handle_collision(ball, paddle1, paddle2):
    if ball.y + ball.radius >= HEIGHT:
        ball.y_vel *= -1
    elif ball.y - ball.radius <= 0:
        ball.y_vel *= -1

    if ball.x_vel < 0:
        if paddle1.y <= ball.y <= paddle1.y + paddle1.height:
            if ball.x - ball.radius <= paddle1.x + paddle1.width:
                ball.x_vel *= -1

                middle_y = paddle1.y + paddle1.height / 2
                difference_in_y = middle_y - ball.y
                reduction_factor = (paddle1.height / 2) / ball.MAX_VEL
                y_vel = difference_in_y / reduction_factor
                ball.y_vel = -1 * y_vel
    else:
        if paddle2.y <= ball.y <= paddle2.y + paddle2.height:
            if ball.x + ball.radius >= paddle2.x:
                ball.x_vel *= -1

                middle_y = paddle2.y + paddle2.height / 2
                difference_in_y = middle_y - ball.y
                reduction_factor = (paddle2.height / 2) / ball.MAX_VEL
                y_vel = difference_in_y / reduction_factor
                ball.y_vel = -1 * y_vel



# HANDLE ALL GRAPHICS
def graphics(screen):
    screen.fill(BLACK)
    paddle1.draw(screen)
    paddle2.draw(screen)
    ball.draw(screen)
    # DRAWS DOTTED LINE
    for i in range(10, HEIGHT, HEIGHT // 20):
        if i % 2 == 1:
            continue
        pygame.draw.rect(screen, WHITE, (WIDTH // 2 - 5, i, 8, HEIGHT // 40))


# GAME LOOP
def main():
    run = True
    clock = pygame.time.Clock()
    P1_SCORE, P2_SCORE = 0, 0
    #ISSUE HERE!!!
    my_font = pygame.font.SysFont('Comic Sans MS', 30)
    text_surface = my_font.render(str(P1_SCORE), False, (255, 255, 255))
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        win.blit(text_surface, (250,250))
        paddle1.move()
        paddle2.move()
        ball.move()
        handle_collision(ball, paddle1, paddle2)
        P1_SCORE, P2_SCORE = ball.check_win(P1_SCORE, P2_SCORE)
        graphics(win)
        pygame.display.update()
        print(f"player 1: {P1_SCORE}, player 2: {P2_SCORE}")
    pygame.quit()


if __name__ == '__main__':
    main()

Upvotes: 2

Views: 74

Answers (1)

Kingsley
Kingsley

Reputation: 14916

The code is rendering the score correctly, blit()ing it to the window perfectly... but then erasing everything when the screen is painted in graphics().

So, you have a function graphics(), move the score-drawing into there.

# HANDLE ALL GRAPHICS
def graphics(screen, font, p1_score, p2_score):
    screen.fill(BLACK)
    paddle1.draw(screen)
    paddle2.draw(screen)
    ball.draw(screen)
    # DRAWS DOTTED LINE
    for i in range(10, HEIGHT, HEIGHT // 20):
        if i % 2 == 1:
            continue
        pygame.draw.rect(screen, WHITE, (WIDTH // 2 - 5, i, 8, HEIGHT // 40))
    # DRAWS THE SCORE
    # PLAYER 1  (LEFT)
    text_surface = my_font.render(str(p1_score), False, (255, 255, 255))
    screen.blit(text_surface, (250,250))
    # PLAYER 2  (RIGHT)
    text_surface = my_font.render(str(p2_score), False, (255, 255, 255))
    screen.blit(text_surface, (WIDTH-WIDTH//4,250))  # 25% from the right-side

# GAME LOOP
def main():
    run = True
    clock = pygame.time.Clock()
    P1_SCORE, P2_SCORE = 0, 0
    my_font = pygame.font.SysFont('Comic Sans MS', 30)
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        paddle1.move()
        paddle2.move()
        ball.move()
        handle_collision(ball, paddle1, paddle2)
        P1_SCORE, P2_SCORE = ball.check_win(P1_SCORE, P2_SCORE)
        graphics(win, my_font, P1_SCORE, P2_SCORE)
        pygame.display.update()
        print(f"player 1: {P1_SCORE}, player 2: {P2_SCORE}")
    pygame.quit()

NOTE: This is not tested code, errors and omissions should be expected.

If the text-rendering becomes slow, you could also pre-render all the scores to an array of surfaces on start-up. Then just blit the one that matches the score.

Upvotes: 2

Related Questions