I have been following a tutorial on how to move and image around, when I run my code, I get this error:
Traceback (most recent call last): File "C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py", line 142, in Menu() File "C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py", line 135, in Menu button("Play", 120, 350, 200, 100, green, bright_green, "Start") File "C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py", line 65, in button In_Game() File "C:\Users\krzys\Desktop\Dawid Grubba\Private\Game creating\Hero Jump\Hero Jump.py", line 110, in In_Game y += y_change UnboundLocalError: local variable 'y_change' referenced before assignment
I have replaced the y with x but it still displayed the same massage but with x. This is the website: https://pythonprogramming.net/pygame-tutorial-moving-images-key-input/
This is the main part of my code which I want you to view to answer this question:
def In_Game_Earth_Image(x,y):
gameDisplay.blit(Earth_Image, (x,y))
def Title():
message("Hero Jump")
Character1=pygame.image.load("Normal Character.png")
Character1=pygame.transform.scale(Character1, (100,150))
def Starting_Charcter(x,y):
gameDisplay.blit(Character1, (x,y))
y_change = 0
def things(thingx, thingy, thing_width, thing_height):
gameDisplay.blit(RockImg,[thingx, thingy, 20, 20])
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y +h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x, y, w, h) )
if click[0]==1 and action != None:
if action=="Start":
In_Game()
elif action=="quit":
pygame.quit()
quit()
else:
pygame.draw.rect(gameDisplay, ic,(x, y, w, h) )
smallText=pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect=text_objects(msg, smallText)
textRect.center=( (x+(w/2)), (y+(h/2)))
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
Earth_Image=pygame.image.load("Ground_Image.png")
Earth_Image=pygame.transform.scale(Earth_Image, (900, 600))
def In_Game():
x=50
y=250
In_Game_Earth_Image(0,0)
thing_starty =265
thing_startx= 1200
thing_speed=-7
thing_width=10
thing_height=10
Crashed=False
while not Crashed:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
y_change=-20
if event.type==pygame.KEYUP:
if event.key==pygame.K_SPACE:
y_change=0
y += y_change
In_Game_Earth_Image(0,0)
Starting_Charcter(x, y)
things(thing_startx, thing_starty, thing_width, thing_height)
thing_startx += thing_speed
if thing_startx < -300:
thing_startx = 1000
thing_starty = 265
pygame.display.update()
clock.tick(60)
def Menu():
Menu=True
while Menu:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
Title()
button("Play", 120, 350, 200, 100, green, bright_green, "Start")
button("Quit", 570, 350, 200, 100, bright_red, red, "quit")
pygame.display.update()
clock.tick(15)
gameDisplay.fill(white)
Menu()
pygame.quit()
quit()
I probably made a silly mistake but I ten I am a bit of a noob with pygame. Thank you for all your help.