一直想要个Mac电脑上的HUD,未果,发现pyglet可以等效实现。
一、安装
pip install pyglet
二、Writing a pyglet application(编写一个pyglet应用程序)
Getting started with a new library or framework can be daunting, especially when presented with a large amount of reference material to read. This chapter gives a very quick introduction to pyglet without going into too much detail.
开始使用新的库或框架可能会让人望而生畏,特别是当面对大量的参考资料阅读。本章给出了一个快速介绍pyglet没有去太多的细节。
We’ll begin with the requisite “Hello, World” introduction. This program will open a window with some text in it and wait to be closed. You can find the entire program in the examples/programming_guide/hello_world.py file.
我们将开始与必要的“Hello, World”的介绍。这个程序将打开一个窗口有一些文本和等待被关闭。你可以找到整个程序的例子/ programming_guide / hello_world。py文件。
Hello, world的完整例子,作者亲测,挺好的。
import pyglet
window = pyglet.window.Window()
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
@window.event
def on_draw():
window.clear()
label.draw()
pyglet.app.run()
下面细说。
Begin by importing the pyglet
package:
首先导入pyglet包:
import pyglet
Create a pyglet.window.Window
by calling its default constructor. The window will be visible as soon as it’s created, and will have reasonable default values for all its parameters:
创建一个pyglet.window。窗口通过调用默认构造函数。窗口可见一旦创建,并将合理的所有参数的默认值:
window = pyglet.window.Window()
To display the text, we’ll create a Label
. Keyword arguments are used to set the font, position and anchorage of the label:
显示文本,我们将创建一个标签。关键字参数用于设置字体、位置和锚地的标签:
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
An on_draw()
event is dispatched to the window to give it a chance to redraw its contents. pyglet provides several ways to attach event handlers to objects; a simple way is to use a decorator:
一个on_draw()事件是派去给它一个机会的窗口重绘的内容。pyglet提供了几种方法来将事件处理程序附加到对象;一个简单的方法是使用一个修饰符:
@window.event
def on_draw():
window.clear()
label.draw()
Within the on_draw()
handler the window is cleared to the default background color (black), and the label is drawn.
在on_draw()处理程序窗口允许默认背景颜色(黑色),和标签。
Finally, call:
最后,调用:
pyglet.app.run()
This will enter pyglet’s default event loop, and let pyglet respond to application events such as the mouse and keyboard. Your event handlers will now be called as required, and the run() method will return only when all application windows have been closed.
这将进入pyglet的默认事件循环,让pyglet响应应用程序事件,比如鼠标和键盘。现在,您的事件处理程序将被称为是必需的,和run()方法将返回只有当所有应用程序窗口已经关闭。
If you are coming from another library, you may be used to writing your own event loop. This is possible to do with pyglet as well, but it is generally discouraged; see The application event loop for details.
如果你是来自另一个库,你可以用来编写自己的事件循环。与pyglet这是可能的,但它通常是气馁;有关详细信息,请参阅应用程序事件循环。
Image viewer
Most games and applications will need to load and display images on the screen. In this example we’ll load an image from the application’s directory and display it within the window:
大多数游戏和应用程序将需要加载和显示图像在屏幕上。在这个例子中,我们将从应用程序的加载图像目录并将其显示在窗口:
import pyglet
window = pyglet.window.Window()
image = pyglet.resource.image('kitten.png')
@window.event
def on_draw():
window.clear()
image.blit(0, 0)
pyglet.app.run()
作者亲测,还不错~
We used the image()
function of pyglet.resource
to load the image, which automatically locates the file relative to the source file (rather than the working directory). To load an image not bundled with the application (for example, specified on the command line, you would use pyglet.image.load()
).
我们使用pyglet的图像()函数。资源加载图像,自动定位文件相对于源文件(而不是工作目录)。加载图像不是捆绑与应用程序(例如,在命令行上指定,您将使用pyglet.image.load ())。
The blit()
method draws the image. The arguments (0, 0)
tell pyglet to draw the image at pixel coordinates 0, 0 in the window (the lower-left corner).
位块传输()方法将图像。的参数(0,0)告诉pyglet画图像像素坐标0,0在窗口(左下角)。
The complete code for this example is located in examples/programming_guide/image_viewer.py.
这个示例的完整代码位于/ programming_guide / image_viewer.py例子。
Handling mouse and keyboard events(处理鼠标和键盘事件)
So far the only event used is the on_draw()
event. To react to keyboard and mouse events, it’s necessary to write and attach event handlers for these events as well:
到目前为止唯一的事件使用on_draw()事件。键盘和鼠标事件作出反应,有必要为这些事件编写和附加事件处理程序:
import pyglet
window = pyglet.window.Window()
@window.event
def on_key_press(symbol, modifiers):
print('A key was pressed')
@window.event
def on_draw():
window.clear()
pyglet.app.run()
Keyboard events have two parameters: the virtual key symbol that was pressed, and a bitwise combination of any modifiers that are present (for example, the CTRL and SHIFT keys).
键盘事件有两个参数:虚拟键按符号,和任何修饰符的位组合(例如,CTRL和SHIFT键)。
The key symbols are defined in pyglet.window.key
:
在pyglet.window.key关键符号定义:
from pyglet.window import key
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
print('The "A" key was pressed.')
elif symbol == key.LEFT:
print('The left arrow key was pressed.')
elif symbol == key.ENTER:
print('The enter key was pressed.')
See the pyglet.window.key
documentation for a complete list of key symbols.
看到pyglet.window。关键符号的完整列表的关键文档。
Mouse events are handled in a similar way:
以类似的方式处理鼠标事件:
from pyglet.window import mouse
@window.event
def on_mouse_press(x, y, button, modifiers):
if button == mouse.LEFT:
print('The left mouse button was pressed.')
The x and y parameters give the position of the mouse when the button was pressed, relative to the lower-left corner of the window.
给的x和y参数的位置鼠标按钮被按下时,相对于窗口的左下角。
There are more than 20 event types that you can handle on a window. An easy way to find the event names and parameters you need is to add the following line to your program:
有20多个事件类型,您可以处理在一个窗口。一个简单的方法来找到你所需要的事件名称和参数是向程序添加以下行:
window.push_handlers(pyglet.window.event.WindowEventLogger())
This will cause all events received on the window to be printed to the console.
这将导致所有接收到的事件窗口打印到控制台。
An example program using keyboard and mouse events is in examples/programming_guide/events.py
一个示例程序使用键盘和鼠标事件/ programming_guide / events.py例子
Playing sounds and music(播放声音和音乐)
pyglet很容易一起播放混合多种声音。下面的例子中扮演一个MP3文件[1]:
import pyglet
music = pyglet.resource.media('music.mp3')
music.play()
pyglet.app.run()
As with the image loading example presented earlier, media() locates the sound file in the application’s directory (not the working directory). If you know the actual filesystem path (either relative or absolute), use pyglet.media.load().
与之前提供的示例图像加载,媒体()定位声音文件在应用程序的目录(不是工作目录)。如果你知道实际的文件系统路径(相对或绝对),使用pyglet.media.load ()。
By default, audio is streamed when playing. This works well for longer music tracks. Short sounds, such as a gunfire shot used in a game, should instead be fully decoded in memory before they are used. This allows them to play more immediately and incur less of a CPU performance penalty. It also allows playing the same sound repeatedly without reloading it. Specify streaming=False in this case:
默认情况下,音频流的时候玩。这适用于较长的音乐。短的声音,比如炮火射击游戏中使用,应该完全在内存中解码之前使用。这使他们更直接的和产生更少的CPU性能损失。它还允许重复玩相同的声音,而不用重新加载它。指定流= False在这种情况下:
sound = pyglet.resource.media('shot.wav', streaming=False)
sound.play()
The examples/media_player.py example demonstrates playback of streaming audio and video using pyglet. The examples/noisy/noisy.py example demonstrates playing many short audio samples simultaneously, as in a game.
例子/ media_player。py示例演示了使用pyglet播放流媒体音频和视频。例子/吵了吵了。py示例演示了玩许多短音频样本同时在一个游戏。
MP3 and other compressed audio formats require AVbin to be installed (this is the default for the Windows and Mac OS X installers). Uncompressed WAV files can be played without AVbin.
MP3压缩音频格式和其他需要安装AVbin(这是默认的Windows和Mac OS X安装程序)。没有AVbin未压缩的WAV文件可以播放。
Where to next?(接下来去哪里?)
The examples above have shown you how to display something on the screen, and perform a few basic tasks. You’re probably left with a lot of questions about these examples, but don’t worry. The remainder of this programming guide goes into greater technical detail on many of pyglet’s features. If you’re an experienced developer, you can probably dive right into the sections that interest you.
上面的示例显示你如何显示在屏幕上,并执行一些基本的任务。你可能会留下很多问题关于这些例子,但别担心。本编程指南的其余部分进入更大的技术细节pyglet的许多特性。如果你是一个有经验的开发人员,您可以直接到你感兴趣的部分。
For new users, it might be daunting to read through everything all at once. If you feel overwhelmed, we recommend browsing through the beginnings of each chapter, and then having a look at a more in-depth example project. You can find an example of a 2D game in the In-depth game example section.
对于新用户来说,它可能是令人畏惧的阅读一切。如果你觉得不知所措,我们建议浏览每一章的开始,然后有一个看一个更深入的示例项目。你可以找到一个例子的2 d游戏(深入游戏例子)部分。
To write advanced 3D applications or achieve optimal performance in your 2D applications, you’ll need to work with OpenGL directly. If you only want to work with OpenGL primitives, but want something slightly higher-level, have a look at the Graphics module.
写先进的3 d应用程序或在2 d应用程序中实现最优性能,您需要直接使用OpenGL。如果你只想使用OpenGL原语,但希望稍微高级的东西,看一看[图形]模块。
There are numerous examples of pyglet applications in the examples/
directory of the documentation and source distributions. If you get stuck, or have any questions, join us on the mailing list!
有许多pyglet应用的例子的例子/目录的文档和源代码发行版。如果你卡住,或者有任何问题,加入我们(邮件列表)!
网友评论