"""参考文档:https://github.com/pyglet/pyglet"""
import pyglet
from pyglet.window import key, mouse
# 获得一个窗口
game_window = pyglet.window.Window()
# resource与所在目录同级
pyglet.resource.path = ['resources']
# 重新索引
pyglet.resource.reindex()
# 加载图像
player_image = pyglet.resource.image('player.png')
bullet_image = pyglet.resource.image("bullet.png")
# 标签
score_lable = pyglet.text.Label(text='Score:0', x=10, y=460)
level_lable = pyglet.text.Label(text='My Amazing Game', x=game_window.width / 2, y=game_window.height / 2,
anchor_x='center')
play_ship = pyglet.sprite.Sprite(img=player_image, x=400, y=300)
@game_window.event # 让Window实例知道on_draw()函数是一个事件处理程序,该窗口需要重绘时,就会触发该事件
def on_draw():
game_window.clear() # 清除屏幕
level_lable.draw()
score_lable.draw()
play_ship.draw()
bullet_image.blit(0, 0) # 在窗口(左下角)的像素坐标0、0处绘制图像
@game_window.event
def on_key_press(symbol, modifiers): # 键盘事件
if symbol == key.A:
print("'A' key")
else:
print('key', symbol)
@game_window.event
def on_mouse_press(x, y, button, modifiers): # 鼠标事件
if button == mouse.LEFT:
print('left mouse')
if __name__ == '__main__':
pyglet.app.run()