Ryan Davis
Ryan Davis

Reputation: 1

How do i instance sprites in Pygame? Do i need too?

So im working on a game for some coursework in my computing course, im almost finished but for the life of me i cant get multiple of the Spider sprite to spawn in at the correct locations or reset properly between levels. I've tried adding different instances to groups before but i always get a different error with each method that i try. the code is below and im fairly new to pygame so sorry for the messy code..

#
class Spider(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load( 'Assets/Level-Assets/Level 1/Enemies/Spider_Down.png')
        self.rect = self.image.get_rect()
        self.Health = 3
        self.Spawned = False
        self.range = 100
        self.Gravity= 6
        self.pos_difference = 0
        self.Active = False
        self.Fall = False
        self.Spiders = []

##################################################################################################################
    def Set_Position(self):
        if self.Spawned == False:
            self.rect.center = (World.mapx, World.mapy)
            Enemies.add(self)
            self.Spawned = True

        else:
            self.rect.center = self.rect.center


######################################################################################################################

    def update(self):


        self.pos_difference = Player.rect.centery - self.rect.centery
        if Player.rect.centerx >= self.rect.centerx -4 or Player.rect.centerx >= self.rect.centerx + 4:
            if Player.rect.centery > self.rect.centery:
                if self.pos_difference < 200:
                    self.Active = True
                    self.Fall = True

        if self.Active == True:
            if self.Fall == True:
                self.rect.centery += self.Gravity

This is the code for the Spider, its not actually doing anything until it is called within the World class, which is where i believe the majority of the problem is...

    def DisplayWorld(self):        
        self.MapLoad = False
        for Row in range(len(self.newMap)):
            for Col in range(len(self.newMap[Row])):
                self.mapx = Col * 64 
                self.mapy = ((Row + self.Height_modifier) * 64)
                self.tile_pos = (self.mapx, self.mapy )


                if int(self.newMap[Row][Col]) == 1:
                    self.rect = self.Brick_Center.get_rect(left = (self.mapx) , bottom = (self.mapy))
                    self.World_sprites.add(World)
                    self.Camera_Pos_Check()
                    Player.Collision_Check()
                    Spider.Collision_Check()
                    Shoot.Collision_Check()
                    self.World_sprites.draw(screen)


                elif int(self.newMap[Row][Col]) == 2:
                    Enemies.add(Spider(screen))




def main():
    play = True
    while play:
        if pygame.key.get_pressed()[pygame.K_ESCAPE]:
            play = False
        if not Sprites.sprites():
            Sprites.add(Player,Spider)
            print(Sprites)
        clock.tick(CLOCKRATE)
        pygame.mouse.set_visible(False)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                play = False



        screen.blit(bgi,(0,0))
        screen.blit(bgi,(0,500))
        World.findLevel()
        Sprites.update()
        Enemies.draw(screen)
        Sprites.draw(screen)
        if Shoot.bullet == True:
            Shoot.update()
            for b in range(len(Shoot.bullets)):
                screen.blit(Shoot.image, (Shoot.bullets[b][0],Shoot.bullets[b][1]))

        UI.Health_Display()
        pygame.display.flip()


Sprites = pygame.sprite.Group()
Enemies = pygame.sprite.Group()
UI = User_Interface()
World = World()
Player = Proto()
Shoot = Shoot()
Portal = Portals()
Spider = Spider()



main()

Upvotes: 0

Views: 89

Answers (1)

Martijn
Martijn

Reputation: 417

I've found your problem: you overwrite your Spider class by an instance of it (Spider()) and then give it the same name. Thus you're consistently adding the same single spider to your enemies list. Instead, you should remove this definition on the bottom of your file and where ever you're adding the (multiple) spider(s), you should create this instance.

In a more general remark, it is considered bad style (and not too great for performance) to use global variables as widely as you do. It'd be much better to pass them around between functions and classes. Also, the CamelCase capitalization you use for all of your variables is commonly only used for classes. I'd recommend checking up on pep8 to learn more about how to use common Python styles. It makes these kinds of problems easier to spot, less likely to occur, and simplifies the reading for anyone involved. Using these points properly might even improve your grade significantly ;).

Upvotes: 1

Related Questions