Pygame:无法打开文件.同一目录 [英] Pygame: Couldn't open file. Same directory

查看:121
本文介绍了Pygame:无法打开文件.同一目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将150个.tif图像加载到了一个glob模块中,目前无法加载它们.我是编程新手,所以我想这是我所缺少的愚蠢错误,但我似乎无法弄清楚.

I loaded 150 .tif images to a glob module and am currently unable to load them. I'm very new to programming so I imagine it's a dumb mistake I'm missing but I cant seem to figure it out.

这是代码:

import pygame, glob

types= ('*.tif')

artfile_names= []

for files in types:
    artfile_names.extend(glob.glob(files))

print(artfile_names)

for artworks in artfile_names:
    pygame.image.load(str(artfile_names))

谢谢您的帮助!

推荐答案

错误是您的types变量只是一个字符串(将其括在括号中没有任何作用),因此您遍历字符串中的字母并为每个字母呼叫artfile_names.extend(glob.glob(files)).

The mistake is that your types variable is just a string (wrapping it in parentheses has no effect), so you iterate over the letters in the string and call artfile_names.extend(glob.glob(files)) for each letter.

逗号组成元组(空元组除外):

The comma makes the tuple (except for the empty tuple):

types = '*.tif',  # This gives you a tuple with the length 1.

types = '*.tif', '*.png'  # This is a tuple with two elements.


在代码的第二部分中,您需要遍历artfile_names,调用pygame.image.load(artwork)以从硬盘加载图像并附加生成的


In the second part of your code, you need to iterate over the artfile_names, call pygame.image.load(artwork) to load the image from your hard disk and append the resulting surface to a list:

images = []

for artwork in artfile_names:
    images.append(pygame.image.load(artwork).convert())

调用.convert()方法(对于具有透明性的图像,请调用.convert_alpha())以提高blit性能.

Call the .convert() method (or .convert_alpha() for images with transparency) to improve the blit performance.

这篇关于Pygame:无法打开文件.同一目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