gpt4 book ai didi

python - 我正在尝试设置一个 if 语句以在图像消失时删除 Sprite , Sprite 留在周围(继续激活 if 语句

转载 作者:行者123 更新时间:2023-12-04 08:27:13 25 4
gpt4 key购买 nike

我在我的项目中添加了一个 If 语句,以允许它吃掉其中一个实体,但它似乎不想工作,因为当图像消失时实际实体没有

if prey.is_collided_with(carrot):
print('works')
carrot.kill()
all_sprites.remove(carrot)
当它删除胡萝卜时,它会导致“工作不断被打印”这个词我不确定是不是因为实体在它“吃掉”胡萝卜后没有移动,但它似乎也没有按照我的意图工作

最佳答案

pygame.sprite.Sprite.kill :

The Sprite is removed from all the Groups that contain it. This won't change anything about the state of the Sprite.


变量 carrot仍然有效。为胡萝卜创建一个组并使用组而不是变量:
pygame.init()#setsup pygame
screen = pygame.display.set_mode((1000,800))#setsup screen
clock = pygame.time.Clock()#tells program how fast to update

timmer = 1#how long program has gone on for
#setsup the classes
predator = Predator()
prey = Prey()
carrot = Carrot()

#setsup the classes as sprites
all_sprites = pygame.sprite.Group()
all_sprites.add(predator)
all_sprites.add(prey)
all_sprites.add(carrot)

carrot_group = pygame.sprite.Group()
carrot_group.add(carrot)

#start of program
running=True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False

movementx, movementy = Track(predator.x, prey.x, predator.y, prey.y)#how the predators move
predator.move(movementx, movementy)

carrots = carrot_group.sprites()
if len(carrots) > 0:
movementx, movementy = Track(prey.x, carrots[0].x, prey.y, carrots[0].y)#how the prey move
prey.move(movementx, movementy)

screen.fill((0,128,0))#background
all_sprites.draw(screen)#makes the sprites
pygame.display.update()#updates screen
timmer=timmer+1

for carrot in carrots:
if prey.is_collided_with(carrot):
print('works')
carrot.kill()

pygame.quit()
exit()

关于python - 我正在尝试设置一个 if 语句以在图像消失时删除 Sprite , Sprite 留在周围(继续激活 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65204249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com