0
votes

I'm trying to display an image using pygame, but I get this error:

Traceback (most recent call last): File "H:/profile/desktop/untitled/venv/Scripts/AhjaiyGame.py", line 28, in start = pygame.image.load(os.path.join(folder, "wecvguh.png")) pygame.error: Couldn't open H:\profile\desktop\untitled\venv\Scripts\wecvguh.png

Code block:

import sys
import random
import os
import subprocess
import pygame
pygame.init()
GUI = pygame.display.set_mode((800,600))
pygame.display.set_caption("The incredible guessing game")
x = 284
y = 250
width = 68
length = 250
run = True
while run:
 for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run =False
 if event.type == pygame.KEYDOWN:
  command = "python AhjaiyCPT.py"
  subprocess.call(command)


 pygame.display.update()


folder = os.path.dirname(os.path.realpath(__file__))

start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
   def img(x,y):
        gameDisplay.blit(start, (x,y))
    while run:
        gameDisplay.fill(white)
        start(x, y)


pygame.quit()
1
Can you verify your wecvguh.png exists on the filesystem at: H:\profile\desktop\untitled\venv\Scripts\wecvguh.png ? Can you also look at the permissions granted to this image? - Nathan
I fixed the error by moving the image, but the image does not display - HamiClash

1 Answers

1
votes

The code has two "run" loops, so it never gets to the second loop.

The code's indentation is confused - maybe from a paste into SO?. The overwhelming majority of programmers use 4 spaces to indent. This is probably a good custom to follow.

The code also loaded the "start" image every loop iteration, this is unnecessary (unless it changes on disk, in this case use os.stat() to check for changes before re-loading it).

Re-worked main loop:

folder = os.path.dirname(os.path.realpath(__file__))
start  = pygame.image.load(os.path.join(folder, "wecvguh.png"))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            command = "python AhjaiyCPT.py"
            subprocess.call(command)

    gameDisplay.fill(white)
    gameDisplay.blit(start, (x,y))
    pygame.display.update()

pygame.quit()