[Python] Why is Python looking for a local variable? [duplicate]

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am making pong with pygame and Python is reading my variables as if they are in the local scope rather than the global scope. This is the relevant piece of code:

    import pygame
    import sys
    import random
    pygame.init
    #the intial starting code to intialize pygame and the screen, as well as associated objects
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Screen Display Test")
    clock = pygame.time.Clock()
    #defines an objects x position
    obj_x_pos = [615, 320]
    #defines an objects y position
    obj_y_pos = [100, 240]
    #assigns in each point in the x and y lists an object
    obj_ref_list = [pygame.image.load("Sprites/paddle.png"), pygame.image.load("Sprites/ball.png") ]
    print(obj_x_pos)
    #defines the list the holds an objects x and y position for display output
    rend_positions = []

    ball_movex = random.randint(-5, 5)
    ball_movey = random.randint(-5, 5)
    while ball_movex == ball_movey:
    ball_movex = random.randint(-5, 5)
    ball_movey = random.randint(-5, 5)
    #defines the function to declare an object movement as the player
    def player_movement(obj_index, move_speed):
    button_presses = pygame.key.get_pressed()

    if button_presses[pygame.K_s]:
    obj_y_pos[obj_index] += move_speed

    if button_presses[pygame.K_w]:
    obj_y_pos[obj_index] -= move_speed


    #function which draw objects to the screen by taking their reference number, then inputting the corresponding x, y and display information
    def draw_object(ref_number):
    rend_positions = [obj_x_pos[ref_number], obj_y_pos[ref_number]]
    screen.blit(obj_ref_list[ref_number], rend_positions)
    def ball_movement(obj_index):
    obj_x_pos[obj_index] += ball_movex
    obj_y_pos[obj_index] += ball_movey
    if obj_x_pos[obj_index] <= 0:
    ball_movex = ball_movex * -1
    if obj_x_pos[obj_index] >= 605:
    ball_movex = ball_movex * -1
    if obj_y_pos[obj_index] <= 0:
    ball_movey = ball_movey * -1
    if obj_y_pos[obj_index] >= 470:
    ball_movey = ball_movey * -1

    #runs the game loop
    while True:
    #calls the function to draw an object on screen
    draw_object(0)
    draw_object(1)
    #calls the function that moves the player
    player_movement(0, 2)

    ball_movement(1)
    #allows the game window to close and function without windows problems
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()
    #updates the display every frame
    pygame.display.update()
    screen.fill((0, 0, 0))
    #locks the frame rate to 60 fps
    clock.tick(60)


    this just throws a traceback error stating that in the first line of the function is "contains no value".

    It should just print the number

    The particular error is:

    Traceback (most recent call last):
    File "game.py", line 63, in <module>
    ball_movement(1)
    File "game.py", line 44, in ball_movement
    obj_x_pos[obj_index] += ball_movex
    ^^^^^^^^^^
    UnboundLocalError: cannot access local variable 'ball_movex' where it is not associated with a value

    Continue reading...

Compartilhe esta Página