1

Variable Definitions : ending_num : it determines if the program should keep you in the gameplay or not(0 is playing and 1 is not playing and game over screen) mov : it moves the texts added to the screen to where they should be home_num : tells the program which screen it should be on

Problem : after the gameover screen apears the window becomes unresponsive and it says Not Responding When it becomes Not Responding And I have tried with the events and stuff and it didn't help.

if pygame.Rect.colliderect(ast1.objrect, jetrect) == True:
ending_num = 1
end_text = font1.render("Game Over : " + str(score), True, "red")
end_textrect = end_text.get_rect()
font2 = pygame.font.Font('freesansbold.ttf', 50)
text2 = font2.render("Home", True, "dark red", "white")
mov[1] = 80
text2rect = text2.get_rect()
text2rect = text2rect.move(mov)
font3 = pygame.font.Font('freesansbold.ttf', 50)
text3 = font3.render("Play", True, "dark red", "white")
mov[1] = 150
text3rect = text3.get_rect()
text3rect = text3rect.move(mov)
o_speed[1] = 0
screen.blit(background, backgroundrect)
screen.blit(end_text, end_textrect)
screen.blit(text2, text2rect)
screen.blit(text3, text3rect)
while True:
    if 10 < mouse[0] < 149 and 80 < mouse[1] < 130 and pygame.mouse.get_pressed()[0]:
        pygame.time.wait(70)
        home_num = 0
        ending_num = 0
        break
    if 10 < mouse[0] < 116 and 150 < mouse[1] < 200 and pygame.mouse.get_pressed()[0]:
        pygame.time.wait(70)
        home_num = 1
        ending_num = 0
        break
    pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

Additionally you have to get the new mouse position in every frame with pygame.mouse.get_pos():

while True:
    pygame.event.pump()
    mouse = pygame.mouse.get_pos()

    # [...]

Alternatively you can use the MOUSEBUTTONDOWN event:

ending_num = 1
while ending_num != 0:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            mouse = event.pos
            if 10 < mouse[0] < 149 and 80 < mouse[1] < 130:
                home_num = 0
                ending_num = 0
            if 10 < mouse[0] < 116 and 150 < mouse[1] < 200 
                home_num = 1
                ending_num = 0
    pygame.display.flip()
    pygame.time.wait(70)

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hey, thankyou that worked perfectly but I am confused on how it works. Would you like to explain it to me? – Sina Aghily May 21 '21 at 22:24
  • @SinaAghily Just read [Pygame mouse clicking detection](https://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection). – Rabbid76 May 22 '21 at 06:27