pygame.event
pygame中用来与事件和队列进行交互的模块
Pygame通过一个事件队列来处理所有的事件消息。这个模块里面函数可以帮助你管理那个事件队列。输入队列很大程度的依赖于pygame的display模块。如果display没有初始化、视频模式还没有设置,则事件队列不能够工作。
队列是一个Event对象的普通队列,有各种不同的访问队列中的事件的方法。从简单的检查是否存在事件,到直接抓取所有的事件。
所有事件都有一个类型标志。这个事件类型的值在NOEVENT和NUMEVENTS之间。所有用户定义的事件类型的值可以是USEREVENT或者更大的值。强烈建议你的事件类型标志遵循这个系统习惯。
要获取各种输入设备的状态,你可以忽略事件队列,而通过适当的模块(比如mouse、key、joystick)直接访问输入设备。如果你使用这种方法,记住pygame需要和系统的窗口管理器及系统其它部分有某种形式的通讯。要保持pygame和系统的同步,你需要调用pygame.event.pump。通常在每次游戏循环中都要调用一次这个函数。
事件队列提供了简单的过滤的方法。使用pygame.event.set_allowed和pygame.event.set_blocked过滤事件,可以阻止特定类型的事件进入队列而帮助提高性能。默认情况下所有的事件都是允许的。
摇杆在初始化之前,不会发送任何事件。
一个Event对象包含一个事件类型,以及一组只读的数据成员。Event对象没有成员函数,只有数据。Event对象是从pygame的事件队列中获得的。你可以通过pygame.event.Event创建自己的事件。
你的程序必须采取一些必要的步骤防止队列溢出。如果程序没有定期清除队列或者把所有事件从队列中取走,它就可能会溢出。当队列溢出,会抛出一个异常。
所有的Event对象在Event.type中保存事件的类型标志。你也可以从Event.dict方法访问所有的Event的数据成员。所有其它的成员查找都是传给Event的字典值进行的。
当调试和试验的时候,你可以打印Event对象来显示它的类型和成员。系统里传来的事件,会根据类型保证有一组特定的成员。这里是一组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
1. pygame.event.pump
- internally process pygame event handlers pygame.event.pump(): return None 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. If you are not using other event functions in your game, you should call pygame.event.pump - internally process pygame event handlers to allow pygame to handle internal actions. This function is not necessary if your program is consistently processing events on the queue through the other pygame.event functions. There are important things that must be dealt with internally in the event queue. The main window may need to be repainted or respond to the system. If you fail to make a call to the event queue for too long, the system may decide your program has locked up.
2. pygame.event.get
- get events from the queue pygame.event.get(): return Eventlist pygame.event.get(type): return Eventlist pygame.event.get(typelist): return Eventlist This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue. 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.
3. pygame.event.poll
- get a single event from the queue pygame.event.poll(): return Event Returns a single event from the queue. If the event queue is empty an event of type pygame.NOEVENT will be returned immediately. The returned event is removed from the queue.
4. pygame.event.wait
- wait for a single event from the queue pygame.event.wait(): return Event Returns a single event from the queue. If the queue is empty this function will wait until one is created. While the program is waiting it will sleep in an idle state. This is important for programs that want to share the system with other applications.
5. pygame.event.peek
- test if event types are waiting on the queue pygame.event.peek(type): return bool pygame.event.peek(typelist): return bool Returns true if there are any events of the given type waiting on the queue. If a sequence of event types is passed, this will return True if any of those events are on the queue.
6. pygame.event.clear
- remove all events from the queue pygame.event.clear(): return None pygame.event.clear(type): return None pygame.event.clear(typelist): return None Remove all events or events of a specific type from the queue. This has the same effect as pygame.event.get - get events from the queue except nothing is returned. This can be slightly more effecient when clearing a full event queue.
7. pygame.event.event_name
- get the string name from and event id pygame.event.event_name(type): return string
Pygame uses integer ids to represent the event types. If you want to report these types to the user they should be converted to strings. This will return a the simple name for an event type. The string is in the WordCap style.
8. pygame.event.set_blocked
- control which events are allowed on the queue
- pygame.event.set_blocked(type): return None pygame.event.set_blocked(typelist): return None pygame.event.set_blocked(None): return None The given event types are not allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to disable an event type multiple times. If None is passed as the argument, this has the opposite effect and none of the event types are allowed to be placed on the queue. August 29, 2006 10:56pm - Anonymous The last line of the documentation above is incorrect. It should read: "If None is passed as the argument, then ALL of the event types are allowed to be placed on the queue." November 25, 2005 5:12pm - Anonymous I just confirmed by checking the source of event.c: When passed 'None' set_blocked() will call-
SDL_EventState((Uint8)0, SDL_IGNORE)
SDL_EventState((Uint8)0xFF, SDL_IGNORE)
- pygame.event.set_blocked(type): return None pygame.event.set_blocked(typelist): return None pygame.event.set_blocked(None): return None The given event types are not allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to disable an event type multiple times. If None is passed as the argument, this has the opposite effect and none of the event types are allowed to be placed on the queue. August 29, 2006 10:56pm - Anonymous The last line of the documentation above is incorrect. It should read: "If None is passed as the argument, then ALL of the event types are allowed to be placed on the queue." November 25, 2005 5:12pm - Anonymous I just confirmed by checking the source of event.c: When passed 'None' set_blocked() will call-
9. pygame.event.set_allowed
- control which events are allowed on the queue pygame.event.set_allowed(type): return None pygame.event.set_allowed(typelist): return None pygame.event.set_allowed(None): return None The given event types are allowed to appear on the event queue. By default all events can be placed on the queue. It is safe to enable an event type multiple times. If None is passed as the argument, this has the opposite effect and all of the event types are allowed to be placed on the queue. August 29, 2006 10:36pm - Anonymous The documentation above is incorrect. It should read "If None is passed as the argument, NONE of the event types are allowed to be placed on the queue." November 25, 2005 4:55pm - Anonymous See the comment above regarding passing 'None' to set_blocked.
10. pygame.event.get_blocked
- test if a type of event is blocked from the queue pygame.event.get_blocked(type): return bool Returns true if the given event type is blocked from the queue.
11. pygame.event.set_grab
- control the sharing of input devices with other applications pygame.event.set_grab(bool): return None When your program runs in a windowed environment, it will share the mouse and keyboard devices with other applications that have focus. If your program sets the event grab to True, it will lock all input into your program. It is best to not always grab the input, since it prevents the user from doing other things on their system.
12. pygame.event.get_grab
- test if the program is sharing input devices pygame.event.get_grab(): return bool Returns true when the input events are grabbed for this application. Use pygame.event.set_grab - control the sharing of input devices with other applications to control this state.
13. pygame.event.post
- place a new event on the queue pygame.event.post(Event): return None This places a new event at the end of the event queue. These Events will later be retrieved from the other queue functions. This is usually used for placing pygame.USEREVENT events on the queue. Although any type of event can be placed, if using the sytem event types your program should be sure to create the standard attributes with appropriate values.
14. pygame.event.Event
- create a new event object pygame.event.Event(type, dict): return Event pygame.event.Event(type, **attributes): return Event Creates a new event with the given type. The event is created with the given attributes and values. The attributes can come from a dictionary argument, or as string keys from a dictionary. The given attributes will be readonly attributes on the new event object itself. These are the only attributes on the Event object, there are no methods attached to Event objects.
pygame.key
pygame.mouse
- pygame module to work with the mouse
- pygame.mouse.get_pressed - get the state of the mouse buttons get the state of the mouse buttons pygame.mouse.get_pos - get the mouse cursor position get the mouse cursor position pygame.mouse.get_rel - get the amount of mouse movement get the amount of mouse movement pygame.mouse.set_pos - set the mouse cursor position set the mouse cursor position pygame.mouse.set_visible - hide or show the mouse cursor hide or show the mouse cursor pygame.mouse.get_focused - check if the display is receiving mouse input check if the display is receiving mouse input pygame.mouse.set_cursor - set the image for the system mouse cursor set the image for the system mouse cursor pygame.mouse.get_cursor - get the image for the system mouse cursor get the image for the system mouse cursor
1. pygame.mouse.get_pressed
- get the state of the mouse buttons pygame.moouse.get_pressed(): return (button1, button2, button3) Returns a sequence of booleans representing the state of all the mouse buttons. A true value means the mouse is currently being pressed at the time of the call. Note, to get all of the mouse events it is better to use either
- pygame.event.wait() or pygame.event.get() and check all of those events
2. pygame.mouse.get_pos
- get the mouse cursor position pygame.mouse.get_pos(): return (x, y) Returns the X and Y position of the mouse cursor. The position is relative the the top-left corner of the display. The cursor position can be located outside of the display window, but is always constrained to the screen.
3. pygame.mouse.get_rel
- get the amount of mouse movement pygame.mouse.get_rel(): return (x, y) Returns the amount of movement in X and Y since the previous call to this function. The relative movement of the mouse cursor is constrained to the edges of the screen, but see the virtual input mouse mode for a way around this. Virtual input mode is described at the top of the page.
4. pygame.mouse.set_pos
- set the mouse cursor position pygame.mouse.set_pos([x, y]): return None Set the current mouse position to arguments given. If the mouse cursor is visible it will jump to the new coordinates. Moving the mouse will generate a new pygaqme.MOUSEMOTION event.
5. pygame.mouse.set_visible
- hide or show the mouse cursor pygame.mouse.set_visible(bool): return bool If the bool argument is true, the mouse cursor will be visible. This will return the previous visible state of the cursor.
6. pygame.mouse.get_focused
- check if the display is receiving mouse input pygame.mouse.get_focused(): return bool Returns true when pygame is receiving mouse input events (or, in windowing terminology, is "active" or has the "focus"). This method is most useful when working in a window. By contrast, in full-screen mode, this method always returns true. Note: under MS Windows, the window that has the mouse focus also has the keyboard focus. But under X-Windows, one window can receive mouse events and another receive keyboard events. pygame.mouse.get_focused - check if the display is receiving mouse input indicates whether the pygame window receives mouse events.
7. pygame.mouse.set_cursor
- set the image for the system mouse cursor pygame.mouse.set_cursor(size, hotspot, xormasks, andmasks): return None When the mouse cursor is visible, it will be displayed as a black and white bitmap using the given bitmask arrays. The size is a sequence containing the cursor width and height. Hotspot is a sequence containing the cursor hotspot position. xormasks is a sequence of bytes containing the cursor xor data masks. Lastly is andmasks, a sequence of bytes containting the cursor bitmask data. Width must be a multiple of 8, and the mask arrays must be the correct size for the given width and height. Otherwise an exception is raised. See the pygame.cursor module for help creating default and custom masks for the system cursor.
8. pygame.mouse.get_cursor
- get the image for the system mouse cursor pygame.mouse.get_cursor(): return (size, hotspot, xormasks, andmasks) Get the information about the mouse system cursor. The return value is the same data as the arguments passed into pygame.mouse.set_cursor - set the image for the system mouse cursor.
pygame.cursors
- pygame module for cursor resources
- pygame.cursors.compile - create binary cursor data from simple strings create binary cursor data from simple strings pygame.cursors.load_xbm - load cursor data from an xbm file load cursor data from an xbm file
>>> pygame.mouse.set_cursor(*pygame.cursors.arrow)
>>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings) >>> pygame.mouse.set_cursor(*cursor)
- o pygame.cursors.arrow o pygame.cursors.diamond o pygame.cursors.broken_x o pygame.cursors.tri_left o pygame.cursors.tri_right
- o pygame.cursors.thickarrow_strings o pygame.cursors.sizer_x_strings o pygame.cursors.sizer_y_strings o pygame.cursors.sizer_xy_strings
1. pygame.cursors.compile
- create binary cursor data from simple strings pygame.cursor.compile(strings, black='X', white='.', xor='o'): return data, mask A sequence of strings can be used to create binary cursor data for the system cursor. The return values are the same format needed by pygame.mouse.set_cursor - set the image for the system mouse cursor. If you are creating your own cursor strings, you can use any value represent the black and white pixels. Some system allow you to set a special toggle color for the system color, this is also called the xor color. If the system does not support xor cursors, that color will simply be black. The width of the strings must all be equal and be divisible by 8. An example set of cursor strings looks like this
- thickarrow_strings = ( #sized 24x24
- "XX ", "XXX ", "XXXX ", "XX.XX ", "XX..XX ", "XX...XX ", "XX....XX ", "XX.....XX ", "XX......XX ", "XX.......XX ", "XX........XX ", "XX........XXX ", "XX......XXXXX ", "XX.XXX..XX ", "XXXX XX..XX ", "XX XX..XX ", " XX..XX ", " XX..XX ", " XX..XX ", " XXXX ", " XX ", " ", " ", " ")
\ZnbNzgytswsc %%%%%%%%%%%%%% _
- thickarrow_strings = ( #sized 24x24
2. pygame.cursors.load_xbm
- load cursor data from an xbm file pygame.cursors.load_xbm(cursorfile, maskfile=None): return cursor_args This loads cursors for a simple subset of XBM files. XBM files are traditionally used to store cursors on unix systems, they are an ascii format used to represent simple images. Sometimes the black and white color values will be split into two separate XBM files. You can pass a second maskfile argument to load the two images into a single cursor. The cursorfile and maskfile arguments can either be filenames or filelike object with the readlines method. The return value cursor_args can be passed directly to the pygame.mouse.set_cursor - set the image for the system mouse cursor function.
pygame.joystick
pygame.time