TableOfContents

pygame.mouse

pygame中处理鼠标的模块

鼠标函数可以用来获取鼠标设备的当前状态。这些函数也可以用来改变系统鼠标指针。

当显示模式设置后,事件队列就会开始收到鼠标事件。当按下和释放鼠标的按键时,会产生pygame.MOUSEBUTTONDOWN和pygame.MOUSEBUTTONUP事件。这些事件包含一个button属性表示哪些键被按下。滚动鼠标滚轮会产生pygame.MOUSEBUTTONDOWN事件。当向上滚动滚轮时button的值是4,向下滚动时button的值是5。当移动鼠标时,会产生pygame.MOUSEMOTION事件。鼠标移动事件会被分解成一组较小的精确的移动事件。当鼠标移动时,会有很多事件被放在队列中。鼠标移动事件没有被正确的清除,常常是事件队列被填满的主要原因。

如果鼠标指针被隐藏,输入被当前的display捕获,鼠标会进入虚拟输入模式,这种模式下鼠标的相对位移不会收到屏幕边界的影响。参看pygame.mouse.set_visible和pygame.event.set_grab函数来实现这样的模式。

1. pygame.mouse.get_pressed

获取鼠标按键的状态

pygame.moouse.get_pressed(): return (button1, button2, button3)

返回一个布尔值的列表,包含所有鼠标按键的状态。True表示在函数调用时鼠标按键被按下了。

注意,要获取所有鼠标事件,最好使用pygame.event.wait()或者pygame.event.get()函数并检查所有的MOUSEBUTTONDOWN、MOUSEBUTTONUP、MOUSEMOTION事件。

注意,在X11上某些XServer使用中键模拟功能,当你同时按下按键1和按键3时,会发生一个按键2事件。

注意,记得在调用这个函数前调用pygame.event.get。否则他不会工作。

2. pygame.mouse.get_pos

获取鼠标指针的位置

pygame.mouse.get_pos(): return (x, y)

返回x和y表示鼠标指针的位置。这个位置是相对于display的左上角的位置。指针位置可以在display窗口的外面,但是总是在屏幕内部。

3. pygame.mouse.get_rel

获得鼠标指针的位移

pygame.mouse.get_rel(): return (x, y)

返回前一次调用这次函数到这次调用鼠标移动的x和y值。鼠标指针的相对位移被限制在屏幕边缘的内部,但是虚拟输入鼠标模式可以突破这个限制。

4. pygame.mouse.set_pos

设置鼠标指针的位置

pygame.mouse.set_pos([x, y]): return None

设置当前鼠标位置为给定的值。如果鼠标指针是可见的,它会跳到新的坐标位置。移动鼠标会产生新的pygame.MOUSEMOTION事件。

5. pygame.mouse.set_visible

隐藏或者显示鼠标指针

pygame.mouse.set_visible(bool): return bool

如果参数是True,则鼠标指针会显示出来。这个函数会返回以前的鼠标指针可见状态。

6. pygame.mouse.get_focused

检查display是否取得了鼠标输入焦点

pygame.mouse.get_focused(): return bool

如果pygame在接受鼠标输入(或者用窗口系统的说法,是激活的或者是获得焦点的)时则返回True。

这个函数对于在窗口中工作非常有用。相比之下,在全屏模式下,这个函数总是返回True。

注意,在MS Windows中,具有鼠标输入焦点的窗口同样有键盘输入焦点。但在X-Window中,可以一个窗口接受鼠标输入事件而同时另一个窗口接受键盘事件。pygame.mouse.get_focused判断pygame的窗口是否接受鼠标输入事件。

7. pygame.mouse.set_cursor

设置系统鼠标指针的图像

pygame.mouse.set_cursor(size, hotspot, xormasks, andmasks): return None

当鼠标指针可见时,它会显示为给定的位域数组指定的黑白色的位图。size是包含指针高度和宽度的列表。hotspot是表示指针的热点位置的列表。xormasks是序列包含指针异或数据位域的序列,andmask是一组包含指针位域数据的序列。

宽度必须是8的倍数,位域数组必须是size给定的正确的大小。否则会抛出一个异常。

参看pygame.cursor模块查看更多创建默认和自定义系统指针的信息。

8. pygame.mouse.get_cursor

获取系统鼠标指针的图像

pygame.mouse.get_cursor(): return (size, hotspot, xormasks, andmasks)

获得有关鼠标系统指针的信息。返回值和传给pygame.mouse.set_cursor的参数一样。

pygame.cursors

1. pygame.cursors.compile

2. pygame.cursors.load_xbm

pygame.joystick

1. pygame.joystick.init

2. pygame.joystick.quit

3. pygame.joystick.get_init

4. pygame.joystick.get_count

5. pygame.joystick.Joystick

            pygame.joystick.Joystick(id): return Joystick
                  Joystick.init - initialize the Joystick       initialize the Joystick
                  Joystick.quit - uninitialize the Joystick     uninitialize the Joystick
                  Joystick.get_init - check if the Joystick is initialized      check if the Joystick is initialized
                  Joystick.get_id - get the Joystick ID get the Joystick ID
                  Joystick.get_name - get the Joystick system name      get the Joystick system name
                  Joystick.get_numaxes - get the number of axes on a Joystick   get the number of axes on a Joystick
                  Joystick.get_axis - get the current position of an axis       get the current position of an axis
                  Joystick.get_numballs - get the number of trackballs on a Joystick    get the number of trackballs on a Joystick
                  Joystick.get_ball - get the relative position of a trackball  get the relative position of a trackball
                  Joystick.get_numbuttons - get the number of buttons on a Joystick     get the number of buttons on a Joystick
                  Joystick.get_button - get the current button state    get the current button state
                  Joystick.get_numhats - get the number of hat controls on a Joystick   get the number of hat controls on a Joystick
                  Joystick.get_hat - get the position of a joystick hat get the position of a joystick hat

5.1. Joystick.init

5.2. Joystick.quit

5.3. Joystick.get_init

5.4. Joystick.get_id

5.5. Joystick.get_name

5.6. Joystick.get_numaxes

5.7. Joystick.get_axis

5.8. Joystick.get_numballs

5.9. Joystick.get_ball

5.10. Joystick.get_numbuttons

5.11. Joystick.get_button

5.12. Joystick.get_numhats

5.13. Joystick.get_hat

pygame.time

1. pygame.time.get_ticks

2. pygame.time.wait

3. pygame.time.delay

4. pygame.time.set_timer

5. pygame.time.Clock

5.1. Clock.tick

5.2. Clock.tick_busy_loop

5.3. Clock.get_time

5.4. Clock.get_rawtime

5.5. Clock.get_fps

The End

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