简体   繁体   English

pygame不会只加载一张图片

[英]pygame won't load just one image

By using pygame, I am loading up several images. 通过使用pygame,我正在加载多个图像。 They are all very similar, in size, and are all png s, and are all in the same folder. 它们的大小都非常相似,都是png ,并且都在同一文件夹中。 And, for the sake of error searching, they are all loaded in exactly the same way. 并且,为了进行错误搜索,它们都以完全相同的方式加载。 However, one of them won't load, and I have no idea why. 但是,其中一个不会加载,我也不知道为什么。

player_character = PlayerCharacter('resources\player.png', 100, 700, 50, 50)
monster_character = PlayerCharacter('resources\enemy.png', 200, 700, 50, 50)
treasure_sprite = PlayerCharacter('resources\treasure.png', 300, 700, 50, 50)

三个精灵和错误消息

Both the enemy, and player, images load perfectly fine. 敌人和玩家的图像加载都很好。 However, the treasure image won't. 但是,宝藏图像不会。 The error message is odd as well as it says it can't load reasure.png I don't know why it is missing the 't'. 错误消息很奇怪,它说它无法加载reasure.png我不知道为什么它缺少't'。

The \\t of \\treasure is being interpreted as a tab character. \\ treasure的\\ t被解释为制表符。 You can see that the t is missing in the error message which provides the clue. 您可以看到在提供提示的错误消息中没有t。 Put r in front of your strings to tell python that they are raw strings: 将r放在您的字符串前面以告诉python它们是原始字符串:

r’resources\treasure.png’

It's worth doing this for all the images. 值得对所有图像执行此操作。

It is because your path is using a backslash \\ and \\t in python is the special character for a tab, causing it to say tab - reasure. 这是因为您的路径使用反斜杠\\而python中的\\t是制表符的特殊字符,导致其表示制表符-reasure。 To fix this you can use the backslash twice \\ in your path names. 要解决此问题,您可以在路径名中使用两次反斜杠\\。

treasure_sprite = PlayerCharacter('resources\\treasure.png', 300, 700, 50, 50)

The reason your other paths worked is because \\e and \\p don't have any special meaning. 您的其他路径起作用的原因是\\e\\p没有任何特殊含义。

Your code i not working because '\\t' represents a tab ' ' , see: https://www.quora.com/What-does-t-do-in-Python 您的代码无法正常工作,因为'\\t'代表制表符' ' ,请参见: https : //www.quora.com/What-does-t-do-in-Python

so instead do: 因此,请执行以下操作:

r'resources\treasure.png'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM