01.基本圖形

"""參考文檔:https://pyglet.readthedocs.io/en/stable/programming_guide/examplegame.html#programming-guide-game"""

圖像資源:resources.py
import pyglet


def center_image(image):
    """默認從左下角繪製並放置全部圖像
    從新設置錨點爲圖像中心位置
    """
    image.anchor_x = image.width//2
    image.anchor_y = image.height//2


# 此處的resource與所在目錄同級
pyglet.resource.path = ['resource']
# 從新索引
pyglet.resource.reindex()
# 加載圖像
player_image = pyglet.resource.image('player.png')
bullet_image = pyglet.resource.image("bullet.png")
asteroid_image = pyglet.resource.image("asteroid.png")
center_image(player_image)
center_image(bullet_image)
center_image(asteroid_image)

隨機生成精靈:load.pyhtml

import random
import math
import pyglet
from . import resources


def asteroids(num_asteroids, play_position):
    """
    隨機位置小行星精靈
    :param num_asteroids: 生成小行星數量
    :param play_position: 玩家的位置
    :return: 返回小行星精靈
    """
    asteroids = []
    for i in range(num_asteroids):
        asteroid_x, asteroid_y = play_position
        while distance((asteroid_x, asteroid_y), play_position) < 100:
            asteroid_x = random.randint(0, 800)
            asteroid_y = random.randint(0, 600)
        new_asteroid = pyglet.sprite.Sprite(img=resources.asteroid_image, x=asteroid_x, y=asteroid_y)
        new_asteroid.rotation = random.randint(0, 360)  # 隨機旋轉
        asteroids.append(new_asteroid)
    return asteroids


def distance(point_1=(0, 0), point_2=(0, 0)):
    """計算兩點間的距離"""
    return math.sqrt((point_1[0] - point_2[0]) ** 2 + (point_1[1] - point_2[1]) ** 2)

 

 

 

主程序:asteroid.pyapp

import pyglet
from let import resources, load

game_window = pyglet.window.Window(800, 600)
score_label = pyglet.text.Label(text="Score: 0", x=10, y=575)
level_label = pyglet.text.Label(text="Version 1: Static Graphics",
                                x=400, y=575, anchor_x='center')
play_ship = pyglet.sprite.Sprite(img=resources.player_image, x=400, y=300)
# 裝載3個小行星
asteroids = load.asteroids(3, play_ship.position)


@game_window.event
def on_draw():
    game_window.clear()
    play_ship.draw()
    for asteroid in asteroids:
        asteroid.draw()
    level_label.draw()
    score_label.draw()


if __name__ == '__main__':
    pyglet.app.run()

 

 

同批次繪圖:

若是存在許多不一樣類型的對象,則手動調用每一個對象的draw()方法可能會變得繁瑣而乏味。若是須要繪製大量對象,效率也很低。pyglet pyglet.graphics.Batch類經過讓您經過單個函數調用繪製全部對象來簡化繪製dom

# 建立新批次
main_batch = pyglet.graphics.Batch()

修改精靈產生函數:ide

def asteroids(num_asteroids, play_position, batch=None):
    """
    隨機位置小行星精靈
    :param num_asteroids: 生成小行星數量
    :param play_position: 玩家的位置
    :param batch 批處理
    :return: 返回小行星精靈
    """
    asteroids = []
    for i in range(num_asteroids):
        asteroid_x, asteroid_y = play_position
        while distance((asteroid_x, asteroid_y), play_position) < 100:
            asteroid_x = random.randint(0, 800)
            asteroid_y = random.randint(0, 600)
        new_asteroid = pyglet.sprite.Sprite(img=resources.asteroid_image, x=asteroid_x, y=asteroid_y, batch=batch)
        new_asteroid.rotation = random.randint(0, 360)  # 隨機旋轉
        asteroids.append(new_asteroid)
    return asteroids

裝載:函數

# 裝載3個小行星
asteroids = load2.asteroids(3, play_ship.position, main_batch)

繪製:ui

@game_window.event
def on_draw():
    game_window.clear()
    play_ship.draw()
    main_batch.draw()
相關文章
相關標籤/搜索