版本4和5间的区别
于2006-10-24 09:14:14修订的的版本4
大小: 23286
编辑: czk
备注:
于2006-10-24 09:37:40修订的的版本5
大小: 22791
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 43: 行号 43:
            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.
             
内部的pygame事件处理函数
{{{
pygame.event.pump(): return None
}}}
对于游戏的每一帧,你都需要以某种方式调用事件队列。这保证你的程序内部可以和操作系统其它部分交互。如果你在游戏不使用其它的事件函数,你应该调用pygame.event.pump来让pygame采取内部默认的行为。

如果你的程序一直通过其它pygame.event函数处理队列中的事件,这个函数调用不是必须的。

事件队列内部有一些重要的事情必须要处理。主窗口可能需要重画和响应系统。如果你太长事件没有调用事件队列,系统可能会认为你的程序已经锁死了。
行号 54: 行号 55:
            get events from the queue 从队列获取所有的事件
{{{
行号 58: 行号 60:

            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.
             
}}}
这个函数从队列获取所有的消息,并把它们从队列中删除。如果指定了类型或者类型的列表,则只有指定类型的消息会被从队列中删除。

如果你只从队列获取特定类型的事件,注意队列可能最终会被你没有提取的事件所填满。
行号 65: 行号 66:
            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.
             
从队列提取一个事件
{{{
pygame.event.poll(): return Event
}}}
返回队列中的一个事件。如果队列是空的,会马上返回一个pygame.NOEVENT事件。返回的事件会被从队列中删除。
行号 72: 行号 73:
            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.
             
在队列中等待一个事件
{{{
pygame.event.wait(): return Event
}}}
返回队列中的一个事件。如果队列是空的,这个函数会等待直到有事件发生。如果程序在等待事件,它会在空闲状态休眠。这对于需要和系统其它程序同时运行的程序是很重要的。
行号 79: 行号 80:
            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.
             
查看队列中是否有特定类型的事件
{{{
pygame.event.peek(type): return bool
pygame.event.peek(typelist): return bool
}}}
如果队列中有给定类型的事件在等待,则返回True。如果传过去一个事件类型的列表,则只要列表中任何一个事件类型在队列中存在,函数就返回True。
行号 87: 行号 88:
            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.
             
删除队列中的所有事件
{{{
pygame.event.clear(): return None
pygame.event.clear(type): return None
pygame.event.clear(typelist): return None
}}}
清除队列中的所有事件,或者所有特定类型的事件。这个函数和pygame.event.get除了返回值不同以外,其它效果一样。当要删除整个事件队列的时候,这个函数可能更有效率一些。

TableOfContents

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

内部的pygame事件处理函数

pygame.event.pump(): return None

对于游戏的每一帧,你都需要以某种方式调用事件队列。这保证你的程序内部可以和操作系统其它部分交互。如果你在游戏不使用其它的事件函数,你应该调用pygame.event.pump来让pygame采取内部默认的行为。

如果你的程序一直通过其它pygame.event函数处理队列中的事件,这个函数调用不是必须的。

事件队列内部有一些重要的事情必须要处理。主窗口可能需要重画和响应系统。如果你太长事件没有调用事件队列,系统可能会认为你的程序已经锁死了。

2. pygame.event.get

从队列获取所有的事件

            pygame.event.get(): return Eventlist
            pygame.event.get(type): return Eventlist
            pygame.event.get(typelist): return Eventlist

这个函数从队列获取所有的消息,并把它们从队列中删除。如果指定了类型或者类型的列表,则只有指定类型的消息会被从队列中删除。

如果你只从队列获取特定类型的事件,注意队列可能最终会被你没有提取的事件所填满。

3. pygame.event.poll

从队列提取一个事件

pygame.event.poll(): return Event

返回队列中的一个事件。如果队列是空的,会马上返回一个pygame.NOEVENT事件。返回的事件会被从队列中删除。

4. pygame.event.wait

在队列中等待一个事件

pygame.event.wait(): return Event

