找不到图像,当图像就在那里时



大家好,我正在制作一个像素赛跑风格的游戏,当我尝试使用我一直使用的方法加载随机图像时,会出现错误:

追踪(最近一次通话(:文件";c: \Users\name\Documents\Python\PixelRunnerMain\PixelRunner.py";,第22行,inpng1=pygame.image.load("1.png"(FileNotFoundError:在工作目录"C:\Users\chizl\Documents"中找不到文件"1.png"。

该文件与图像位于完全相同的目录中!请帮忙。(另外,我需要帮助,不要只是来纠正我的问题。(

import pygame, random
#initialise pygame
pygame.init()
#window values
win = pygame.display.set_mode((416,256))
pygame.display.set_caption("Pixel Runner")
run = True
#player values
x = 192
y = 144
width = 16
height = 16
vel = 12
#enemy values
e_x = 0
e_y = 0
e_vel = 12
png1 = pygame.image.load("1.png")
png2 = pygame.image.load("2.png")
e_types = [png1, png2]
e_type = random.choice(e_types)
#while loop (all main code goes here)
while run:
pygame.time.delay(80)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#key presses
keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and x > vel-16:
x -= vel
if keys[pygame.K_RIGHT] and x < 390:
x += vel

#gravity
e_y = e_y + e_vel
if e_y > 256:
e_y = 0
#player gravity or something
if win.get_at((x,y)) == (0,0,0) or win.get_at((x+16,y)) == (0,0,0):
y += 12
e_type = random.choice(e_types)
#more window stuff
win.fill((255,255,255))
pygame.draw.rect(win, (255,0,0), (x, y, width, height))
win.blit(win, e_type, (0,e_y))
pygame.display.update()

pygame.quit()

获取当前目录怎么样?

# Import the os module
import os
# Get the current working directory
cwd = os.getcwd()

并检查您的目录是否正确

# Print the current working directory
print("Current working directory: {0}".format(cwd))

如果一切都正确,请将调用图像的代码替换为:

png1 = pygame.image.load(cwd+"1.png")

相关内容

最新更新