简体   繁体   中英

Not working quitting events in pygame.event

My program does only two things :

1) It's printing word 'hello' every time i hover mouse on one of two images (anyone,in any order)

2) It's closing , when i close window by pressing ESC , or by mouse

import pygame,sys ,os,random,math
from pygame.locals import *

pygame.init()

white=(255,255,255)
black=(0,0,0)
WINDOWWIDTH = 1680 
WINDOWHEIGHT =1050
mousex, mousey=0,0

screen=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))

ticket=pygame.image.load(os.path.join('imag','ticket.png'))
ticket2=pygame.image.load(os.path.join('imag','ticket2.png'))

def terminate():
    pygame.quit()
    sys.exit()
def checkForQuit():
    for event in pygame.event.get(QUIT):  # get all the QUIT events
        terminate()                       # terminate if any QUIT events are present
    for event in pygame.event.get(KEYUP): # get all the KEYUP events
        if event.key == K_ESCAPE:
            terminate()                   # terminate if the KEYUP event was for the ESc key
        pygame.event.post(event)

tic1=ticket.get_rect(topleft=(50,770))
tic2=ticket2.get_rect(topleft=(220,770))

List_of_tickets=[tic1,tic2]

while 1:
    screen.fill(white)
    checkForQuit()          
    screen.blit(ticket,(50,770))
    screen.blit(ticket2,(220,770))

    for tic in List_of_tickets:
        if tic.collidepoint(pygame.mouse.get_pos()):
            print 'Hello !'


    pygame.display.update()

The problem is that second part is not working !

So if i hover mouse on one ticket - everything fine . I got my 'Hello' word and i can close window if i need to . But if i hover mouse on one ticket , and THEN on second ticket (so i got many more of my 'Hello' - and that's good) - i cannot quit program nor with ESC , nor with mouse.

It seems like quitting event ceased to be handled.

The questions are :

1) What i do not understand in handling of events in Pygame ?

2) What should i do to make my code quittin in any case ?

From the docs: "If you are only taking specific events from the queue, be aware that the queue could eventually fill up with the events you are not interested."

Toss pygame.event.clear() in there and see what happens.

(If so, the reason it only fails if you go to both tickets is that all mouse movements add an event; moving to one ticket doesn't completely fill the queue, but moving to both does. Slightly wiggling the mouse would have the same effect.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM