pyglet安装非常简单,用pip就行。
pip install pyglet --user
如果问我为什么后面有“–user”,我也不知道,官网这么写的。
hello world入门往往使用一个“hello world”,比如python
print("hello world")
所以,我这次拿“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()
我存到了我的https://github.com/cjuer512/pyglet-code/tree/main/%E4%B8%80下的1.py
开始分析
第一行:导入pyglet模块
第三行:创建窗口
四至八行:Label函数,用于输出文字:第一个参数为输出文本;font_name为字体;x和y我还没搞清楚(大佬看到请联系我,到时候我会写的);x_anchor和y_anchor是大概位置,x_anchor只能填“left”或“right”或“center”,y_anchor只能填“top”或“bottom”或“center”或“baseline”
第九行:声明“on_draw”为“window”的事件
第十行:定义“on_draw”事件,即显示。这个名字不能改
第十一行:清空“window”窗口
第十二行:显示,没这个上面的“label”显示不了
第十四行:开始运行
与上文的“hello world”相似,将四至八行改为:
image = pyglet.resource.blit(path)
注:path为图片路径,有的图片显示不了,最好用jpg
将第十二行改为:
image.blit(0,0)
注意:缩进
我存到了https://github.com/cjuer512/pyglet-code/tree/main/%E4%B8%80下的2.py
啊对了,我突然想起来一件事。代码中其实有时候可以不用像我写“hello world”那样写“def”什么的,可以直接去写,这个在我的《pyglet教程——3》中我会提到
结束好了,这篇文章就结束了,所有代码在https://github.com/cjuer512/pyglet-code/tree/main/%E4%B8%80。
若有问题或错误,请联系我(qq放在了主页),我会改进。侵权我会改进或删除。如有雷同,纯属巧合。



