1

I have tried to change displayed text and it didn't work. How do i change it? I have tried to do variables that change. Here is my code. It is super messy, but i hope that you know how to help.

import pygame
import os
pygame.font.init()

FONT = pygame.font.SysFont('comicsans', 100)

SCREENX = 1700
SCREENY = 900

WHITE = (255, 255, 255)

x = 0
TEXT = FONT.render(str(x), 1, (255, 255, 255))

WIN = pygame.display.set_mode((SCREENX, SCREENY))
BACKGROUND = pygame.transform.scale(pygame.image.load(
    os.path.join('Assets', 'space.png')), (SCREENX, SCREENY))


def draw_window():
    WIN.blit(BACKGROUND, (0, 0))
    TEXT = FONT.render(str(x), 1, (255, 255, 255))
    WIN.blit(TEXT, ((SCREENX - TEXT.get_width()) /
         2, (SCREENY - TEXT.get_height()) / 2))
    pygame.display.update()


def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    x += 1
                
        draw_window()
    pygame.quit


if __name__ == '__main__':
    main()

So when i press space it says: UnboundLocalError: local variable 'x' referenced before assignment.

Can you please help? I have looked from google, but i haven't seen problem like mine. I use VS Code.

1 Answers1

1

You must use the global statement, if you wan to change a variable in global namespace within a function:

def main():
    global x # <--- this is missing

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    x += 1
                
        draw_window()
    pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174