0

I am currently learning the code from a tutorial on Pygame Game Development: https://www.youtube.com/watch?v=fcryHcZE_sM
I have copied the code exactly into Pycharm and I believe it should work.

#Pygame Sprite Example
import pygame
import random
import os


WIDTH = 360
HEIGHT = 480
FPS = 30

#Colours
BLACK = (0, 0, 0)
WHITE= (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


# set up assets folders
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")


class Player(pygame.sprite.Sprite):
    # sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_folder, "p1_jump.png")).convert()
        self.image.set_colourkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

    def update(self):
        self.rect.x += 5
        if self.rect.left > WIDTH:
            self.rect.right = 0

 #initialise pygame and create window
 pygame.init()
 pygame.mixer.init()
 screen = pygame.display.set_mode((WIDTH, HEIGHT))
 pygame.display.set_caption("My Game")
 clock = pygame.time.Clock()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

#Game Loop
running = True
while running:
    #keep loop running at right speed
    clock.tick(FPS)
    #Process Input/Events
    for event in pygame.event.get():
        #check for closing window
        if event.type == pygame.QUIT:
            running = False

    #Update
    all_sprites.update()

    #Draw/Render
    screen.fill(BLUE)
    all_sprites.draw(screen)
    # *after drawing everything, flip the display*
    pygame.display.flip()

pygame.quit()

it gives me this error when I run it:

C:\Users\George\PycharmProjects\app\venv1\Scripts\python.exe "C:/Users/George/Documents/School/Year 12/IT/Coursework/SpriteExample.py"
Traceback (most recent call last):
  File "C:/Users/George/Documents/School/Year 12/IT/Coursework/SpriteExample.py", line 46, in <module>
    player = Player()
  File "C:/Users/George/Documents/School/Year 12/IT/Coursework/SpriteExample.py", line 28, in __init__
    self.image = pygame.image.load(os.path.join(img_folder, "p1_jump.png")).convert()
pygame.error: Couldn't open C:/Users/George/Documents/School/Year 12/IT/Coursework\img\p1_jump.png

Process finished with exit code 1

In the tutorial video, the guy is using a Mac, and I am using a Windows laptop (running Windows 7). I have tried researching around the site and I couldn't find anything specific enough to my error. I also tried changing the code in and just above the initialization of the class Player to this:

game_folder = os.path.dirname(os.path.abspath(__file__))
img_folder = os.path.join(game_folder, "img")
image1 = pygame.image.load(os.path.join(img_folder, "p1_jump.png" ))


class Player(pygame.sprite.Sprite):
    # sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = image1.convert()
        self.image.set_colourkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

and it still didn't work.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
George M.
  • 1
  • 1
  • 2
    Are the image name and the path correct? – skrx Mar 05 '18 at 11:38
  • [This answer](https://stackoverflow.com/a/2259387/7675174) shows how to check for file existence, `os.path.isfile(path)`. Try that before loading the image to verify that you're trying to open a valid file. – import random Mar 06 '18 at 01:37

1 Answers1

0

If you make a little python file name it my_path.py or similar and place it in documents folder and run it. It will help you see that the sprite2.py file should be in documents folder and that you will need a sub folder within documents that is labelled 'img' and contains the 'png' file. Then the game should run ok. Therefore the game_folder is actually 'documents folder' and img_folder is img within documents. I hope that is a little clearer then the code above or within sprite2.py can be tinkered with if you have game in another folder

game_folder = os.path.dirname(__file__)
print(game_folder)
img_folder = os.path.join(game_folder, "img")
print(img_folder)