为什么我的 pygame 图像无法加载? [英] Why are my pygame images not loading?

查看:98
本文介绍了为什么我的 pygame 图像无法加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上(在 Youtube 上)找到了一个教程,它向我展示了使用 pygame 创建游戏的基础知识.我找到了他所说的那种和他所拥有的图像.他没有说把它们保存到哪里,我想那可能是我的问题.在这里,这是我的程序中的示例以及错误中显示的内容

I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame. i found images of the kind he said and what he had. he didn't say where to save them to, i think that may be my problem. here, this is an example of what i have in my program and what shows up on the error

bif="bg.jpg"
mif="images.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((420,315),0,32)

backround=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(backround, (0,0))

    x,y = pygame.mouse.get_pos()
    x -= mouse_c.get_width()/2
    y -+ mouse_c.get_height()/2

    screen.blit(mouse_c, (x,y))

    pygame.display.update()

错误如下:

Traceback (most recent call last):  
File "C:/Python26/pygame.games/pygame
1", line 10, in <module>
    backround=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg

请尽快回复.

推荐答案

根据该代码,您的图像文件需要与游戏的 .py 文件保存在同一目录中.尝试将它们移到那里.

Based on that code, your image files need to be saved in the same directory as your game's .py file. Try moving them there.

作为健全性检查(以确保图像会加载,并且提供正确路径实际上是一个问题)您可以在代码中临时指定图像的绝对路径,而不是

As a sanity check (to make sure the images will load and it is in fact an issue with supplying the correct path) you could temporarily specify an absolute path to the image in your code, so instead of

bif = "bg.jpg"

你会

bif = "C:/My_game/images/bg.jpg"

但显然将第二位更改为指向您的图像实际存储的位置.一旦您确定可以使用绝对路径加载图像,您应该使用相对路径,如果您可能有很多资源,您应该将它们与代码分开,例如将它们放在 assets 文件夹与您的游戏代码位于同一文件夹或类似文件夹中.

But obviously change the second bit to point to where your image is actually stored. Once you're sure that the image can be loaded using an absolute path, you should use a relative path, and if you're likely to have many assets you should keep them separate from the code, for example by putting them in an assets folder in the same folder as your game code, or similar.

bif = "assets/bg.jpg"

这篇关于为什么我的 pygame 图像无法加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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