thenoober
thenoober

Reputation: 77

i want to kill the my last sprite while moving and keeping my current sprtie

import pygame, sys
start = True
class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.attack_animation = False
        self.sprites = []
        self.sprites.append(pygame.image.load('crossHair.png'))
        self.sprites.append(pygame.image.load('crossHair_2.png'))
        self.sprites.append(pygame.image.load('crossHair_3.png'))
        self.sprites.append(pygame.image.load('crossHair_4.png'))
        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        self.image.set_colorkey('white')
        for items in self.sprites:
            items.set_colorkey('white')
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]
        

    def attack(self):
        self.attack_animation = True
        self.image.set_colorkey('white')

    def update(self,speed):
        self.image.set_colorkey('white')
        
        self.current_sprite += speed
        if int(self.current_sprite) >= len(self.sprites):
            self.attack_animation = False
            self.current_sprite = 0
        self.image = self.sprites[int(self.current_sprite)]
# General setup
pygame.init()
clock = pygame.time.Clock()
# Game Screen
screen_width = 400
screen_height = 400
mouse = pygame.mouse.get_pos()
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Sprite Animation")

# Creating the sprites and groups


moving_sprites = pygame.sprite.Group()
while True:
    globals()['mouse'] = pygame.mo[![now this is the problem][1]][1]use.get_pos()
    player = Player(mouse[0],mouse[1])
    moving_sprites.add(player)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            player.attack()
    # Drawing
    screen.fill((0,0,0))
    screen.set_colorkey('white')
    moving_sprites.draw(screen)
    moving_sprites.update(0.04)
    pygame.display.flip()
    clock.tick(120)

this is creatiing my player / crossHair every 120th of a second and it leaves behind it's last sprite

now the problem with that is it leaves behind my last sprite and if i put moving_sprites = pygame.sprite.Group() in while loop then it won't animate anybody can solve this pls answer my question...

Upvotes: 1

Views: 48

Answers (1)

Rabbid76
Rabbid76

Reputation: 211077

This is not the way to animate sprites. Creating a new sprite every frame is bad practice and a waste of performance since all objects have to be recreated. Even more you load the images in the constructor of the Player class. If you do this every frame, then every frame the images have to be loaded from the volume and decoded.
Create the sprite once before the application loop and change the attributes of the sprite object in the loop. This will give you the best performance:

class Player(pygame.sprite.Sprite):
    # [...]
   
    def set_pos(self, pos_x, pos_y):
        self.rect.topleft = (pos_x, pos_y)
moving_sprites = pygame.sprite.Group()
player = Player(0, 0)
moving_sprites.add(player)

while True:
    player.set_pos(*pygame.mouse.get_pos())

    # [...]

Upvotes: 2

Related Questions