0

I'm trying to just draw any text on to the screen in pygame but when I run the program it just runs normally but no text appears on the screen. Is there something I'm doing wrong?

    import pygame, random, sys
    from pygame.locals import *
    pygame.init()
    #set constants
    WINDOWWIDTH = 600
    WINDOWHEIGHT = 600
    TEXTCOLOR = (255, 255, 255)
    BACKGROUNDCOLOR = (0, 0, 255)
    FPS = 40
    BLACK = (0,0,0)
    RED = (255, 0, 0)
    WHITE = (255, 255, 255)
    #set variables
    rectY1 = 200
    rectY2 = 200
    Y1change = 0
    Y2change = 0
    ballX = 320
    ballY = 240
    ballXvel = 0
    ballYvel = 0
    paddle1 = pygame.Rect(18,rectY1,10,100)
    paddle2 = pygame.Rect(620,rectY2,10,100)
    ball = pygame.Rect(ballX,ballY,30,30)
    font = pygame.font.SysFont(None, 48)
    #create shapes
    def drawshapes():   
        pygame.draw.rect(DISPLAY, WHITE, paddle1)
        pygame.draw.rect(DISPLAY, WHITE, paddle2)
        pygame.draw.rect(DISPLAY, WHITE, ball)
    def drawText(text, font, surface, x, y):
        textobj = font.render(text, 1, TEXTCOLOR)
        textrect = textobj.get_rect()
        textrect.topleft = (x, y)
        surface.blit(textobj, textrect)

    #set up display    

    DISPLAY = pygame.display.set_mode((640,480),0,32)
    pygame.display.set_caption('Pong')
    fpsClock = pygame.time.Clock()
    drawText('bruh', font, DISPLAY, (200), (200))
    pygame.display.update
  • 1
    Isn't `pygame.display.update` a function? – Jongware Feb 29 '20 at 18:23
  • ... `pygame.display.update` -> `pygame.display.update()`. It's a typo, you have missed the parentheses. – Rabbid76 Feb 29 '20 at 18:24
  • @Rabbid76 It still doesn't work. Is there anything else? – beginnercoder Feb 29 '20 at 18:29
  • 2
    @beginnercoder Your code works fine. But since you have no application loop, the window opens and closes immediately. You have to draw the text in the application loop. See [How to display text in pygame?](https://stackoverflow.com/questions/20842801/how-to-display-text-in-pygame) or [How to make a string's content appears on screen as we type on keyboard?](https://stackoverflow.com/questions/60455692/how-to-make-a-strings-content-appears-on-screen-as-we-type-on-keyboard/60456556#60456556). – Rabbid76 Feb 29 '20 at 18:39
  • 1
    find any tutorial and you should see that it use loop to get events - and this loop keeps program running till you exit this loop. – furas Feb 29 '20 at 19:35

1 Answers1

-1

idk if this really is the loop you are talking about but here it is:

running = True
while running:
    # RGB - Red, Green, Blue
    screen.fill((0, 0, 128 ))
    #background Image
    screen.blit(background, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219