python - Pygame:无法打开文件。同一目录

标签 python pygame pygame-surface

我将 150 张 .tif 图像加载到 glob 模块,但目前无法加载它们。我对编程很陌生,所以我想这是我遗漏的一个愚蠢的错误,但我似乎无法弄清楚。

这是代码:

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)) 每个字母。

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

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) 从您的硬盘加载图像并附加结果 surface到列表:

images = []

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

调用.convert()方法(或.convert_alpha()用于透明图像)以提高blit性能。

关于python - Pygame:无法打开文件。同一目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244198/

相关文章:

python - 在 tastypie-swagger 中指定参数

python - 通过创建模块简化数据库(psycopg2)的使用

python-3.x - Pygame 水物理无法按预期工作

python - Pygame 速度和碰撞问题

python - 似乎无法在 Sprite 上的 pygame 中渲染多行

python - 如何在 Mongoengine 上创建抽象模型?

python - 在 Windows 上使用 python 启动分离的无限进程并将输出通过管道传输到文件中

python - 相互播放音乐和音效 (PyGame)

Python 说 pygames 没有属性 'init()' 但 lib 已正确安装

python - 为什么我的 python 模拟时钟的指针显示错误的时间?