Pygame图像不适用于Visual Studio代码

2024-10-04 11:28:45 发布

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

首先,这是我在Pygame中的第一个项目,所以你必须忍受我在这方面的无能

我正在用Pygame和visualstudio代码用Python编写Pac-Man,并从MagPi杂志上借用了一段代码来检查Pac-Man想要移动的位置是否有效。这需要它加载2张图像,一张黑白地图检查它可以走的路线,另一张地图检查它稍后将在线路上放置小球的位置。如果正方形是黑色,吃豆人可以移动到它,如果是白色,吃豆人不能。但是,试图加载这是程序失败的地方

下面是完整的代码(我想安全总比抱歉好):

from pygame import image, Color
import os 
moveimage = image.load("movemap.png")
dotimage = image.load("pelletmap.png")
def checkMovePoint(p):
    global moveimage
    if p.x+p.movex < 0: p.x = p.x+600
    if p.x+p.movex > 600: p.x = p.x-600
    if moveimage.get_at((int(p.x+p.movex), int(p.y+p.movey-80))) != Color('black'):
    p.movex = p.movey = 0

def checkDotPoint(x,y):
    global dotimage
    if dotimage.get_at((int(x), int(y))) == Color('black'):
       return True
    return False

def getPossibleDirection(g):
    global moveimage
    if g.x-20 < 0:
       g.x = g.x+600
    if g.x+20 > 600:
       g.x = g.x-600
    directions = [0,0,0,0]
       if g.x+20 < 600:
       if moveimage.get_at((int(g.x+20), int(g.y-80))) == Color('black'): directions[0] = 1
    if g.x < 600 and g.x >= 0:
       if moveimage.get_at((int(g.x), int(g.y-60))) == Color('black'): directions[1] = 1
    if g.x-20 >= 0:
       if moveimage.get_at((int(g.x-20), int(g.y-80))) == Color('black'): directions[2] = 1
    if g.x < 600 and g.x >= 0:
       if moveimage.get_at((int(g.x), int(g.y-100))) == Color('black'): directions[3] = 1
    return directions

我得到的错误是: pygame.error:无法打开movemap.png(显然第二个图像没有问题,因为它甚至无法通过第一个图像)

到目前为止,我所尝试的:

-将.png和.jpg格式的图像放在my.py所在的文件夹和单独的/images目录中

-使用os.path.join

-使用直接路径

所有这些都产生了同样的问题。我看到很多其他问题都在问这个问题,也尝试过他们的解决方案,但都再次提出了同样的问题。这是VisualStudio代码的问题,还是我遗漏了一些非常明显的东西?就像我说的,我是个新手。直到最近,我才在一个基本级别上完成Python,它只在shell中运行,除此之外,还有一些简单的Pygame Zero的基本教程,这对我来说是一个全新的世界,所以如果错误显而易见,请原谅我

提前谢谢


Tags: 代码图像imagegetifpngpygameat