2

I have this code and it's not displaying the text assigned to the variable "title" in the middle of the width of the screen. Is there any way to center it other than guessing the positions based on how long the text is?

import pygame
pygame.init()
pygame.font.init()

WIDTH = 1920
HEIGHT = 1080

CYAN = (0, 255, 255)
RED = (255, 0, 0)

window = pygame.display.set_mode((WIDTH, HEIGHT))

def startWindow(): 
    titleFont = pygame.font.SysFont("Comic Sans MS", 35)

    window.fill(RED)
    title = titleFont.render("Ball Game", False, CYAN)
    window.blit(title, (WIDTH//2, 35))
    pygame.display.update()


startWindow()
A.F.
  • 23
  • 3

1 Answers1

2

You should first get the text rectangle after rendering it, then center the rectangle relative to the screen width. Afterwhich you can pass it into blit.

centerTitle = title.get_rect(center=(WIDTH/2,35))
window.blit(title, centerTitle)
blessthefry
  • 121
  • 7