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()
标签:基本,01,resource,image,asteroids,asteroid,pyglet,import,图形 来源: https://www.cnblogs.com/fly-book/p/11771124.html