Play animation upon collision with enemy

て烟熏妆下的殇ゞ 提交于 2021-01-28 05:31:08

问题


so I have loaded the images into pygame:

Explosion = [pygame.image.load('Explosion1.png'), pygame.image.load('Explosion2.png'), pygame.image.load('Explosion3.png'), pygame.image.load('Explosion4.png'), pygame.image.load('Explosion5.png'), pygame.image.load('Explosion6.png'), pygame.image.load('Explosion7.png'), pygame.image.load('Explosion8.png'), pygame.image.load('Explosion9.png'), pygame.image.load('Explosion10.png')]

and I would like, when the bullet, which is a separate class, makes a collision with the missile, it plays this animation at the position where both the bullet and the enemy collide, I'm not sure how I go around doing this?

Collision Script (In the main loop):

hits = pygame.sprite.groupcollide(enemies, bullets, True, True)

Bullet Class:

class Bullet (pygame.sprite.Sprite):
    def __init__ (self, x, y):
        super (Bullet, self).__init__()
        self.surf = pygame.image.load("Bullet.png").convert()
        self.surf.set_colorkey((255,255,255), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedx = bullet_speed
    def update(self):
        self.rect.x += self.speedx

        if self.rect.left > SCREEN_WIDTH:
            self.kill()

Enemy Class:

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.image.load("Missiles.png").convert()
        self.surf.set_colorkey((255,255,255), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(Enemy_SPEED_Min, Enemy_SPEED_Max)



    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()

all of the code is here https://pastebin.com/CG2C6Bkc if you need it!

thank you in advance!


回答1:


Do not destroy the enemies, when it collides with an bullet. Iterate through the enemies and which are returned in hits and start the explosion animation instead of killing the enemies:

hits = pygame.sprite.groupcollide(enemies, bullets, True, True)

hits = pygame.sprite.groupcollide(enemies, bullets, False, True)
for enemy in hits:
    enemy.start_animation()

Add the attributes animation_count, animation_frames and animation to th class Enemy. When the explosion animation is started then animation is set True. animation_frames controls the speed of the animation. If the animation is started, then the enemy stops to move and the Explosion image is shown instead of the enemy. After the last image of the animation is shown, the enemy is killed:

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.image.load("Missiles.png").convert()
        self.surf.set_colorkey((255,255,255), RLEACCEL)
        self.rect = self.surf.get_rect(
            center=(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(Enemy_SPEED_Min, Enemy_SPEED_Max)

        self.animation_count = 0
        self.animation_frames = 10
        self.animation = False

    def start_animation(self):
        self.animation = True

    def update(self):

        if self.animation:
            image_index = self.animation_count // self.animation_frames
            self.animation_count += 1

            if image_index < len(Explosion):
                self.surf = Explosion[image_index]
                self.rect = self.surf.get_rect(center = self.rect.center)

            else:
                self.kill()

        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()



回答2:


Well i dont want to do it for you, But i will say one way you could do it, then you can go and try to do it, and if you run into a problem/error, you can update the question and we can help you.

I would create another class called Explosion and and give it the images, when when you draw it, draw the first image, then change to the second, then draw that one, then change...

Then after it finishes, destroy the object.

So, create the class, create a spritegroup, when the bullet collides, create a new instance of the class, giving it the x and y co ordinates of the collision, update it every frame, and it will draw all the images then destroy itself

also, an easier way to get all of your images instead of typing them all out is

Explosion_imgs = [pygame.image.load("explosion" + str(x) + ".png") for x in range(1,11,1)]


来源:https://stackoverflow.com/questions/61964552/play-animation-upon-collision-with-enemy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!