0

I am making a very simple, and in fact, it would not be that innacurate to call it trashy, game to learn Pygame and Python, so sorry for all the begginery mess that this is.

Anyway, the issue is that the function does not seem to work even though I had called it.

Thanks for the help.

ButtonIMG = pygame.image.load("graphics/EmptyButton.png")

# This will select the button to press, and everything else button related

ButtonChoice = 1

button = "left"

def ButtonUpdate():
    ButtonChoice = randint(1,4)
    if ButtonChoice == 1:
        button = "left"
        ButtonIMG = pygame.image.load("graphics/LeftButton.png")
    if ButtonChoice == 2:
        button = "down"
        ButtonIMG = pygame.image.load("graphics/DownButton.png")
    if ButtonChoice == 3:
        button = "right"
        ButtonIMG = pygame.image.load("graphics/RightButton.png")
    if ButtonChoice == 4:
        button = "up"
        ButtonIMG = pygame.image.load("graphics/UpButton.png")

ButtonUpdate()

# Main Game Loop

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if button == "left":
                    ButtonUpdate()
                else:
                    lives -= 1
            if event.key == pygame.K_DOWN:
                if button == "down":
                    ButtonUpdate()
                else:
                    lives -= 1
            if event.key == pygame.K_UP:
                if button == "up":
                    ButtonUpdate()
                else:
                    lives -= 1
            if event.key == pygame.K_RIGHT:
                if button == "right":
                    ButtonUpdate()
                else:
                    lives -= 1



    lifeText = font.render('Lives: ' + str(lives), True, "WHITE")


    screen.blit(Background,(0,0))
    screen.blit(ButtonIMG, (width/2-25, height/2-25))
    screen.blit(lifeText, (5,5))

    if lives <= 0:
        screen.blit(gameOverText,(300,300))
        lives = 0

    pygame.display.update()
    clock.tick(100)

. I seem to be having an issue where a function I defined just simply does not run. Here is the code:

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
SLyCe
  • 33
  • 3
  • You have to use the [`global` statement](https://docs.python.org/3/reference/simple_stmts.html?highlight=global#grammar-token-global-stmt) if you want change the value of a variable in global namespace within a function. (add `global button, ButtonIMG` at the beginning of the `ButtonUpdate` function) – Rabbid76 Aug 20 '21 at 20:57

0 Answers0