版本18和19间的区别
于2006-10-24 18:01:21修订的的版本18
大小: 16488
编辑: czk
备注:
于2006-10-24 21:19:55修订的的版本19
大小: 13844
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 4: 行号 4:

      pygame module for interacting with joystick devices


      The joystick module manages the joystick devices on a computer (there can be more than one). Joystick devices include trackballs and video-game-style gamepads, and the module allows the use of multiple buttons and "hats".
       
pygame中与摇杆设备交互的模块

joystick模块管理计算机上的摇杆设备(可能多于一个)。摇杆设备包括轨迹球、类似电视游戏机的游戏手柄,模块允许使用多个按钮和'hats'。
行号 11: 行号 9:
            initialize the joystick module
            pygame.joystick.init(): return None

            This function is called automatically by pygame.init - initialize all imported pygame modules.

            It initializes the joystick module. This will scan the system for all joystick devices. The module must be initialized before any other functions will work.

            It is safe to call this function more than once.
             
初始化joystick模块
{{{
pygame.joystick.init(): return None
}}}
这个函数会被pygame.init自动调用。

它初始化joystick模块。这个函数会扫描系统的所有摇杆设备。在使用这个模块的其他函数之前,必须先进行初始化。

多次调用这个函数是可以的。
行号 22: 行号 20:
            uninitialize the joystick module
            pygame.joystick.quit(): return None

            Uninitialize the joystick module. After you call this any existing joystick objects will no longer work.

            It is safe to call this function more than once.
             
反初始化joystick模块
{{{
pygame.joystick.quit(): return None
}}}
反初始化joystick模块。在调用了这个函数后,任何现有的摇杆对象都不能再使用了。

多次调用这个函数是可以的。
行号 31: 行号 29:
            true if the joystick module is initialized
            pygame.joystick.get_init(): return bool

            Test if the pygame.joystick.init - initialize the joystick module function has been called.
             
如果joystick模块已经被初始化了返回True
{{{
pygame.joystick.get_init(): return bool
}}}
测试pygame.joystick.init是否已经被调用过了。
行号 38: 行号 36:
            number of joysticks on the system
            pygame.joystick.get_count(): return count

            Return the number of joystick devices on the system. The count will be 0 if there are no joysticks on the system.

            When you create Joystick objects using Joystick(id), you pass an integer that must be lower than this count.
             
系统中摇杆的数量
{{{
pygame.joystick.get_count(): return count
}}}
返回系统中摇杆设备的数量。如果系统没有摇杆,数量是0。

当你使用Joystick(id)创建Joystick对象的时候,你需要传一个小于这个数量的整数过去。
行号 47: 行号 45:
            create a new Joystick object 创建一个新的Joystick对象
行号 64: 行号 62:
            Create a new joystick to access a physical device. The id argument must be a value from 0 to pygame.joystick.get_count()-1.

            To access most of the Joystick methods, you'll need to init() the Joystick. This is separate from making sure the joystick module is initialized. When multiple Joysticks objects are created for the same physical joystick device (i.e., they have the same ID number), the state and values for those Joystick objects will be shared.

            The Joystick object allows you to get information about the types of controls on a joystick device. Once the device is initialized the Pygame event queue will start receiving events about its input.

            You can call the Joystick.get_name - get the Joystick system name and Joystick.get_id - get the Joystick ID functions without initializing the Joystick object.
            January 14, 2006 10:59pm - Anonymous

            I have both a four button and two button joystick. (Two for older games).
            What they have in common is that when I pull the joystick in any direction it goes FULL BLAST. I need a joystick which will move SLOWLY with a minimal turn rather than a MAXIMUM turn upon any movement of the joy stick. Is there any way to make the joystick move a little bit with small movements of the stick?
            If I want maximum movement I would pull the stick FULL in that direction!

            Please advise

            garymac@hotpop.com

             
创建一个新的摇杆来访问物理设备。id参数必须是从0到pygame.joystick.get_count()-1之间的值。

要访问Joystick的大部分方法之前,首先要对Joystick调用init()。这个初始化和joystick模块的初始化是分开的。当对同一个摇杆创建多个Joystick对象(即它们都有相同的id)时,这些Joystick对象会共享状态和值。

Joystick对象允许你获取摇杆设备上的控制器的类型信息。一旦设备被初始化后,pygame的事件队列会开始收到关于摇杆的事件。

