I'm making a menu in Pygame for a game I'm making. I made a scene system that controls what things are displayed on the screen. Clicking buttons changes the scene, and also key variables in the game such as options, game settings, etc. My problem is that when I click a button, the click seems to overflow into the other screen, accidentally clicking other buttons and skipping the said scene.
Game Loop (some code left out):
while run:
clock.tick(60)
#basic program stuff
event_list = pygame.event.get()
for event in event_list :
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if scene != "Menu":
scene = "Map"
infoToggle = False
screen.fill("red")
if scene == "Menu":
titleText = utils.text("Tile Wars", 750, 50 , reallylargefont, (0,0,0), screen)
if start_button.draw():
scene = "ChooseNation"
if scene == "ChooseNation":
if lutosia_button.draw():
nationplayed = "Lutosia"
scene = "Map"
if muonetta_button.draw():
nationplayed = "Muonetta"
scene = "Map"
if scene != "Map":
infoToggle = False
#important functions
if scene == "Map":
world.draw()
draw_grid()
#buttons
if gov_button.draw():
print("government")
scene = "Gov"
if tech_button.draw():
print("technology")
scene = "Tech"
if dip_button.draw() and phase == "Diplomacy":
print('DIPLOMACY!!!')
scene = "Dip"
#click handling
action = world.isClicked(event_list)
mapClicked = action[0]
if mapClicked == True and scene == "Map":
infoToggle = True
terrainType = action[1]
tileNumber = action[5]
ownerName = owner_data[action[3]][action[4]]
expansion = gamfuncs.expand(action[2], action[3], action[4], map_data, action[1], nationplayed)
if expansion[0] == True and phase == "Expansion":
expansionRow = expansion[1]
expansionCol = expansion[2]
map_data[expansionRow][expansionCol] = expansion[3]
owner_data[expansionRow][expansionCol] = expansion[4]
ownerName = owner_data[action[3]][action[4]]
world.update(map_data)
phase = "Diplomacy"
#final stuff in the loop
mapClicked = False
pygame.display.update()
pygame.quit()
Button Code:
pygame.init()
class Button():
def __init__(self,x,y,image,screen,scale) -> None:
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width*scale), int(height*scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.screen = screen
self.clicked = False
def draw(self):
action = False
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
self.screen.blit(self.image, (self.rect.x, self.rect.y))
return action
I tried to remedy this by adding artificial time between the button click detection and the scene change, like this:
if scene == "Menu":
titleText = utils.text("Tile Wars", 750, 50 , reallylargefont, (0,0,0), screen)
if start_button.draw():
scene = "ChooseNation"
pygame.time.wait(100)
if scene == "ChooseNation":
if lutosia_button.draw():
nationplayed = "Lutosia"
pygame.time.wait(100)
scene = "Map"
if muonetta_button.draw():
nationplayed = "Muonetta"
pygame.time.wait(100)
scene = "Map"
I expected it to cause a delay in the click handling so it wouldn't skip the scene, but it did the same thing, albeit with a small delay. Why is this happening and how can I fix it?