[英]Permanently delete sprite from memory Pygame
I want to delete a sprite permanently from the memory once an event occurs.一旦事件发生,我想从 memory 中永久删除一个精灵。 Using self.kill() doesn't help as the image of the sprite is deleted, but the sprite is still there.
使用 self.kill() 并没有帮助,因为精灵的图像被删除了,但精灵仍然存在。 What can I do to delete it from memory permanently?
我该怎么做才能将它从 memory 中永久删除?
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.
现在,每当子弹碰到(隐形)炸弹时,它也会被删除。
You have to use bomb_list
instead of bomb
.您必须使用
bomb_list
而不是bomb
。 Iterate through bomb_list
to move the bombs:遍历
bomb_list
来移动炸弹:
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
:使用
pygame.sprite.groupcollide
而不是pygame.sprite.spritecollide()
来查找Group bomb_list
和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
.如果 arguments dokill1和dokill2设置为
True
,您根本不需要for bullet in bomb_hit_list:
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.