在没有初始化Joystick对象之前,你可以调用Joystick.get_name和Joystick.get_id。
行号 83: 行号 71:
                  initialize the Joystick
                  Joystick.init(): return None

                  The Joystick must be initialized to get most of the information about the controls. While the Joystick is initialized the Pygame event queue will receive events from the Joystick input.

                  It is safe to call this more than once.
                   
初始化Joystick
{{{
Joystick.init(): return None
}}}

Joystick必须先初始化,以获得关于这个控制器的大部分信息。当Joystick被初始化后,pygame的事件队列会收到Joystick产生的事件。

这个函数被多次调用是可以的。
行号 92: 行号 81:
                  uninitialize the Joystick
                  Joystick.quit(): return None

                  This will unitialize a Joystick. After this the Pygame event queue will no longer receive events from the device.

                  It is safe to call this more than once.
                   
反初始化Joystick
{{{
Joystick.quit(): return None
}}}

这个函数反初始化Joystick。在调用了这个函数后,pygame的事件队列就不会收到这个设备的事件了。

这个函数被多次调用是可以的。
行号 101: 行号 91:
                  check if the Joystick is initialized
                  Joystick.get_init(): return bool

                  Returns True if the init() method has already been called on this Joystick object.
                   
检查这个Joystick是不是被初始化了
{{{
Joystick.get_init(): return bool
}}}
如果这个Joystick对象的init方法已经被调用了,这个函数返回True。
行号 108: 行号 98:
                  get the Joystick ID
                  Joystick.get_id(): return int

                  Returns the integer ID that represents this device. This is the same value that was passed to the Joystick() constructor. This method can safely be called while the Joystick is not initialized.
                   
得到Joystick的ID
{{{
Joystick.get_id(): return int
}}}

返回表示这个设备的整数ID。这和传给Joystick构造函数的值是相同的。这个函数可以在没有初始化的时候被调用。
行号 115: 行号 106:
                  get the Joystick system name
                  Joystick.get_name(): return string

                  Returns the system name for this joystick device. It is unknown what name the system will give to the Joystick, but it should be a unique name that identifies the device. This method can safely be called while the Joystick is not initialized.
                   
获取Joystick的系统名称
{{{
Joystick.get_name(): return string
}}}

返回这个摇杆设备的系统名称。系统给这个Joystick取什么名字是未知的,但是它应该是可以标识这个设备的唯一的名字。这个函数可以在Joystick没有初始化的时候被调用。
行号 122: 行号 114:
                  get the number of axes on a Joystick
                  Joystick.get_numaxes(): return int

                  Returns the number of input axes are on a Joystick. There will usually be two for the position. Controls like rudders and throttles are treated as additional axes.

                  The pygame.JOYAXISMOTION events will be in the range from -1.0 to 1.0. A value of 0.0 means the axis is centered. Gamepad devices will usually be -1, 0, or 1 with no values in between. Older analog joystick axes will not always use the full -1 to 1 range, and the centered value will be some area around 0. Analog joysticks usually have a bit of noise in their axis, which will generate a lot of rapid small motion events.
                   
返回Joystick的轴数
{{{
Joystick.get_numaxes(): return int
}}}
返回Joystick上的轴数。通常有两个用于定位的轴。像方向盘和油门也会被作为轴来看待。

pygame.JOYAXISMOTION事件的值在-1.0和1.0之间。值0.0表示轴在正中间。游戏手柄设备一般只有-1、0、1三个值,而没有中间的值。而老的模拟摇杆并不一定能完全达到-1到1的区间,中间的值也可能是0附近的一个值。模拟手柄通常会有一点噪声,从而会产生很多很小很快的运动事件。
行号 131: 行号 123:
                  get the current position of an axis
                  Joystick.get_axis(axis_number): return float

                  Returns the current position of a joystick axis. The value will range from -1 to 1 with a value of 0 being centered. You may want to take into account some tolerance to handle jitter, and joystick drift may keep the joystick from centering at 0 or using the full range of position values.

                  The axis number must be an integer from zero to get_numaxes()-1.
                   
获得轴上当前的位置。
{{{
Joystick.get_axis(axis_number): return float
}}}
这个函数返回手柄一个轴的当前位置。这个值在-1到1之间,0表示中间。你需要考虑一些偏差来控制抖动的发生,摇杆的漂移可能使摇杆的中间偏离0值,或者不能达到整个取值区域。


