我遇到的问题是,当精灵在屏幕上绘制时,之前的图像会闪烁。
我需要这个看起来光滑,如果可能的话我还需要图像居中。为此,我考虑采用全屏分辨率并从屏幕分辨率宽度的一半减去图像宽度的一半,以得出将图像放置在x轴上的位置。这个理论有什么问题吗?
import argparse
import random
import os
import pyglet
def update_image(dt):
global count
try:
count
except:
count=0
img = pyglet.image.load(image_paths[count])
sprite.image = img
sprite.scale = get_scale(window, img)
## sprite.x = 0
## sprite.y = 0
if count ==len(image_paths)-1:
count=0
else:
count+=1
window.clear()
def get_image_paths(input_dir='.'):
paths = []
for root, dirs, files in os.walk(input_dir, topdown=True):
for file in sorted(files):
if file.endswith(('jpg', 'png', 'gif')):
path = os.path.abspath(os.path.join(root, file))
paths.append(path)
return paths
def get_scale(window, image):
if image.width > image.height:
scale = float(window.width) / image.width
else:
scale = float(window.height) / image.height
return scale
global window
window = pyglet.window.Window(fullscreen=True)
@window.event
def on_draw():
sprite.draw()
if __name__ == '__main__':
global count
try:
count
except:
count=0
parser = argparse.ArgumentParser()
parser.add_argument('dir', help='directory of images',
nargs='?', default=os.getcwd()+'\\slides')
args = parser.parse_args()
global image_paths
image_paths = get_image_paths(args.dir)
img = pyglet.image.load(image_paths[count])
sprite = pyglet.sprite.Sprite(img)
sprite.scale = get_scale(window, img)
pyglet.clock.schedule_interval(update_image, 2.0)
pyglet.app.run()