在Pygame中加载图像时,出现错误“找不到文件”

2024-03-29 04:48:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习pygame,并且会出错

每当我想在我的窗口中放置背景图像时,我都会得到一个错误No file found。而图像名称是合适的

我正在使用Windows PC

希望这个社区能帮助我,激励我学习更多

这是我的密码

import pygame

pygame.init()

window_width = 400
window_height = 400

size = (window_height, window_width)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("Hello_World")

background_image = pygame.image.load("motivation.png").convert()

while True:
        screen.blit(background_image, [0, 0])

Tags: no图像imagesize错误displaywindowwidth
1条回答
网友
1楼 · 发布于 2024-03-29 04:48:55

映像文件路径必须相对于当前工作目录。工作目录可能与python文件的目录不同

进一步见pygame.image.load('sprite/test_bg.jpg'): pygame.error: Couldn't open sprite/test_bg.jpg


您必须处理应用程序循环中的事件。分别见^{}^{}

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

进一步见PyGame window not responding after a few seconds

相关问题 更多 >