无法在pygame中打开.png文件 [英] Can't open .png file in pygame

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

问题描述

我用powerups制作了一个pong游戏,它可以在windows中运行,但是在Linux中,它给了我错误无法打开paddle.png。

I have made a pong game with powerups and it works in windows, but in linux, it gives me the error "Can't open paddle.png".

同样,它适用于Windows,但不适用于Linux。

Again, it works in Windows, but not Linux.

代码示例: - 代码的一部分,而不是整个

Sample of the code: -- MEANS PART OF THE CODE, NOT WHOLE THING

import pygame, sys, time
from random import *

pygame.init()

disw = 640
dish = 480
black = (  0,  0,  0)
white = (255,255,255)
green = (  0,255,  0)

gameDisplay = pygame.display.set_mode((disw, dish))
pygame.display.set_caption("Super-Pong", "S-P")
Clock = pygame.time.Clock()

playerOne = pygame.image.load("paddle.png")
playerOneX = 50
playerOneY = 180

playerTwo = pygame.image.load("paddle.png")
playerTwoX = 590
playerTwoY = 180

问题:无法在Linux中为python程序打开.png文件。

Problem: Won't open .png files in linux for python program.

尝试:在Windows中运行,在Windows中工作。

Tried: Running in Windows, worked in Windows.

他们也在同一个目录中。

They are in the same directory as well.

我也尝试过制作paddle.jpg和一对夫妇其他格式,没有骰子。

I've also tried making it paddle.jpg and a couple other formats, no dice.

我的错误:

[hunter@localhost ~]$ su -
Password: 
Last login: Sun Nov 30 16:56:52 EST 2014 on pts/0
[root@localhost ~]# python /home/hunter/Documents/realprojects/superpong/Super-Pong.py
Traceback (most recent call last):
  File "/home/hunter/Documents/realprojects/superpong/Super-Pong.py", line 16, in <module>
    playerOne = pygame.image.load("paddle.png")
pygame.error: Couldn't open paddle.png
[root@localhost ~]# 


推荐答案

当你告诉Python库中的函数加载具有相对路径的文件时,例如paddle.png,它将查看当前工作目录(CWD)。如果您启动Python程序而CWD是用户 root 的主目录,Pygame将在用户 root的主目录中查找图像文件除非另有说明。

When you tell a function in a Python library to load a file with a relative path, such as "paddle.png", it'll look in the current working directory (CWD). And if you start a Python program while the CWD is the home directory of user root, Pygame will look for image files in the home directory of user root unless otherwise specified.

要更改Pygame查找图片的位置,您需要执行以下三项操作之一:

To change where Pygame looks for images, you need to do one of three things:


  • 将当前工作目录( cd )更改为包含 paddle.png <的目录/ code>在运行 Super-Pong.py 之前。当您双击Python程序时,Python for Windows会自动执行此操作,这就是它在Windows中工作的原因。

  • 在程序中,将当前工作目录更改为包含<$ c的目录$ c> paddle.png ,这可能是包含该程序的同一目录( Super-Pong.py )。该程序的路径作为 sys.argv [0] 传递,所以试试这个: os.chdir(os.path.dirname(__ file__) )

  • 在程序中,添加文件的路径。同样,假设 paddle.png 位于同一文件夹中,请尝试以下操作: playerOne = pygame.image.load(os.path.join(os。 path.dirname(__ file__),paddle.png))

  • Change the current working directory (cd) to the directory containing paddle.png before running Super-Pong.py. Python for Windows automatically does this when you double-click a Python program, which is why it worked in Windows.
  • In the program, change the current working directory to the directory containing paddle.png, which is likely the same directory containing the program (Super-Pong.py). The path to the program is passed as sys.argv[0], so try this: os.chdir(os.path.dirname(__file__))
  • In the program, add the path to the file. Again, assuming paddle.png is in the same folder, try this: playerOne = pygame.image.load(os.path.join(os.path.dirname(__file__), "paddle.png"))

(为什么 su - 无论如何?游戏不需要以超级用户身份运行。)

(Why su - anyway? A game shouldn't need to run as the superuser.)

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

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