1

Hello everybody, I am making a runner style game. When I try to pick a random image for the sprite, (enemy), it comes up with the error:

Taceback (most recent call last): File "c:\Users\chizl\Documents\Python\PixelRunnerMain\PixelRunner.py", line 24, in pygame.image.load(random_image) FileNotFoundError: No file '2.png' found in working directory 'C:\Users\chizl\Documents\Python" or

Because I pick a random image 2.png is replaced with 1.png sometimes.

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
images = ["1.png", "2.png"]
random_image = random.choice(images)
pygame.image.load(random_image)

#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
        random_image = random.choice(images)

    #more window stuff
    win.fill((255,255,255))
    pygame.draw.rect(win, (255,0,0), (x, y, width, height))
    win.blit(win, random_image,  (e_x, e_y)) 
    pygame.display.update()
   
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
The name and path of the file can be retrieved with __file__. The current working directory can be get changed with os.chdir(path).
Put the following at the beginning of your code to set the working directory to the same as the script's directory:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Load all the images before you choose a random image:

fimenames = ["1.png", "2.png"]
images = [pygame.image.load(f) for f in fimenames]
random_image = random.choice(images)

blit has just 2 arguments:

win.blit(win, random_image, (e_x, e_y))

win.blit(random_image, (e_x, e_y)) 

See How do I detect collision in pygame? and use pygame.Rect objects and colliderect() to detect the collision of the image and the rectangle:

rect = pygame.Rect(x, y, width, height)
image_rect = random_image.get_rect(topleft = (e_x, e_y))
if rect.colliderect(image_rect):
    y += 12
    random_image = random.choice(images)

Complete example:

import pygame, random
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

#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

filenames = ["1.png", "2.png"]
images = [pygame.image.load(name) for name in filenames]
random_image = random.choice(images)

#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
    rect = pygame.Rect(x, y, width, height)
    image_rect = random_image.get_rect(topleft = (e_x, e_y))
    if rect.colliderect(image_rect):
        y += 12
        random_image = random.choice(images)

    #more window stuff
    win.fill((255,255,255))
    pygame.draw.rect(win, (255,0,0), (x, y, width, height))
    win.blit(random_image,  (e_x, e_y)) 
    pygame.display.update()
   
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174