0

So, I cannot recognise what the error is and how to fix it. This is my code :

import pygame
pygame.init()

win = pygame.display.set_mode((500,480))
pygame.display.set_caption("First Game")

walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('bg.jpg')
char = pygame.image.load('standing.png')

x = 50
y = 400
width = 40
height = 60
vel = 5

clock = pygame.time.Clock()

isJump = False
jumpCount = 10

left = False
right = False
walkCount = 0

def redrawGameWindow():
    global walkCount
    
    win.blit(bg, (0,0))  
    if walkCount + 1 >= 27:
        walkCount = 0
        
    if left:  
        win.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1                          
    elif right:
        win.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1
    else:
        win.blit(char, (x, y))
        walkCount = 0
        
    pygame.display.update() 
    


run = True

while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT] and x > vel: 
        x -= vel
        left = True
        right = False

    elif keys[pygame.K_RIGHT] and x < 500 - vel - width:  
        x += vel
        left = False
        right = True
        
    else: 
        left = False
        right = False
        walkCount = 0
        
    if not(isJump):
        if keys[pygame.K_SPACE]:
            isJump = True
            left = False
            right = False
            walkCount = 0
    else:
        if jumpCount >= -10:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: 
            jumpCount = 10
            isJump = False

    redrawGameWindow() 
    
    
pygame.quit()

This code is supposed to import several images. The character is supposed to move as well but the movement is fine. The problem is in importing the image. The same problem is with the background too. The indentations were fine too. I don't know how to resolve this error. And the error is below:

Traceback (most recent call last):
  File "C:/Users/LENOVO/Desktop/GROOT!/er.py", line 7, in <module>
    walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
pygame.error: Couldn't open R1.png

I checked if there was a problem in the name of documents but I was unable to find an error in that! Please help me with this.

  • Is the file `R1.png` in the folder `GROOT!`? How (and from what directory) are you running the code? – xavc Jul 11 '20 at 11:22
  • @xavc the file R1.png is in a compressed folder and in that compressed folder there is another folder named Game. And in that folder is R1.png. –  Jul 11 '20 at 11:27
  • In order for your program to load the file you need to specify where the file is, and not just its name. If you place the file in the same folder (not compressed) as where you're running the game then the image should be able to be loaded. – xavc Jul 11 '20 at 11:29
  • @xavc can you please tell me the code to do that? And if you could please give it in answer form it would be a great favour. –  Jul 11 '20 at 11:32
  • @BruceBanner The current working directory of seems to be "C:/Users/LENOVO/Desktop/GROOT!". Hence the files have to be in this folder. If the files are in a different folder, then you've to specify a relative or absolut path. Or you've to change the working directory. Nobody can answer your question, because nobody knows your directory structure and the working directory of your application. The working directory can be different from the directory where the python file is located. – Rabbid76 Jul 11 '20 at 11:41

2 Answers2

0

From what I understand, your game er.py is running inside the folder GROOT!, on your desktop. In order for the program to access the images, it needs to know where they are. One convenient way (rather than specifying a long path) is to put then where your running the program (which I assume is the folder GROOT!. So, without modifying the code, all you need to do is copy all the images into the folder GROOT!, so long as you run the program in the same folder (that is, you execute python er.py, meaning GROOT! will be your working directory). A more organised method would be to make a folder, like resources, inside of GROOT!. Then, instead of writing pygame.image.load('R1.png'), you would write pygame.image.load('resources\\R1.png') (assuming a Windows platform).

xavc
  • 1,245
  • 1
  • 8
  • 14
0

I recommend to change the working directory of the application. If the files are located in in the same directory as the python file then get the directory of the file and set the working directory:

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

If the images are contained in a sub directory (e.g. iamges) then join the file path and the directory path:

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
imagePath = os.path.join(sourceFileDir, "images") 
os.chdir(imagePath)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174