axis_number必须在0到get_numaxes()-1之间。
行号 140: 行号 133:
                  get the number of trackballs on a Joystick
                  Joystick.get_numballs(): return int

                  Returns the number of trackball devices on a Joystick. These devices work similar to a mouse but they have no absolute position; they only have relative amounts of movement.

                  The pygame.JOYBALLMOTION event will be sent when the trackball is rolled. It will report the amount of movement on the trackball.
                   
获取摇杆上的轨迹球的数量。
{{{
Joystick.get_numballs(): return int
}}}
返回一个Joystick上的轨迹球的数量。这些设备和鼠标类似,但是没有绝对的位置,而只有相对的位移。

当轨迹球滚动时,pygame.JOYBALLMOTION事件会发生。它会报告轨迹球总共滚动了多少距离。
行号 149: 行号 142:
                  get the relative position of a trackball
                  Joystick.get_ball(ball_number): return x, y

                  Returns the relative movement of a joystick button. The value is a x, y pair holding the relative movement since the last call to get_ball.

                  The ball number must be an integer from zero to get_numballs()-1.
                   
获取轨迹球的相对位置
{{{
Joystick.get_ball(ball_number): return x, y
}}}
返回一个轨迹球的相对位移。返回值是一个x,y值对,来保存上一次调用这个函数后轨迹球的相对位移。

ball_number必须是一个在0到get_numballs()-1之间的整数。
行号 178: 行号 171:
                  get the number of hat controls on a Joystick
                  Joystick.get_numhats(): return int

                  Returns the number of joystick hats on a Joystick. Hat devices are like miniature digital joysticks on a joystick. Each hat has two axes of input.

                  The pygame.JOYHATMOTION event is generated when the hat changes position. The position attribute for the event contains a pair of values that are either -1, 0, or 1. A position of (0, 0) means the hat is centered.
                   
返回摇杆上hat控制器的总数。
{{{
Joystick.get_numhats(): return int
}}}
返回Joystick上hat的总数。hat设备是摇杆上一种微型的数字摇杆。每个hat有两条轴。

当hat的位置改变时,pygame.JOYHATMOTION事件会产生。事件的位置属性包括一对值,它们是-1或者0或者1。位置(0,0)表示hat在正中间。
行号 187: 行号 180:
                  get the position of a joystick hat
                  Joystick.get_hat(hat_number): return x, y

                  Returns the current position of a position hat. The position is given as two values representing the X and Y position for the hat. (0, 0) means centered. A value of -1 means left/down and a value of 1 means right/up: so (-1, 0) means left; (1, 0) means right; (0, 1) means up; (1, 1) means upper-right; etc.

                  This value is digital, i.e., each coordinate can be -1, 0 or 1 but never in-between.

                  The hat number must be between 0 and get_numhats()-1.
返回hat的位置。
{{{
Joystick.get_hat(hat_number): return x, y
}}}
返回hat的当前位置。位置有两个值表示hat的X和Y的位置。(0,0)表示中间。-1表示下或者左,1表示上或者右。因此(-1,0)表示左,(1,0)表示右,(0,1)表示上,(1,1)表示右上,等等。

这些值是数字的,就是说,它们只有-1、0或者1,没有其他中间的值。

hat_number必须在0和get_numhats()-1之间。

TableOfContents

pygame.joystick

pygame中与摇杆设备交互的模块

joystick模块管理计算机上的摇杆设备(可能多于一个)。摇杆设备包括轨迹球、类似电视游戏机的游戏手柄,模块允许使用多个按钮和'hats'。

1. pygame.joystick.init

初始化joystick模块

pygame.joystick.init(): return None

这个函数会被pygame.init自动调用。

它初始化joystick模块。这个函数会扫描系统的所有摇杆设备。在使用这个模块的其他函数之前,必须先进行初始化。

多次调用这个函数是可以的。

2. pygame.joystick.quit

反初始化joystick模块

pygame.joystick.quit(): return None

反初始化joystick模块。在调用了这个函数后,任何现有的摇杆对象都不能再使用了。

多次调用这个函数是可以的。

3. pygame.joystick.get_init

