1

I am trying to make a clickable image that exits pygame, but im not sure how to make it close. I have tried using the pygame.quit() and sys.exit() but that loads for a second but doesn't do anyhting. I will show the code I have here(the only relevant code is the x and y variables nad the exit button down the bottom):

import pygame, sys

clock = pygame.time.Clock()

from pygame.locals import *
pygame.init() # inititates Pygame

pygame.display.set_caption('Lightmind')

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # initiates the window

logo = pygame.image.load("GameLogo.png").convert()
logo = pygame.transform.scale(logo, (256, 256))
start_button = pygame.image.load("StartButton.png").convert()
start_button = pygame.transform.scale(start_button, (256, 256))
exit_button = pygame.image.load("ExitButton.png").convert()
exit_button = pygame.transform.scale(exit_button, (256, 100))

x_2 = 560
y_2 = 400

fade_in = True
fade_out = True
fade_in_ball = True
fade_in_start = True
fade_in_exit = True

running = True
while running: # game loop

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            pygame.quit()
            sys.exit()
            
    # fade in the logo
    if fade_in == True:
        for i in range(255):
            screen.fill((0,0,0))
            logo.set_alpha(i)
            screen.blit(logo, (560,260))
            pygame.display.flip()
            clock.tick(60)
            fade_in = False

    # fade out the logo
    if fade_out == True:
        for i in range(255):
            screen.fill((0,0,0))
            logo.set_alpha(255-i)
            screen.blit(logo, (560,260))
            pygame.display.flip()
            clock.tick(60)
            fade_out = False


    # fade in the start button
    if fade_in_start == True:
        for i in range(255):
            start_button.set_alpha(i)
            screen.blit(start_button, (560, 240))
            pygame.display.flip()
            clock.tick(60)
            fade_in_start = False
    
    # fade in the exit button
    if fade_in_exit == True:
        for i in range(255):
            exit_button.set_alpha(i)
            screen.blit(exit_button, (x_2, y_2))
            pygame.display.flip()
            clock.tick(60)
            fade_in_exit = False
            # make exit button exit game
            if event.type == pygame.MOUSEBUTTONDOWN:
                x_2, y_2 = event.pos
                if exit_button.get_rect().collidepoint(x_2, y_2):
                    pygame.quit()
                    sys.exit()

    

    pygame.display.update()

Any help is appreciated!

  • If "the only relevant code is the x and y variables nad the exit button down the bottom" then please reduce your code to a [Minimum, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) containing only that. – Jan Wilamowski Sep 02 '21 at 03:49

2 Answers2

0

You're checking the event outside of your event loop. Move it up instead:

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x_2, y_2 = event.pos
            if exit_button.get_rect().collidepoint(x_2, y_2):
                pygame.quit()
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
  • It still just brings up the windows loading symbol then does nothing. It doesn't even let me click it again. – TowerOfGray11 Sep 02 '21 at 04:06
  • Try to set `running = False` as well – Jan Wilamowski Sep 02 '21 at 04:07
  • @TowerOfGray11 There are several other issues with your code, like your repeated use of clock.tick(), the usage of `x_2` and `y_2` before they're defined, and the interruption of the main loop by the fading in. Try to simplify your example down to only what's necessary to reproduce the problem. – Jan Wilamowski Sep 02 '21 at 04:15
0

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
You must handle the click detection in the event loop.

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position on the display with the blit function.

You've to set the location of the rectangle, either by a keyword argument, e.g:

running = True
while running: # game loop

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            extit_button_rect = exit_button.get_rect(topleft = (x_2, y_2))
            if extit_button_rect.collidepoint(event.pos):
                running = False

    # [...]

pygame.quit()
sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174