版本6和7间的区别
于2006-10-24 13:57:57修订的的版本6
大小: 20439
编辑: czk
备注:
于2006-10-24 14:28:58修订的的版本7
大小: 21785
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 2: 行号 2:
= 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类型及其对应的成员。

= pygame.key =
pygame中与键盘相关的模块

这个模块包含处理键盘的函数。

当键盘的按键被按下时,事件队列会得到pygame.KEYDOWN和pygame.KEYUP事件。这两个事件都有一个叫做key的整型属性表示键盘上的键。pygame.KEYDOWN事件还有一个额外的属性unicode,表示键盘输入的转后对应的字符。这个字符会考虑shift键和其它组合键的状态。

有很多键盘相关的常量,他们表示键盘上的键。下面是所有键盘常量的列表:
行号 25: 行号 12:
          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
            KeyASCII ASCII Common Name
            K_BACKSPACE \b backspace
            K_TAB \t tab
            K_CLEAR clear
            K_RETURN \r return
            K_PAUSE pause
            K_ESCAPE ^[ escape
            K_SPACE space
            K_EXCLAIM ! exclaim
            K_QUOTEDBL " quotedbl
            K_HASH # hash
            K_DOLLAR $ dollar
            K_AMPERSAND & ampersand
            K_QUOTE quote
            K_LEFTPAREN ( left parenthesis
            K_RIGHTPAREN ) right parenthesis
            K_ASTERISK * asterisk
            K_PLUS + plus sign
            K_COMMA , comma
            K_MINUS - minus sign
            K_PERIOD . period
            K_SLASH / forward slash
            K_0 0 0
            K_1 1 1
            K_2 2 2
            K_3 3 3
            K_4 4 4
            K_5 5 5
            K_6 6 6
            K_7 7 7
            K_8 8 8
            K_9 9 9
            K_COLON : colon
            K_SEMICOLON ; semicolon
            K_LESS < less-than sign
            K_EQUALS = equals sign
            K_GREATER > greater-than sign
            K_QUESTION ? question mark
            K_AT @ at
            K_LEFTBRACKET [ left bracket
            K_BACKSLASH \ backslash
            K_RIGHTBRACKET ] right bracket
            K_CARET ^ caret
            K_UNDERSCORE _ underscore
            K_BACKQUOTE ` grave
            K_a a a
            K_b b b
            K_c c c
            K_d d d
            K_e e e
            K_f f f
            K_g g g
            K_h h h
            K_i i i
            K_j j j
            K_k k k
            K_l l l
            K_m m m
            K_n n n
            K_o o o
            K_p p p
            K_q q q
            K_r r r
            K_s s s
            K_t t t
            K_u u u
            K_v v v
            K_w w w
            K_x x x
            K_y y y
            K_z z z
            K_DELETE delete
            K_KP0 keypad 0
            K_KP1 keypad 1
            K_KP2 keypad 2
            K_KP3 keypad 3
            K_KP4 keypad 4
            K_KP5 keypad 5
            K_KP6 keypad 6
            K_KP7 keypad 7
            K_KP8 keypad 8
            K_KP9 keypad 9
            K_KP_PERIOD . keypad period
            K_KP_DIVIDE / keypad divide
            K_KP_MULTIPLY * keypad multiply
            K_KP_MINUS - keypad minus
            K_KP_PLUS + keypad plus
            K_KP_ENTER \r keypad enter
            K_KP_EQUALS = keypad equals
            K_UP up arrow
            K_DOWN down arrow
            K_RIGHT right arrow
            K_LEFT left arrow
            K_INSERT insert
            K_HOME home
            K_END end
            K_PAGEUP page up
            K_PAGEDOWN page down
            K_F1 F1
            K_F2 F2
            K_F3 F3
            K_F4 F4
            K_F5 F5
            K_F6 F6
            K_F7 F7
            K_F8 F8
            K_F9 F9
            K_F10 F10
            K_F11 F11
            K_F12 F12
            K_F13 F13
            K_F14 F14
            K_F15 F15
            K_NUMLOCK numlock
            K_CAPSLOCK capslock
            K_SCROLLOCK scrollock
            K_RSHIFT right shift
            K_LSHIFT left shift
            K_RCTRL right ctrl
            K_LCTRL left ctrl
            K_RALT right alt
            K_LALT left alt
            K_RMETA right meta
            K_LMETA left meta
            K_LSUPER left windows key
            K_RSUPER right windows key
            K_MODE mode shift
            K_HELP help
            K_PRINT print screen
            K_SYSREQ sysrq
            K_BREAK break
            K_MENU menu
            K_POWER power
            K_EURO euro
}}}
键盘还有一组修饰键的状态,可以通过位或把它们组合起来:
{{{
            KMOD_NONE, KMOD_LSHIFT, KMOD_RSHIFT, KMOD_SHIFT, KMOD_CAPS,
            KMOD_LCTRL, KMOD_RCTRL, KMOD_CTRL, KMOD_LALT, KMOD_RALT,
            KMOD_ALT, KMOD_LMETA, KMOD_RMETA, KMOD_META, KMOD_NUM, KMOD_MODE
行号 42: 行号 154:
== pygame.event.pump ==
内部的pygame事件处理函数
== pygame.key.get_focused ==
如果display取得了系统的键盘输入焦点,返回True。
行号 45: 行号 157:
pygame.event.pump(): return None pygame.key.get_focused(): return bool
行号 47: 行号 159:
对于游戏的每一帧,你都需要以某种方式调用事件队列。这保证你的程序内部可以和操作系统其它部分交互。如果你在游戏不使用其它的事件函数,你应该调用pygame.event.pump来让pygame采取内部默认的行为。

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

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


== pygame.event.get ==
从队列获取所有的事件
当键盘从系统获得输入焦点时,这个函数返回True。如果display要保证它不失去输入焦点,则可以使用pygame.event.set_grab函数来捕获所有的输入。

== pygame.key.get_pressed ==
取得所有键盘按键的状态
行号 57: 行号 164:
            pygame.event.get(): return Eventlist
            pygame.event.get(type): return Eventlist
            pygame.event.get(typelist): return Eventlist
pygame.key.get_pressed(): return bools
行号 61: 行号 166:
这个函数从队列获取所有的消息,并把它们从队列中删除。如果指定了类型或者类型的列表,则只有指定类型的消息会被从队列中删除。

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

== pygame.event.poll ==
从队列提取一个事件
返回一组布尔值,表示键盘上每个按键的状态。使用按键常量来索引这个数组。如果值为True,表示这个键被按下了。

            Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed - get the state of all keyboard buttons. There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.
            December 25, 2005 7:36pm - Anonymous

              pygame.event.pump()
              m = pygame.key.get_mods()
              if m & KMOD_SHIFT:
                print 'shift pressed'

            December 7, 2005 4:09am - Anonymous

            When I tryed to use this, he couldn't find the key K_t I wanted
             untill I used:

            from pygame.locals import *

            So be sure to use it - Shefy



            December 1, 2005 10:30am - Anonymous

            Before calling pygame.key.get_pressed(), one should call pygame.event.pump() to get the lates state of the keyboard.

            This is so because the get_pressed() function wraps the SDL_GetKeyState() function and in the SDL_GetKeyState() documentation it is written that one should use SDL_PumpEvents() to update the state array and pygame.event.pump() just happens to be a wrapper for SDL_PumpEvents() :-)

             

== pygame.key.get_mods ==
            determine which modifier keys are being held
            pygame.key.get_mods(): return int

            Returns a single integer representing a bitmask of all the modifier keys being held. Using bitwise operators you can test if specific shift keys are pressed, the state of the capslock button, and more.
            February 23, 2006 12:14pm - Anonymous

            DSFDSFDKJKFJEFJDJFKLSD

             

== pygame.key.set_mods ==
            temporarily set which modifier keys are pressed
            pygame.key.set_mods(int): return None

            Create a bitmask of the modifier constants you want to impose on your program.
            October 17, 2006 9:00am - Anonymous

            sexwiev

             

== pygame.key.set_repeat ==
            control how held keys are repeated
            pygame.key.set_repeat(): return None
            pygame.key.set_repeat(delay, interval): return None

            When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number if milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled.

            When pygame is initialized the key repeat is disabled.
             

== pygame.key.name ==
获取一个键的名字
行号 68: 行号 230:
pygame.event.poll(): return Event pygame.key.name(key): return string
行号 70: 行号 232:
返回队列中的一个事件。如果队列是空的,会马上返回一个pygame.NOEVENT事件。返回的事件会被从队列中删除。

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

== pygame.event.peek ==
查看队列中是否有特定类型的事件
{{{
pygame.event.peek(type): return bool
pygame.event.peek(typelist): return bool
}}}
如果队列中有给定类型的事件在等待,则返回True。如果传过去一个事件类型的列表,则只要列表中任何一个事件类型在队列中存在,函数就返回True。

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

== pygame.event.event_name ==
从事件的类型获得它的名称
{{{
pygame.event.event_name(type): return string
}}}
pygame使用一个整数标志来表示事件的类型。如果你要把这些类型给用户看,最好把它们转换为字符串。这个函数会返回事件类型对应的一个简单名字。字符串是采用WordCap这样的命名风格。

== pygame.event.set_blocked ==
控制哪些事件不可以放在队列中
{{{
pygame.event.set_blocked(type): return None
pygame.event.set_blocked(typelist): return None
pygame.event.set_blocked(None): return None
}}}
指定的事件类型不允许放在事件队列中。默认情况下,队列里可以放入任何类型的事件。多次禁止同一个事件类型也是允许的。

如果参数是None,则任何类型的事件都允许出现在队列中。

== pygame.event.set_allowed ==
控制哪些事件允许出现在队列中
{{{
pygame.event.set_allowed(type): return None
pygame.event.set_allowed(typelist): return None
pygame.event.set_allowed(None): return None
}}}
指定的事件类型允许出现在队列中。默认情况下,任何事件类型都允许出现在队列中。多次允许同一个事件类型也是可以的。

如果参数是None,则任何类型的事件都不允许出现在队列中。

== pygame.event.get_blocked ==
测试某种类型的事件是否被阻止了
{{{
pygame.event.get_blocked(type): return bool
}}}
如果指定的事件类型被阻止了,则返回True。

== pygame.event.set_grab ==
控制与其它程序的设备共享
{{{
pygame.event.set_grab(bool): return None
}}}
当你的程序在窗口环境下运行时,你的键盘、鼠标会和其它拥有焦点的应用程序共享。如果你把事件捕获的属性设置成True,则所有事件都只会到达你的程序。

最好不要总是捕获输入,因为它阻止用户在系统里面做其它事情。

== pygame.event.get_grab ==
测试你的程序是否共享输入设备
{{{
pygame.event.get_grab(): return bool
}}}
当输入设备被这个应用程序捕获时,返回True。使用pygame.event.set_grab来控制这个状态。

== pygame.event.post ==
把一个新的事件放在队列中。
{{{
pygame.event.post(Event): return None
}}}
这个函数在事件队列的末尾放一个新的事件。这个事件会被以后的其它队列函数所得到。

这个函数通常用来在队列中放置pygame.USEREVENT事件。但是它也可以用来放置任何类型的事件,如果使用系统事件类型,你的程序要保证创建的事件有标准的属性并有适当的值。

== pygame.event.Event ==
创建一个事件对象

{{{
pygame.event.Event(type, dict): return Event
pygame.event.Event(type, **attributes): return Event
}}}
创建一个新的指定类型的事件。这个事件创建出来可以带有指定的属性和值。属性可以用一个字典参数指定,也可以用关键字参数来指定。

指定的属性会成为新创建的对象的只读属性。这些是Event对象仅有的属性,它没有方法。

= pygame.key =
从一个键的标识常量获得这个键的描述性名称。

TableOfContents

pygame.key

pygame中与键盘相关的模块

这个模块包含处理键盘的函数。

当键盘的按键被按下时,事件队列会得到pygame.KEYDOWN和pygame.KEYUP事件。这两个事件都有一个叫做key的整型属性表示键盘上的键。pygame.KEYDOWN事件还有一个额外的属性unicode,表示键盘输入的转后对应的字符。这个字符会考虑shift键和其它组合键的状态。

有很多键盘相关的常量,他们表示键盘上的键。下面是所有键盘常量的列表:

            KeyASCII      ASCII   Common Name
            K_BACKSPACE   \b      backspace
            K_TAB         \t      tab
            K_CLEAR               clear
            K_RETURN      \r      return
            K_PAUSE               pause
            K_ESCAPE      ^[      escape
            K_SPACE               space
            K_EXCLAIM     !       exclaim
            K_QUOTEDBL    "       quotedbl
            K_HASH        #       hash
            K_DOLLAR      $       dollar
            K_AMPERSAND   &       ampersand
            K_QUOTE               quote
            K_LEFTPAREN   (       left parenthesis
            K_RIGHTPAREN  )       right parenthesis
            K_ASTERISK    *       asterisk
            K_PLUS        +       plus sign
            K_COMMA       ,       comma
            K_MINUS       -       minus sign
            K_PERIOD      .       period
            K_SLASH       /       forward slash
            K_0           0       0
            K_1           1       1
            K_2           2       2
            K_3           3       3
            K_4           4       4
            K_5           5       5
            K_6           6       6
            K_7           7       7
            K_8           8       8
            K_9           9       9
            K_COLON       :       colon
            K_SEMICOLON   ;       semicolon
            K_LESS        <       less-than sign
            K_EQUALS      =       equals sign
            K_GREATER     >       greater-than sign
            K_QUESTION    ?       question mark
            K_AT          @       at
            K_LEFTBRACKET [       left bracket
            K_BACKSLASH   \       backslash
            K_RIGHTBRACKET ]      right bracket
            K_CARET       ^       caret
            K_UNDERSCORE  _       underscore
            K_BACKQUOTE   `       grave
            K_a           a       a
            K_b           b       b
            K_c           c       c
            K_d           d       d
            K_e           e       e
            K_f           f       f
            K_g           g       g
            K_h           h       h
            K_i           i       i
            K_j           j       j
            K_k           k       k
            K_l           l       l
            K_m           m       m
            K_n           n       n
            K_o           o       o
            K_p           p       p
            K_q           q       q
            K_r           r       r
            K_s           s       s
            K_t           t       t
            K_u           u       u
            K_v           v       v
            K_w           w       w
            K_x           x       x
            K_y           y       y
            K_z           z       z
            K_DELETE              delete
            K_KP0                 keypad 0
            K_KP1                 keypad 1
            K_KP2                 keypad 2
            K_KP3                 keypad 3
            K_KP4                 keypad 4
            K_KP5                 keypad 5
            K_KP6                 keypad 6
            K_KP7                 keypad 7
            K_KP8                 keypad 8
            K_KP9                 keypad 9
            K_KP_PERIOD   .       keypad period
            K_KP_DIVIDE   /       keypad divide
            K_KP_MULTIPLY *       keypad multiply
            K_KP_MINUS    -       keypad minus
            K_KP_PLUS     +       keypad plus
            K_KP_ENTER    \r      keypad enter
            K_KP_EQUALS   =       keypad equals
            K_UP                  up arrow
            K_DOWN                down arrow
            K_RIGHT               right arrow
            K_LEFT                left arrow
            K_INSERT              insert
            K_HOME                home
            K_END                 end
            K_PAGEUP              page up
            K_PAGEDOWN            page down
            K_F1                  F1
            K_F2                  F2
            K_F3                  F3
            K_F4                  F4
            K_F5                  F5
            K_F6                  F6
            K_F7                  F7
            K_F8                  F8
            K_F9                  F9
            K_F10                 F10
            K_F11                 F11
            K_F12                 F12
            K_F13                 F13
            K_F14                 F14
            K_F15                 F15
            K_NUMLOCK             numlock
            K_CAPSLOCK            capslock
            K_SCROLLOCK           scrollock
            K_RSHIFT              right shift
            K_LSHIFT              left shift
            K_RCTRL               right ctrl
            K_LCTRL               left ctrl
            K_RALT                right alt
            K_LALT                left alt
            K_RMETA               right meta
            K_LMETA               left meta
            K_LSUPER              left windows key
            K_RSUPER              right windows key
            K_MODE                mode shift
            K_HELP                help
            K_PRINT               print screen
            K_SYSREQ              sysrq
            K_BREAK               break
            K_MENU                menu
            K_POWER               power
            K_EURO                euro

键盘还有一组修饰键的状态,可以通过位或把它们组合起来:

            KMOD_NONE, KMOD_LSHIFT, KMOD_RSHIFT, KMOD_SHIFT, KMOD_CAPS,
            KMOD_LCTRL, KMOD_RCTRL, KMOD_CTRL, KMOD_LALT, KMOD_RALT,
            KMOD_ALT, KMOD_LMETA, KMOD_RMETA, KMOD_META, KMOD_NUM, KMOD_MODE

1. pygame.key.get_focused

如果display取得了系统的键盘输入焦点,返回True。

pygame.key.get_focused(): return bool

当键盘从系统获得输入焦点时,这个函数返回True。如果display要保证它不失去输入焦点,则可以使用pygame.event.set_grab函数来捕获所有的输入。

2. pygame.key.get_pressed

取得所有键盘按键的状态

pygame.key.get_pressed(): return bools

返回一组布尔值,表示键盘上每个按键的状态。使用按键常量来索引这个数组。如果值为True,表示这个键被按下了。

  • Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed - get the state of all keyboard buttons. There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality. December 25, 2005 7:36pm - Anonymous
    • pygame.event.pump() m = pygame.key.get_mods()

      if m & KMOD_SHIFT:

      • print 'shift pressed'
    December 7, 2005 4:09am - Anonymous When I tryed to use this, he couldn't find the key K_t I wanted
    • untill I used:
    from pygame.locals import * So be sure to use it - Shefy December 1, 2005 10:30am - Anonymous Before calling pygame.key.get_pressed(), one should call pygame.event.pump() to get the lates state of the keyboard.

    This is so because the get_pressed() function wraps the SDL_GetKeyState() function and in the SDL_GetKeyState() documentation it is written that one should use SDL_PumpEvents() to update the state array and pygame.event.pump() just happens to be a wrapper for SDL_PumpEvents() :-)

3. pygame.key.get_mods

  • determine which modifier keys are being held pygame.key.get_mods(): return int Returns a single integer representing a bitmask of all the modifier keys being held. Using bitwise operators you can test if specific shift keys are pressed, the state of the capslock button, and more. February 23, 2006 12:14pm - Anonymous DSFDSFDKJKFJEFJDJFKLSD

4. pygame.key.set_mods

  • temporarily set which modifier keys are pressed pygame.key.set_mods(int): return None Create a bitmask of the modifier constants you want to impose on your program. October 17, 2006 9:00am - Anonymous sexwiev

5. pygame.key.set_repeat

  • control how held keys are repeated pygame.key.set_repeat(): return None pygame.key.set_repeat(delay, interval): return None When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number if milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled. When pygame is initialized the key repeat is disabled.

6. pygame.key.name

获取一个键的名字

pygame.key.name(key): return string

从一个键的标识常量获得这个键的描述性名称。

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.