如果joystick模块已经被初始化了返回True

pygame.joystick.get_init(): return bool

测试pygame.joystick.init是否已经被调用过了。

4. pygame.joystick.get_count

系统中摇杆的数量

pygame.joystick.get_count(): return count

返回系统中摇杆设备的数量。如果系统没有摇杆,数量是0。

当你使用Joystick(id)创建Joystick对象的时候,你需要传一个小于这个数量的整数过去。

5. pygame.joystick.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

创建一个新的摇杆来访问物理设备。id参数必须是从0到pygame.joystick.get_count()-1之间的值。

要访问Joystick的大部分方法之前,首先要对Joystick调用init()。这个初始化和joystick模块的初始化是分开的。当对同一个摇杆创建多个Joystick对象(即它们都有相同的id)时,这些Joystick对象会共享状态和值。

Joystick对象允许你获取摇杆设备上的控制器的类型信息。一旦设备被初始化后,pygame的事件队列会开始收到关于摇杆的事件。

在没有初始化Joystick对象之前,你可以调用Joystick.get_name和Joystick.get_id。

5.1. Joystick.init

初始化Joystick

Joystick.init(): return None

Joystick必须先初始化,以获得关于这个控制器的大部分信息。当Joystick被初始化后,pygame的事件队列会收到Joystick产生的事件。

这个函数被多次调用是可以的。

5.2. Joystick.quit

反初始化Joystick

Joystick.quit(): return None

这个函数反初始化Joystick。在调用了这个函数后,pygame的事件队列就不会收到这个设备的事件了。

这个函数被多次调用是可以的。

5.3. Joystick.get_init

检查这个Joystick是不是被初始化了

Joystick.get_init(): return bool

如果这个Joystick对象的init方法已经被调用了,这个函数返回True。

5.4. Joystick.get_id

得到Joystick的ID

Joystick.get_id(): return int

返回表示这个设备的整数ID。这和传给Joystick构造函数的值是相同的。这个函数可以在没有初始化的时候被调用。

5.5. Joystick.get_name

获取Joystick的系统名称

Joystick.get_name(): return string

返回这个摇杆设备的系统名称。系统给这个Joystick取什么名字是未知的,但是它应该是可以标识这个设备的唯一的名字。这个函数可以在Joystick没有初始化的时候被调用。

5.6. Joystick.get_numaxes

返回Joystick的轴数

Joystick.get_numaxes(): return int

返回Joystick上的轴数。通常有两个用于定位的轴。像方向盘和油门也会被作为轴来看待。

pygame.JOYAXISMOTION事件的值在-1.0和1.0之间。值0.0表示轴在正中间。游戏手柄设备一般只有-1、0、1三个值,而没有中间的值。而老的模拟摇杆并不一定能完全达到-1到1的区间,中间的值也可能是0附近的一个值。模拟手柄通常会有一点噪声,从而会产生很多很小很快的运动事件。

5.7. Joystick.get_axis

获得轴上当前的位置。

Joystick.get_axis(axis_number): return float

这个函数返回手柄一个轴的当前位置。这个值在-1到1之间,0表示中间。你需要考虑一些偏差来控制抖动的发生,摇杆的漂移可能使摇杆的中间偏离0值,或者不能达到整个取值区域。

axis_number必须在0到get_numaxes()-1之间。

5.8. Joystick.get_numballs

获取摇杆上的轨迹球的数量。

Joystick.get_numballs(): return int

返回一个Joystick上的轨迹球的数量。这些设备和鼠标类似,但是没有绝对的位置,而只有相对的位移。

当轨迹球滚动时,pygame.JOYBALLMOTION事件会发生。它会报告轨迹球总共滚动了多少距离。

5.9. Joystick.get_ball

获取轨迹球的相对位置

Joystick.get_ball(ball_number): return x, y

返回一个轨迹球的相对位移。返回值是一个x,y值对,来保存上一次调用这个函数后轨迹球的相对位移。

ball_number必须是一个在0到get_numballs()-1之间的整数。

5.10. Joystick.get_numbuttons

  • get the number of buttons on a Joystick Joystick.get_numbuttons(): return int Returns the number of pushable buttons on the joystick. These buttons have a boolean (on or off) state. Buttons generate a pygame.JOYBUTTONDOWN and pygame.JOYBUTTONUP event when they are pressed and released. December 8, 2005 9:12pm - Anonymous There is a way to know the state of a button! Joystick.get_button(n)

5.11. Joystick.get_button

  • get the current button state Joystick.get_button(button): return bool Returns the current state of a joystick button.

5.12. Joystick.get_numhats

返回摇杆上hat控制器的总数。

Joystick.get_numhats(): return int

返回Joystick上hat的总数。hat设备是摇杆上一种微型的数字摇杆。每个hat有两条轴。

当hat的位置改变时,pygame.JOYHATMOTION事件会产生。事件的位置属性包括一对值,它们是-1或者0或者1。位置(0,0)表示hat在正中间。

5.13. Joystick.get_hat

返回hat的位置。

Joystick.get_hat(hat_number): return x, y

返回hat的当前位置。位置有两个值表示hat的X和Y的位置。(0,0)表示中间。-1表示下或者左,1表示上或者右。因此(-1,0)表示左,(1,0)表示右,(0,1)表示上,(1,1)表示右上,等等。

这些值是数字的,就是说,它们只有-1、0或者1,没有其他中间的值。

hat_number必须在0和get_numhats()-1之间。

pygame.time

  • pygame module for monitoring time Times in pygame are represented in milliseconds (1/1000 seconds). Most platforms have a limited time resolution of around 10 milliseconds.

1. pygame.time.get_ticks

  • get the time in milliseconds pygame.time.get_ticks(): return milliseconds Return the number of millisconds since pygame.init - initialize all imported pygame modules was called. Before pygame is initialized this will always be 0.

2. pygame.time.wait

  • pause the program for an amount of time pygame.time.wait(milliseconds): return time Will pause for a given number of milliseconds. This function sleeps the process to share the processor with other programs. A program that waits for even a few milliseconds will consume very little processor time. It is slightly less accurate than the pygame.time.delay - pause the program for an amount of time function. This returns the actual number of milliseconds used.

3. pygame.time.delay

  • pause the program for an amount of time pygame.time.delay(milliseconds): return time Will pause for a given number of milliseconds. This function will use the processor (rather than sleeping) in order to make the delay more accurate than pygame.time.wait - pause the program for an amount of time. This returns the actual number of milliseconds used.

4. pygame.time.set_timer

  • repeatedly create an event on the event queue pygame.time.set_timer(eventid, milliseconds): return None Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed. Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS. To disable the timer for an event, set the milliseconds argument to 0.

5. pygame.time.Clock

  • create an object to help track time pygame.time.Clock(): return Clock
    • Clock.tick - update the clock update the clock Clock.tick_busy_loop - update the clock update the clock Clock.get_time - time used in the previous tick time used in the previous tick Clock.get_rawtime - actual time used in the previous tick actual time used in the previous tick Clock.get_fps - compute the clock framerate compute the clock framerate
    Creates a new Clock object that can be used to track an amount of time. The clock also provides several functions to help control a game's framerate.

5.1. Clock.tick

  • update the clock Clock.tick(framerate=0): return milliseconds control timer events This method should be called once per frame. It will compute how many milliseconds have passed since the previous call. If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second. Note that this function uses SDL_Delay function which is not accurate on every platform, but does not use much cpu. Use tick_busy_loop if you want an accurate timer, and don't mind chewing cpu.

5.2. Clock.tick_busy_loop

  • update the clock Clock.tick_busy_loop(framerate=0): return milliseconds control timer events This method should be called once per frame. It will compute how many milliseconds have passed since the previous call. If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second. Note that this function uses pygame.time.delay, which uses lots of cpu in a busy loop to make sure that timing is more acurate.

5.3. Clock.get_time

  • time used in the previous tick Clock.get_time(): return milliseconds Returns the parameter passed to the last call to Clock.tick - update the clock. It is the number of milliseconds passed between the previous two calls to Pygame.tick().

5.4. Clock.get_rawtime

  • actual time used in the previous tick Clock.get_rawtime(): return milliseconds Similar to Clock.get_time - time used in the previous tick, but this does not include any time used while Clock.tick - update the clock was delaying to limit the framerate.

5.5. Clock.get_fps

  • compute the clock framerate Clock.get_fps(): return float Compute your game's framerate (in frames per second). It is computed by averaging the last few calls to Clock.tick - update the clock.

The End

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

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