简体   繁体   English

显示额外的精灵?

[英]Extra sprite being displayed?

I have a problem with an extra sprite being displayed in my space invaders program that I am making for my A Level computing project.我在为 A Level 计算项目制作的太空入侵者程序中显示一个额外的精灵有问题。

It looks like this:它看起来像这样: 太空侵略者精灵

My code looks like this:我的代码如下所示:

class Enemy(pygame.sprite.Sprite):

    def __init__(self,x,y,direction,enemy_type):
        pygame.sprite.Sprite.__init__(self)

        self.EnemyType = enemy_type
        self.Direction = direction

        if enemy_type == 1:
            enemy_image = pygame.image.load("sprites\\enemy1_1.png")
            self.Speed = 1
            self.Score = 5

        if enemy_type == 2:
            enemy_image = pygame.image.load("sprites\\enemy1_1.png")
            self.Score = 15
            self.Speed = 1

        if enemy_type == 3:
            enemy_image = pygame.image.load("sprites\\enemy1_1.png")
            self.Score = 10
            self.Speed = 1

        if enemy_type == 4:
            enemy_image = pygame.image.load("sprites\\enemy1_1.png")
            self.Score = 20
            self.Speed = 1

        if enemy_type == 5:
            enemy_image = pygame.image.load("sprites\\enemy1_1.png")
            self.Score = 25
            self.Speed = 1

        self.image = pygame.Surface([100, 100])
        self.image.set_colorkey(BLACK)
        self.image.blit(enemy_image,(0,0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def move_enemy(self):
        if self.Direction == "right":
            self.rect.x += self.Speed
        if self.Direction == "left":
            self.rect.x -= self.Speed

    #draw image
    def draw(self, screen):
        screen.blit(self.image, self.rect)


allEnemies = pygame.sprite.Group()
a_enemies = Enemy(50,50,"right",1)
allEnemies.add(a_enemies)


#For X coords
    spawnPositions = [100,200,300,400,500,600,700]

    yCoord = 10
#creating enemies
    for n in range(5):
        for i in range(len(spawnPositions)):
            xCoord = spawnPositions[i] 
            enemy_type = random.randint(1,5)
            enemy = Enemy(xCoord, yCoord,"right", enemy_type)
            allEnemies.add(enemy)
        yCoord = yCoord + 50
#update enemy positions
        loop = 0


        for enemy in (allEnemies.sprites()):
            if enemy.rect.x < 5:
                enemy.rect.y = enemy.rect.y + 15
                enemy.Direction = "right"
            if enemy.rect.x > 750:
                enemy.rect.y = enemy.rect.y + 15
                enemy.Direction = "left"
            loop =+1

        for enemy in (allEnemies.sprites()):
            enemy.move_enemy()
#draw enemies
        allEnemies.draw(gameDisplay)

I've cut out irrelevant bits of code from my program to help make it easier for you to understand.我从我的程序中删除了不相关的代码,以帮助您更容易理解。 I'm not too sure why it does this but i'm trying to just make the enemy class just move within the rows like the picture shows, but an extra sprite appears next to them.我不太清楚它为什么会这样做,但我试图让敌人类像图片所示那样在行内移动,但在它们旁边会出现一个额外的精灵。

The extra enemy is caused by the singel enemy that is add to the group when it is intiilaized:额外的敌人是由在初始化时添加到组中的单个敌人引起的:

 a_enemies = Enemy(50,50,"right",1) allEnemies.add(a_enemies)

Remove that code, to solve the issue.删除该代码以解决问题。

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

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