Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

python - Permanently delete sprite from memory Pygame

I want to delete a sprite permanently from the memory once an event occurs. Using self.kill() doesn't help as the image of the sprite is deleted, but the sprite is still there. What can I do to delete it from memory permanently?

import pygame
import time
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
GREY = (129, 129, 129)
frame = 0

class SpriteSheet(object):
    def __init__(self, file_name):
        self.sprite_sheet = pygame.image.load(file_name).convert()
    def get_image(self, x, y, width, height, colour):
        image = pygame.Surface([width, height]).convert()
        image.set_colorkey(colour)
        image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
        return image
class Bomb(pygame.sprite.Sprite):
    change_x =0
    change_y = 0
    def __init__(self):
        super().__init__()
        sprite_sheet = SpriteSheet("Untitled.png")
        self.image = sprite_sheet.get_image(2, 2, 48, 48, WHITE)
        self.rect = self.image.get_rect()
    def move(self):
        self.change_y = 2
        self.rect.y += self.change_y
        if self.rect.y > 500:
            self.kill()

class Soldier(pygame.sprite.Sprite):

    def __init__(self):
        self.change_x = 0
        self.change_y = 0
        self.direction = "R"
        super().__init__()
        self.walking_frames_l = []
        self.walking_frames_r = []
        
        sprite_sheet = SpriteSheet("Picture2.png")
        self.image = sprite_sheet.get_image(0, 0, 150, 205, GREY)
        self.walking_frames_l.append(self.image)

        self.image = sprite_sheet.get_image(233, 0, 140, 210, GREY)
        self.walking_frames_l.append(self.image)
        self.image = sprite_sheet.get_image(425, 5, 123, 210, GREY)
        self.walking_frames_l.append(self.image)

        self.image = sprite_sheet.get_image(0, 0, 150, 205, GREY)
        self.image = pygame.transform.flip(self.image, True, False)
        self.walking_frames_r.append(self.image)
        self.image = sprite_sheet.get_image(233, 0, 140, 210, GREY)
        self.image = pygame.transform.flip(self.image, True, False)
        self.walking_frames_r.append(self.image)
        self.image = sprite_sheet.get_image(425, 5, 123, 210, GREY)
        self.image = pygame.transform.flip(self.image, True, False)
        self.walking_frames_r.append(self.image)

        self.image = self.walking_frames_r[0]
        
        self.rect = self.image.get_rect()
        self.rect.y = 297
        self.rect.x = 100
        self.frame = 0
        self.moved = 0



    def move(self):
        self.rect.x += self.change_x
    def walk(self):
        self.moved += abs(self.change_x)

        pixels_for_one_step = 60
        if self.moved > pixels_for_one_step:
            self.frame += 1
            self.moved = 0
            if self.frame >= len(self.walking_frames_r):
                self.frame = 0
        if self.direction =="R":
            self.image = self.walking_frames_r[self.frame]
        else:
            self.image = self.walking_frames_l[self.frame]
            
        if self.change_x == 0 and self.direction == "R":
            self.image = self.walking_frames_r[2]
        if self.change_x == 0 and self.direction == "L":
            self.image = self.walking_frames_l[2]
    def go_left(self):
        self.change_x = -6
        self.direction = "L"
    def go_right(self):
        self.direction = "R"
        self.change_x = 6
    def stop(self):
        self.change_x = 0
        self.image = self.walking_frames_r[2]

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.change_x =0
        self.change_y = 0
        self.direction = ""
        sprite_sheet = SpriteSheet("Bullet_2.png")
        self.image = sprite_sheet.get_image(0, 0, 27, 27, BLACK)
        self.image = pygame.transform.rotate(self.image, 45)
        self.rect = self.image.get_rect()
        self.rect.y = 0
        self.rect.x = 0
    def moveright(self):
        self.change_x =2
        self.change_y = 2
        self.rect.y -= self.change_y
        self.rect.x -= self.change_x
        if self.rect.y < -30:
            self.kill()
    def moveleft(self):
        self.change_x =-2
        self.change_y = 2
        self.rect.y -= self.change_y
        self.rect.x -= self.change_x
        if self.rect.y < -30:
            self.kill()

pygame.init()
screen_width = 1000
screen_height = 500
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
done = False
bomb  = Bomb()
soldier = Soldier()
all_sprites = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
bomb_list = pygame.sprite.Group()
bomb_list.add(bomb)
all_sprites.add(bomb)
all_sprites.add(soldier)
screen_rect = screen.get_rect()
pygame.mouse.set_cursor(*pygame.cursors.broken_x)
bg = pygame.image.load ("3601933.jpg")

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    soldier.go_left()
                if event.key == pygame.K_RIGHT:
                    soldier.go_right()
                if event.key == pygame.K_SPACE:
                    bullet = Bullet()
                    if soldier.direction == "R":
                        bullet.direction = "R"
                        bullet.rect.x = soldier.rect.left -23
                        bullet.rect.y = soldier.rect.top - 23
                    else:
                        bullet.image = pygame.transform.flip(bullet.image, True, False)
                        bullet.rect.x = soldier.rect.left  +110
                        bullet.rect.y = soldier.rect.top - 24
                        bullet.direction = "L"
                    all_sprites.add(bullet)
                    bullet_list.add(bullet)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and soldier.change_x < 0:
                soldier.stop()
            if event.key == pygame.K_RIGHT and soldier.change_x > 0:
                soldier.stop()
                
        
    soldier.rect.clamp_ip(screen_rect)
    screen.blit(bg, [0, 0])
    all_sprites.draw(screen)
    bomb.move()
    soldier.move()
    soldier.walk()
    for bullet in bullet_list:
        if bullet.direction == "R":
            bullet.moveright()
        else:
            bullet.moveleft()
    bomb_hit_list = pygame.sprite.spritecollide(bomb, bullet_list, True)
    for bullet in bomb_hit_list:
        bomb_list.remove(bomb)
        all_sprites.remove(bomb)
    pygame.sprite.spritecollide(soldier, bomb_list, True)
    clock.tick(60)
    pygame.display.flip()
pygame.quit()

I want to delete the bomb. If the bomb touches the player, then its image disappears but it is still there. Now whenever a bullet touches the (invisible) bomb, it gets deleted as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use bomb_list instead of bomb. Iterate through bomb_list to move the bombs:

for bomb in bomb_list:
    bomb.move()

Use pygame.sprite.groupcollide instead of pygame.sprite.spritecollide() to finde the collisions between the Group bomb_list and the Group bullet_list:

bomb_hit_list = pygame.sprite.spritecollide(bomb, bullet_list, True)

bomb_hit_dict = pygame.sprite.groupcollide(bullet_list, bomb_list, True, True)

You don't need the loop for bullet in bomb_hit_list: at all, if the the arguments dokill1 and dokill2 are set True.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...