What I mean is that lists randomly decide to merge and combine values. I’m on Python 3.10.6. The lists are “buttons” (for storing button instances) and “keys” (for storing keys pressed). The issue specifically is in my Pause Menu. These lists stay separate anywhere else, but in the pause menu, the buttons list decides to share values with keys. Here is the entire pause script. The bottom is the part that matters, but I want to send in the whole thing anyway to be more specific.
def Pause():
global menu , paused , running , screen , WinProps , clock , FPS , scrwidth , scrheight , keys , event , buttons , frame , audio
WinProps[2].set_alpha(162.5)
font = pygame.font.Font(resource_path(r'AssetsWindowPixelifySans-VariableFont_wght.ttf') , round(64 * scale))
text = font.render('Pause (Esc to Exit)' , True , (255 , 255 , 255) , (27 , 27 , 27))
textSurf = text.get_rect()
textSurf.center = (scrwidth / 2 , 50 * scale)
screen.blit(pygame.transform.scale(WinProps[2] , (scrwidth , scrheight)) , (0 , 0))
screen.blit(text , textSurf)
if not menu:
buttons.append(Button(pygame.image.load(resource_path(r'AssetsWindowButtonsmenu.png')) , (300 , 300) , 'Menu'))
while paused:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
paused = False
elif event.type == pygame.VIDEORESIZE:
scrwidth , scrheight = event.size
WindowSize(scrheight)
elif event.type == pygame.KEYDOWN:
HandleKey('ESCAPE' , 'KEYDOWN')
elif event.type == pygame.KEYUP:
HandleKey('ESCAPE' , 'KEYUP')
DisplayButtons()
print(buttons , keys)
for button in buttons:
if 'ESCAPE' in keys or button.is_pressed():
pygame.mixer.unpause()
paused = False
if button.is_pressed():
menu = True
pygame.mixer.stop()
audio[0].play()
frame = 1
pygame.display.flip()
From the print() block, you would expect it to return button instances and keys preessed, but instead they merge into one list. Here is the output:
[<Assets.button.Button object at 0x000002151DCC1B10>] [<Assets.button.Button object at 0x000002151DCC1B10>]
Also, I made a system in DisplayButtons() to eradicate non-Button instances (int , str , etc.). I decided for debug purposes to include a print() function to print the non-Button instances. Here is what it finds (the output):
ESCAPE
If it matters, here is the DisplayButtons() function.
def DisplayButtons():
global buttons , screen , scale , nmp
itemDel = []
for button in buttons:
if not isinstance(button , Button):
itemDel.append(button)
continue
w , h = (button.sprite.get_width() , button.sprite.get_height())
screen.blit(pygame.transform.scale(button.sprite , (w * scale , h * scale)) , ((button.pos)[0] * scale , (button.pos)[1] * scale))
for item in itemDel:
print(item)
buttons.remove(item)
del itemDel
I’ve had this issue before and had to entirely rewrite snippets of code. I have done this without ever knowing why this happens. Here I have no idea how to rewrite buttons and keys, as those are main components of the game. In the game loop (gameplay) it works as normal (output):
[<Assets.button.Button object at 0x000002112315BAF0>] [‘d’, ‘w’]
The Main Menu (not pause) doesn’t matter since no keyboard use is required. This only happens whenever the game is paused. I can luckily develop my game still, but it’ll be annoying if I accidently pause (which doesn’t really happen). I have no idea what is wrong with the code, I need help.
MonkeyFeet10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.