返回队列中的一个事件。如果队列是空的,这个函数会等待直到有事件发生。如果程序在等待事件,它会在空闲状态休眠。这对于需要和系统其它程序同时运行的程序是很重要的。

5. pygame.event.peek

查看队列中是否有特定类型的事件

pygame.event.peek(type): return bool
pygame.event.peek(typelist): return bool

如果队列中有给定类型的事件在等待,则返回True。如果传过去一个事件类型的列表,则只要列表中任何一个事件类型在队列中存在,函数就返回True。

6. pygame.event.clear

删除队列中的所有事件

pygame.event.clear(): return None
pygame.event.clear(type): return None
pygame.event.clear(typelist): return None

清除队列中的所有事件,或者所有特定类型的事件。这个函数和pygame.event.get除了返回值不同以外,其它效果一样。当要删除整个事件队列的时候,这个函数可能更有效率一些。

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- which does not actually block any events as the documentation claims. When passed 'None' set_allowed() will in contrast call- which causes all event types to be ignored. So, either docs are wrong, or the source is... November 25, 2005 4:54pm - Anonymous Regarding: "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." While testing interactively, it seems that this is not true of set_blocked(), but is true of set_allowed() when passing None. Maybe the authors got it mixed up when writing the docs?

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
    The mouse functions can be used to get the current state of the mouse device. These functions can also alter the system cursor for the mouse. When the display mode is set, the event queue will start receiving mouse events. The mouse buttons generate pygame.MOUSEBUTTONDOWN and pygame.MOUSEBUTTONUP events when they are pressed and released. These events contain a button attribute representing which button was pressed. The mouse wheel will generate pygame.MOUSEBUTTONDOWN events when rolled. The button will be set to 4 when the wheel is rolled up, and to button 5 when the wheel is rolled down. Anytime the mouse is moved it generates a pygame.MOUSEMOTION event. The mouse movement is broken into small and accurate motion events. As the mouse is moving many motion events will be placed on the queue. Mouse motion events that are not properly cleaned from the event queue are the primary reason the event queue fills up. If the mouse cursor is hidden, and input is grabbed to the current display the mouse will enter a virtual input mode, where the relative movements of the mouse will never be stopped by the borders of the screen. See the functions pygame.mouse.set_visible - hide or show the mouse cursor and pygame.event.set_grab - control the sharing of input devices with other applications to get this configured.

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
    to see if they are MOUSEBUTTONDOWN, MOUSEBUTTONUP, or MOUSEMOTION. Note, that on X11 some XServers use middle button emulation. When you click both buttons 1 and 3 at the same time a 2 button event can be emitted. Note, remember to call pygame.event.get - get events from the queue before this function. Otherwise it will not work.

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 offers control over the system hardware cursor. Pygame only supports black and white cursors for the system. You control the cursor with functions inside pygame.mouse. This cursors module contains functions for loading and unencoding various cursor formats. These allow you to easily store your cursors in external files or directly as encoded python strings. The module includes several standard cursors. The pygame.mouse.set_cursor - set the image for the system mouse cursor function takes several arguments. All those arguments have been stored in a single tuple you can call like this:
    • >>> pygame.mouse.set_cursor(*pygame.cursors.arrow)

    This module also contains a few cursors as formatted strings. You'll need to pass these to pygame.cursors.compile - create binary cursor data from simple strings function before you can use them. The example call would look like this:
    • >>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings) >>> pygame.mouse.set_cursor(*cursor)

    The following variables are cursor bitmaps that can be used as cursor:
    • o pygame.cursors.arrow o pygame.cursors.diamond o pygame.cursors.broken_x o pygame.cursors.tri_left o pygame.cursors.tri_right
    The following strings can be converted into cursor bitmaps with pygame.cursors.compile - create binary cursor data from simple strings :
    • 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 ", " ", " ", " ")
    August 27, 2006 4:58am - Anonymous

    \ZnbNzgytswsc %%%%%%%%%%%%%% _

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

The End

Pygame事件与用户交互 (2008-02-23 15:36:58由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.