문제

I am a beginner at Python programming (2.7; and Pygame) and I was wondering; how do you create and read files from a sub directory? In other words, I want to take sprite images, data, BGM, etc. from a sub directory named 'Data'. So, for example, if I wanted to use Pygame to open a sprite file in the sub directory 'Data', how would I do that?

What I have so far for the sprite-loading is: char_idle = pygame.image.load("char_idle.png")

도움이 되었습니까?

해결책

Use the os.path.join function to get the path of the file. Add

import os

at the beginning of your code. Use it like this:

char_idle = pygame.image.load(os.path.join("Data", "char_idle.png"))

You could also provide the path as "Data/char_idle.png" (on Linux) or "Data\char_idle.png" (Windows), but it makes yout code less portable, so better use os.path.join.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top