Skip to content
Advertisement

How to stop pygame buttons from overlapping in separate images?

I’m making a simple text based RPG game in pygame. I’m using a button class (not mine, but fairly simple to use and integrate) to add buttons with choices in them. However, it seems a button on the second slide, the ‘Sit and Wait’ choice, is overlapping with the ‘No’ button on the first slide. Since the No button closes the game, pressing on Sit and Wait seems to close the game also. I’ve tried ordering the if-else statements in different ways, but every other button seems fine. Any tips? Thank you. Here is my code, sorry I know it’s a lot but I’m a beginner and not great at condensing yet:

JavaScript

Advertisement

Answer

You can use an if-elif instruction like below, so that if the user clicks on the mouse while cursor is over the sit and wait button then you don’t check if it’s also over the quit button (and thus you don’t quit the game):

JavaScript

A cleaner solution would however be to remember which buttons are actually currently displayed. For example, you could have a list of visible buttons and check if the cursor is over a button only if this one is in the list.

On a side note, your running variable is useless: your main loop is while running but as soon as running is set to False you just quit the game immediately. So the condition of your loop never evaluates to False.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement