I am trying to draw some lines to make a box in pygame. My issue is I have a UnboundLocalVariable 'start_position' referenced before assignment. I sort of understand why it is happening because it is outside of the function's scope, but when I put it inside the function, it doesn't seem to set the proper coordinates. I believe turning this into a Class may be my best course of action but I was struggling to do that. If anyone has any suggestions on how to resolve this it would be much appreciated.
Here is the full code:
import pygame
#Intialize game
pygame.init()
#Setup screen dimensions and draw
WIDTH, HEIGHT = 1024, 1024
screen = pygame.display.set_mode((WIDTH, HEIGHT))
#Set global X,Y
X = 0
Y = 0
start_position = [X, Y]
position = [X, Y]
move_positive = + 50
move_negative = -50
def move_right():
position[0] = position[0] + move_positive
return position
def move_left():
position[0] = position[0] + move_negative
return position
def move_up():
position[1] = position[1] + move_negative
return position
def move_down():
position[1] = position[1] + move_positive
return position
def draw_square():
#pygame.draw.line(surface, color, start_pos, end_pos, width)
move_right()
pygame.draw.line(screen, 'blue', start_position, position, 1)
start_position = position
move_down()
pygame.draw.line(screen, 'blue', start_position, position, 1)
start_position = position
move_left()
pygame.draw.line(screen, 'blue', start_position, position, 1)
start_position = position
move_up()
pygame.draw.line(screen, 'blue', start_position, position, 1)
start_position = position
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
draw_square()
pygame.display.update()
if __name__ == '__main__':
main()
The issue is within the draw_square function. Thank you for your time.