I am new to python and am building a little 2d shoot out game. I have a function "attack()" that takes 3 arguments.
I'm trying to pass in a string variable argument, and two integer variables, and return all three to the main program for further use.
This is the code for the function and it's implementation code inside the main loop.
fire_state = "ready"
def attack(w,x,y):
w ='fire'
screen.blit(attackimg, (x+25,y+30))
return w,x,y
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if fire_state == "ready":
print(fire_state)
attackX = playerX
attack(w=fire_state,x=attackX,y=attackY)
If I hard code the string variable as a global variable inside the function it works, but severely limits the functions usability.
fire_state2 = "ready"
def attack2(x,y):
global fire_state2
fire_state2 ='fire'
screen.blit(attackimg, (x+25,y+30))
if event.key == pygame.K_w:
if fire_state2 == "ready":
attack2X = player2X
attack2(attack2X, attack2Y)
If someone could explain why the fire_state variable isn't updateing unless it's defined as global inside the function, but the integer variables work just fine when passed in as arguments.