1

I have a ball animation and I want it so that it that runs until you CLOSE the program (A.K.A until pygame.QUIT is activated) And then after pygame.QUIT is activated it will count down from 10 seconds before it closes.

(PS. If you're an admin of this website the other post you send me doesn't help with my specific problem)

import pygame
import random
pygame.init()
window = pygame.display.set_mode([400,400])

c = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
x = random.randint(10,390)
y = random.randint(10,390)
speed_x = random.randint(3,5)
speed_y = random.randint(3,5)
time = 10



#this is definitely wrong so this where I need help on
playing = True 
while playing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            time -= 1 
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            time -= 1
            print(time)
            pygame.time.wait(1000)
            if time == 0:
                playing = False
                        
    x += speed_x
    y += speed_y
    window.fill((white))
    pygame.draw.circle(window,black,(x,y),20)
    if x > 390 or x < 10:
        speed_x = speed_x * -1
    if y > 390 or y < 10:
        speed_y = speed_y * -1
        
    c.tick(60)
    pygame.display.flip()

1 Answers1

1

Instead of exiting the application, start a countdown timer. Quit the application when the countdown is complete.
See Countdown timer in Pygame.

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
counter = 0
text = font.render(str(10), True, (0, 128, 0))

timer_event = pygame.USEREVENT+1

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            counter = 10
            pygame.time.set_timer(timer_event, 1000)
        elif event.type == timer_event:
            counter -= 1
            text = font.render(str(counter), True, (0, 128, 0))
            if counter == 0:
                run = False             

    window.fill((255, 255, 255))
    
    # draw game
    # [...]
    
    if counter > 0:
        text_rect = text.get_rect(center = window.get_rect().center)
        window.blit(text, text_rect)

    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I edited my post with the code, can you help me fix the code? Thank you – thankyoustack Mar 24 '21 at 16:18
  • But my assignment in python class said quotw "A window that counts down from 10 seconds after the close button is hit" And so I thought my question was different – thankyoustack Mar 24 '21 at 16:36
  • I understand I was just saying how I thought my question wasn't a duplicate because my scenario was a little bit different because you said above how my question was a duplicate but nonetheless thank you for your time have a good day – thankyoustack Mar 24 '21 at 16:41