繁体   English   中英

我如何在Pygame中实例精灵? 我也需要吗?

[英]How do i instance sprites in Pygame? Do i need too?

因此,我正在为自己的计算机课程中的某些课程工作而开发游戏,我几乎完成了,但是为了我的生命,我无法在正确的位置生成多个蜘蛛精灵,也无法在关卡之间正确重置。 我尝试过将不同的实例添加到组中,但是我尝试的每种方法总是会收到不同的错误。 该代码在下面,对pygame来说还是很新的内容,对不起代码混乱。

    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()

这是Spider的代码,在World类中被调用之前,它实际上并没有做任何事情,这是我认为大部分问题所在。

  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() 

我发现了您的问题:您用它的一个实例( Spider() )覆盖了Spider类,然后为其赋予了相同的名称。 因此,您一直在向敌人列表中添加同一只蜘蛛。 相反,您应该在文件底部删除此定义,并在要添加(多个)蜘蛛的任何地方创建此实例。

笼统地说,像您一样广泛使用全局变量被认为是不好的风格(并且对性能不太好)。 最好在函数和类之间传递它们。 同样,用于所有变量的CamelCase大写字母通常仅用于类。 我建议检查pep8,以了解有关如何使用常见Python样式的更多信息。 它使这类问题更容易发现,更不可能发生,并且简化了涉及人员的阅读。 正确地使用这些点甚至可以显着提高您的成绩;)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM