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()