0

First of all, this is one of my first projects in Pygame, so you'll have to bear my incompetence in it.

I'm coding Pac Man in Python using Pygame and Visual Studio Code and have borrowed a piece of code from the MagPi magazine to check whether the place the Pac Man wants to move is valid or not. This requires it to load 2 images, a black and white map to check the available routes it can take and another to check where it'll place the pellets later on down the line. If the square is black, the Pac Man can move to it, if its white the Pac Man cannot. But yet trying to load this is where the program fails.

Here's the entire bit of code (better to be safe than sorry, I suppose):

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

The error I get is: pygame.error: Couldn't open movemap.png (it obviously hasn't bothered with the 2nd image as it can't even get past the first)

What I've tried so far:

-Having the images in both .png and .jpg formats in both the folder my .py is in and a separate /images directory

-Using os.path.join

-Using a direct path

All have yielded the same issue. I've seen so many other questions asking this and have tried their solutions but all have given the same issue yet again. Is it an issue with Visual Studio Code, or is there something super obvious I'm missing? Like I said, I'm pretty novice. I've only done Python at a basic level where it only runs in the shell up until very recently, and outside of that a few basic tutorials for the easier Pygame Zero, this is a whole new world for me, so please forgive me if the error is painfully obvious.

Thanks in advance

lg325
  • 1
  • You need to provide the correct path to the image. You can either use a relative path or an absolute path. Using an absolute path is generally consider bad (as it'll only work if your image is at a very specific location on the computer) but it's a quick way for you to get up and running. Simply right click the file of the image and copy the full path. On Mac `"/Users/yourname/Documents\Pacman\images\movemap.png"` and on Windows it might look something like `"C:\Documents\Pacman\images\movemap.png"` (although you have to write `"C:\\Documents\\Pacman\\images\\movemap.png"` in your code) – Ted Klein Bergman Nov 22 '20 at 22:15
  • Hi, I've changed the code to the absolute path of the file. Same error persists. – lg325 Nov 23 '20 at 10:29

0 Answers0