pygame.event.Event().type == pygame.QUIT, confusio

2019-06-22 18:58发布

Consider these lines in the pygame loop:

ev = pygame.event.poll()
ev.type == pygame.QUIT

From: http://openbookproject.net/thinkcs/python/english3e/pygame.html

From what I understand the function pygame.event.poll() creates an instance of the Event class in the event module of the pygame package.

I. Now ev.type is an attribute call(?) but how do I know which values it can have? How can you even tell from the pygame documentation that it has the possibility to equal pygame.QUIT?

II. What exactly is pygame.QUIT? How does it get a value?

III. help('pygame.QUIT') says pygame.QUIT = class int(object). How do you call this construction?

3条回答
The star\"
2楼-- · 2019-06-22 19:08

pygame.QUIT is just a constant int that happens to be defined inside the pygame module.

>>> import pygame
>>> pygame.QUIT
12

This is the relevant page in the documentation: http://www.pygame.org/docs/ref/event.html. You can see all the possible event types (just above the comments).

查看更多
冷血范
3楼-- · 2019-06-22 19:10
ev = pygame.event.poll()

is a call to a function that returns a single event from the event queue (basically, a list of things that have happened that your application might want to know about). It assigns that event (which is an Event object) to the variable ev.

ev.type

gets the value of the type attribute of that Event object, which is a numerical constant.

== pygame.QUIT

checks to see if it's equal the numerical constant defined as pygame.QUIT.

The possible event types are listed at http://www.pygame.org/docs/ref/event.html - I've copy-pasted the list here as well (which also lists the associated attributes passed with each event):

QUIT             none
ACTIVEEVENT      gain, state
KEYDOWN          unicode, key, mod
KEYUP            key, mod
MOUSEMOTION      pos, rel, buttons
MOUSEBUTTONUP    pos, button
MOUSEBUTTONDOWN  pos, button
JOYAXISMOTION    joy, axis, value
JOYBALLMOTION    joy, ball, rel
JOYHATMOTION     joy, hat, value
JOYBUTTONUP      joy, button
JOYBUTTONDOWN    joy, button
VIDEORESIZE      size, w, h
VIDEOEXPOSE      none
USEREVENT        code
查看更多
趁早两清
4楼-- · 2019-06-22 19:17

pygame.QUIT is sent when the user clicks the window's "X" button, or when the system 'asks' for the process to quit. If ignored, it can still be killed by the system. It lets you save, before quitting.

查看更多
登录 后发表回答