Resource Not Found Exception in pyglet

我怕爱的太早我们不能终老 提交于 2019-12-04 03:54:26

This is what i do to be able to load resources:

pyglet.resource.path = ['C:\\Users\\myname\\Downloads\\temp']
pyglet.resource.reindex()

pic = pyglet.resource.image('1.jpg')

texture = pic.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)

texture.width = 960
texture.height = 720

texture.blit(0, 0, 0)

If the relative path is not working you can try with the absolute path using the os module.

import pyglet
import os

working_dir = os.path.dirname(os.path.realpath(__file__))
pyglet.resource.path = [os.path.join(working_dir,'Images')]
pyglet.resource.reindex()

image = pyglet.resource.image('character.png'))

It's better to use the os.path.join method instead of writing it as a string for a better cross-platform support.

try this

pyglet.image.load('path/to/image.png')

I get a problem like this using pyglet and pyscripter.(the text editor) In order for the file to be found I have to restart the editor before running the program.

This might be a problem with pyscripter however.

It is worth looking into: http://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/programming_guide/resources.html

You should include all the directories into:

 pyglet.resource.path = [...]

Then pass the ONLY the file name when you call:

pyglet.resource.image("ball_color.jpeg")

Try:

import os
import pyglet

working_dir = os.path.dirname(os.path.realpath(__file__))
image_dir = os.path.join(working_dir, 'images')
sound_dir = os.path.join(working_dir, 'sound')

print working_dir
print "Images are in: " + image_dir
print "Sounds are in: " + sound_dir

pyglet.resource.path = [working_dir, image_dir, sound_dir]
pyglet.resource.reindex()

try:
    window = pyglet.window.Window()
    image = pyglet.resource.image("ball_color.jpeg")

    @window.event
    def on_draw():
        window.clear()
        image.blit(0, 0)

    pyglet.app.run()

finally:
    window.close()

The use of try and finally are not necessary, but they are recommended. You might end up with a lot of open windows and pyglet back processes if you don't close the window when there are errors.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!