I was trying to create a back button in pygame using python but my parameter(condition) is not being set to false, how do I do this? Here is my code:
def back(condition): #condition - any boolean variable in my script
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 5 + 125 > mouse[0] > 5 and 5 + 125 > mouse[1] > 5:
gameDisplay.blit(backBtn, (5, 5))
if click[0] == 1:
if condition:
condition = False
else:
gameDisplay.blit(backBtn_hover, (5, 5))
If you have any sort of answer please answer.
Thank you: :D
Since condition
is of an immutable type, any change to it is only visible inside the function.
You will need to return it and assign it back when calling the function.
def back(condition): #condition - any boolean variable in my script
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 5 + 125 > mouse[0] > 5 and 5 + 125 > mouse[1] > 5:
gameDisplay.blit(backBtn, (5, 5))
if click[0] == 1:
if condition:
condition = False
else:
gameDisplay.blit(backBtn_hover, (5, 5))
return condition
...
condition = back(condition)
It is usually not advisable to make it a global variable so I'm not going to suggest that.
Do you expect this to happen?
condition = True
back(condition)
print(condition) # False
That's not how Python works. You can't change variables passed to functions. Something like that would work.
container = { 'condition': True }
back(container)
print(container['condition']) # False
Because in this case you don't change the variable, but its content. But anyway it's bad code. Normally you would do something like that:
condition = True
condition = back(condition)
print(condition) # False
Of course you need to add a return statement to your function.
Your answer likely is due to scoping of your variables, three examples provided below that hopefully will lead you to a solution:
Situation 1
condition = True
def back(condition):
condition = False
print(f"{condition=}")
def main():
local_condition = back(condition)
global_condition = condition
print(f"{local_condition=}\n{global_condition=}")
if __name__ == '__main__':
main()
Results:
condition=False
local_condition=None
global_condition=True
Situation 2 with added return in back funciton
condition = True
def back(condition):
condition = False
print(f"{condition=}")
return condition
def main():
local_condition = back(condition)
global_condition = condition
print(f"{local_condition=}\n{global_condition=}")
if __name__ == '__main__':
main()
Results 2:
condition=False
local_condition=False
global_condition=True
Situation 3 redefining global
condition = True
def back(condition):
condition = False
print(f"{condition=}")
return condition
def main():
global condition
condition = back(condition)
global_condition = condition
print(f"{condition=}\n{global_condition=}")
if __name__ == '__main__':
main()
Results 3:
condition=False
condition=False
global_condition=False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.