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.