"""參考文檔: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()