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.py

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.py

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类通过让您通过单个函数调用绘制所有对象来简化绘制

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

修改精灵产生函数:

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)

绘制:

@game_window.event
def on_draw():
    game_window.clear()
    play_ship.draw()
    main_batch.draw()

 

posted @ 2019-10-31 14:46  fly_bk  阅读(428)  评论(0编辑  收藏  举报