1

my code:

def shop_button(self):
    click = pygame.mouse.get_pressed()
    if click[0] == 1:
        self.open_shop()  # draws shop
        self.inshop = True
        while self.inshop:
            pygame.time.delay(10)
            mousepos = pygame.mouse.get_pos()
            if click[0] == 1 and b.mouse_on_button(mousepos[0], mousepos[1], b.escapex, b.escapex2, b.escapey, b.escapey2):
                self.inshop = False

whenever you click on the shop button this function is run, however, while self.inshop is inside an if statement like such the game instantly stops responding and crashes. This could be a very simple solution as I am semi-new to pygame and the python language as a whole. However, with that being said I can't seem to find a solution anywhere else.

Gearmo
  • 41
  • 3

1 Answers1

1

The game does not crash, it does not respond, because you have a loop inside the main application loop. The inner loop has no event handling and update of the display.

def shop_button(self):
   # [...]

   while self.inshop:
           pygame.time.delay(10)
           mousepos = pygame.mouse.get_pos()
           # [...]

The position which is returned by pygame.mouse.get_pos() is read from an internal cache. The cache is updated when the events are handled by either pygame.event.pump() or pygame.event.get().
That means if you retrieve the position in a loop, without handling the events, the the position does not reflect the current mouse position, it is continuously the same position, the position at the point when pygame.event.get() called, before the loop.
Furthermore the display Surface won't change, without updating the display, by either pygame.display.flip() or pygame.display.update().

You have a main application loop, use it. The main application loop has to:

Never implemented nested application loops. You have to change the control flow and the design of your applcaition.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174