Python Forum
Issue with Pygame opening my Sprite png
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with Pygame opening my Sprite png
#1
import pygame, sys
from pygame.locals import *
from random import randrange

#initialize pygame
pygame.init()

#colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
green = (0,250,154)

#speed
xspeed = 0
yspeed = 0

##############################
class SpriteSheetImage(pygame.sprite.Sprite):
    def __init__(self, target):
        pygame.sprite.Sprite.__init__(self) #extend the base Sprite class
        self.master_image = None
        self.frame = 0
        self.old_frame = -1
        self.frame_width = 1
        self.frame_height = 1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0

    #X property
    def _getx(self): return self.rect.x
    def _setx(self,value): self.rect.x = value
    X = property(_getx,_setx)

    #Y property
    def _gety(self): return self.rect.y
    def _sety(self,value): self.rect.y = value
    Y = property(_gety,_sety)

    #position property
    def _getpos(self): return self.rect.topleft
    def _setpos(self,pos): self.rect.topleft = pos
    position = property(_getpos,_setpos)
        
    def load(self, filename, width, height, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.rect = Rect(0,0,width,height)
        self.columns = columns
        #try to auto-calculate total frames
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width // width) * (rect.height // height) - 1

    def update(self, current_time, rate=30):
        #update animation frame number
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        #build current frame only if it changed
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width
            frame_y = (self.frame // self.columns) * self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame
##############################

##############################
screen = pygame.display.set_mode((800,800))
pygame.display.set_caption("Sprite Game")
clock = pygame.time.Clock()
ticks = pygame.time.get_ticks()
##########
#warlock
warlock = SpriteSheetImage(screen)
warlock.load("warlock.png",30,51,4) #width, height, number of columns
warlock.position = 390,400
warlockGroup = pygame.sprite.Group()
warlockGroup.add(warlock)
warlock.first_frame = 0
warlock.last_frame = 0
##########
#warlockcorrupt
for n in range(0,5):
    warlockcorrupt = SpriteSheetImage(screen)
    warlockcorrupt.load("warlockcorrupt2.png",36,72,2) #width, height, number of columns
    warlockcorruptGroup = pygame.sprite.Group()
    warlockcorruptGroup.add(warlockcorrupt)
    warlockcorrupt.first_frame = 0
    warlockcorrupt.last_frame = 1
    warlockcorrupt.dir = 0
    warlockcorruptXspeed = 3
    warlockcorruptYspeed = -3
    warlockcorrupt.position = 95,55
##########
#health
health = SpriteSheetImage(screen)
health.load("health.png",200,200,5) #width, height, number of columns
healthGroup = pygame.sprite.Group()
health.position = 525,660
healthGroup.add(health)
health.first_frame = 0
health.last_frame = 0
##########
#floor
floor = SpriteSheetImage(screen)
floor.load("floor.png", 800,800,4) #width, height, number of columns
floor.first_frame = 0
floor.last_frame = 0
floorGroup = pygame.sprite.Group()
floorGroup.add(floor)
##########
#startscreen
##start = SpriteSheetImage(screen)
##start.load("startscreen.png", 585,512,7) #width, height, number of columns
##start.first_frame = 0
##start.last_frame = 0
##startGroup = pygame.sprite.Group()
##startGroup.add(start)
##########
#label
myFont = pygame.font.Font(None,30)
label = myFont.render("level complete ",1,white)
win = False
##############################
order = 0   
##############################
while True:
    if win == False:
        screen.fill(black)
        floorGroup.update(ticks,5)
        floorGroup.draw(screen)
##########
##        startGroup.update(ticks,40)
##        startGroup(screen)
##########
        warlockGroup.update(ticks,40)
        warlockGroup.draw(screen)
        warlock.X = warlock.X + xspeed
        warlock.Y = warlock.Y + yspeed
##########
        healthGroup.update(ticks,1)
        healthGroup.draw(screen)
##########
        if order == 9:
            warlockcorrupt.update(ticks,40)
            warlockcorruptGroup.draw(screen) 
##########   
        if (warlock.X >=65 and warlock.X <=105) and (warlock.Y >=40 and warlock.Y <=80) and order == 0:
            floor.frame = 1
            floor.first_frame = 1
            floor.last_frame = 1
            order = 1
        if (warlock.X >=665 and warlock.X <=705) and (warlock.Y >=40 and warlock.Y <=80) and order == 1:
            floor.first_frame = 2
            floor.last_frame = 2
            order = 2
        if (warlock.X >=360 and warlock.X <=410) and (warlock.Y >=250 and warlock.Y <=300) and order == 2:
            floor.first_frame = 3
            floor.last_frame = 3
            screen.blit(label,(10,10))
            order = 3
        if (warlock.X >=340 and warlock.X <=440) and (warlock.Y >=685 and warlock.Y <=730) and order == 3:
            warlock.X = 390
            warlock.Y = 400
            floor.first_frame = 4
            floor.last_frame = 4
            order = 4
        if (warlock.X >=360 and warlock.X <=410) and (warlock.Y >=250 and warlock.Y <=300) and order == 4:
            floor.first_frame = 10
            floor.last_frame = 10
        if (warlock.X >=66 and warlock.X <=108) and (warlock.Y >=106 and warlock.Y <=154)and order == 4:
            floor.first_frame = 5
            floor.last_frame = 5
            order = 5
        if (warlock.X >=666 and warlock.X <=708) and (warlock.Y >=106 and warlock.Y <=157)and order == 5:
            floor.first_frame = 6
            floor.last_frame = 6
            order = 6
        if (warlock.X >=63 and warlock.X <=111) and (warlock.Y >=181 and warlock.Y <=229)and order == 6:
            floor.first_frame = 7
            floor.last_frame = 7
            order = 7
        if (warlock.X >=657 and warlock.X <=712) and (warlock.Y >=181 and warlock.Y <=229)and order == 7:
            floor.first_frame = 8
            floor.last_frame = 8
            order = 8
        if (warlock.X >=135 and warlock.X <=189) and (warlock.Y >=250 and warlock.Y <=304)and order == 8:
            floor.first_frame = 9
            floor.last_frame = 9
            order = 9
        if (warlock.X >=582 and warlock.X <=639) and (warlock.Y >=31 and warlock.Y <=79)and order == 9:
            floor.first_frame = 10
            floor.last_frame = 10
########
##for c in warlockcorruptGroup:
##    if c.dir == 0: #top row in the sprite sheet
##        c.frame = 0
##        c.first_frame = 0
##        c.last_frame = 1
##        c.Y = c.Y + 3
##        c.X = c.X + 0
##
##    if c.dir == 1:#second row in the sprite sheet
##        c.frame = 0
##        c.first_frame = 0
##        c.last_frame = 1
##        c.Y = c.Y + 0
##        c.X = c.X -3
##
##    if c.dir == 2: #third row in the sprite sheet
##        c.frame = 0
##        c.first_frame = 0
##        c.last_frame = 1
##        c.Y = c.Y + 0
##        c.X = c.X + 3
##
##    if c.dir == 3: #fourth rows in the sprite sheet
##        c.frame = 0
##        c.first_frame = 0
##        c.last_frame = 1
##        c.Y = c.Y -3
##        c.X = c.X + 0
##
##    #bounce off of the wall
##    if c.X >= 775:
##            c.dir = 0
##    if c.X <= 25:
##            c.dir = 0
##    if c.Y >= 575:
##            c.dir = 0
##    if c.Y <= 25:
##            c.dir = 0
##
##    #collision
##    if pygame.sprite.spritecollide(warlock,corruptwarlockGroup,True):
##        print("corrupt destroyed")
##            
            
##############################
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                warlock.frame = 3
                warlock.first_frame = 3
                warlock.last_frame = 3
                yspeed = -3
            if event.key == pygame.K_DOWN:
                warlock.frame = 0
                warlock.first_frame = 0
                warlock.last_frame = 0
                yspeed = 3
            if event.key == pygame.K_LEFT:
                warlock.frame = 2
                warlock.first_frame = 2
                warlock.last_frame = 2
                xspeed = -3
            if event.key == pygame.K_RIGHT:
                warlock.frame = 1
                warlock.first_frame = 1
                warlock.last_frame = 1
                xspeed = 3

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                yspeed = 0
                warlock.first_frame = 3
                warlock.last_frame = 3
            if event.key == pygame.K_DOWN:
                yspeed = 0
                warlock.first_frame = 0
                warlock.last_frame = 0
            if event.key == pygame.K_LEFT:
                xspeed = 0
                warlock.first_frame = 2
                warlock.last_frame = 2
            if event.key == pygame.K_RIGHT:
                xspeed = 0
                warlock.first_frame = 1
                warlock.last_frame = 1
##############################
    pygame.display.update()
    ticks = pygame.time.get_ticks()
    clock.tick(60)
HERE IS MY ERROR MESSAGE:
Traceback (most recent call last):
  File "/Users/*******/Desktop/Sprite Game/Sprite Game (latest).py", line 85, in <module>
    warlock.load("warlock.png",30,51,4) #width, height, number of columns
  File "/Users/*******/Desktop/Sprite Game/Sprite Game (latest).py", line 51, in load
    self.master_image = pygame.image.load(filename).convert_alpha()
pygame.error: Couldn't open warlock.png
>>>
Also, all of my sprites and code are contained together in the same folder.
Reply
#2
Just a sanity check try running this just before loading the image
print(os.getcwd())
Are you running this with an IDE? If so i would run it via terminal/command prompt and see if it is an IDE related issue. IDE's can change the current working directory than what you think it might be.

you can try using the absolute path
Quote:Change pygame.image.load('/images/xxx') to

pygame.image.load('/Users/xxx/..../images/xxxx')

What was your pygame installation process? Did you happen to compile pygame without a dependency or did you use pip?
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020