gpt4 book ai didi

python - 我试图用子弹击中永久 'kill' 目标,但在使用 '.kill()' (PYGAME) 后它们重新出现

转载 作者:行者123 更新时间:2023-12-01 15:03:24 27 4
gpt4 key购买 nike

我最近上传了很多问题,我认为人们对我感到厌倦,但我不擅长编程,并且正在尝试为 A-level 类(class)编写游戏代码,所以我需要所有可以得到的帮助来学习语言。不管怎样,我将在下面展示一些相关的代码以供引用。我使用 all_targets 是因为我想使用 threshold 添加延迟,这样它们就不会同时生成。我认为这导致目标被重新添加到 Sprite 组,但我无法解决它。我希望目标在被子弹击中时从 target_sprites 中移除。我尝试使用 bool 值来实现 if destroyed == True:all_bullets.remove(item) 但它似乎不起作用。感谢您的帮助。

class Target(pygame.sprite.Sprite):
def __init__(self, width, height, offset, threshold):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image = target_img
self.rect = self.image.get_rect()
self.rect.center = (self.rect.x + 50, self.rect.y + offset)
self.threshold = threshold



target_sprites = pygame.sprite.Group()

target_1 = Target(100, 100, 100, 0)
target_2 = Target(100, 100, 300, 150)
target_3 = Target(100, 100, 200, 300)
target_4 = Target(100, 100, 100, 450)
target_5 = Target(100, 100, 400, 600)
target_6 = Target(100, 100, 250, 750)

#Function to add delay between targets spawning.

def target_delay():
global clock
clock += 1
for item in all_targets:
if clock >= item.threshold:
target_sprites.add(item)

#Function to make targets move each time screen refreshes.
def movement():
for item in target_sprites:
item.rect.x += 1
  for item in all_bullets_keep:
if item['y']-30 < (target_1.rect.y) + 100 and item['y']+30 > target_1.rect.y:
if item['x']+10 > target_1.rect.x and item['x']-30 < (target_1.rect.x) + 100:
target_1.kill()
if item['y']-30 < (target_2.rect.y) + 100 and item['y']+30 > target_2.rect.y:
if item['x']+10 > target_2.rect.x and item['x']-30 < (target_2.rect.x) + 100:
target_2.kill()
if item['y']-30 < (target_3.rect.y) + 100 and item['y']+30 > target_3.rect.y:
if item['x']+10 > target_3.rect.x and item['x']-30 < (target_3.rect.x) + 100:
target_3.kill()
if item['y']-30 < (target_4.rect.y) + 100 and item['y']+30 > target_4.rect.y:
if item['x']+10 > target_4.rect.x and item['x']-30 < (target_4.rect.x) + 100:
target_4.kill()
if item['y']-30 < (target_5.rect.y) + 100 and item['y']+30 > target_5.rect.y:
if item['x']+10 > target_5.rect.x and item['x']-30 < (target_5.rect.x) + 100:
target_5.kill()
if item['y']-30 < (target_6.rect.y) + 100 and item['y']+30 > target_6.rect.y:
if item['x']+10 > target_6.rect.x and item['x']-30 < (target_6.rect.x) + 100:
target_6.kill()

最佳答案

您不需要阈值属性。删除它:

class Target(pygame.sprite.Sprite):
def __init__(self, width, height, offset):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image = target_img
self.rect = self.image.get_rect()
self.rect.center = (self.rect.x + 50, self.rect.y + offset)

创建一个空的 Sprite 组:

target_sprites = pygame.sprite.Group()

pygame.init() 以来的毫秒数可以通过 pygame.time.get_ticks() 检索.参见 pygame.time模块。在主应用程序循环中使用滴答按间隔生成 Sprite (target_delay 必须在应用程序循环中调用):

offsets = [100, 300, 200, 100, 400, 150]
next_target_time = 0
threshold = 1000 # 1000 milliseconds = 1 second

def target_delay():
global next_target_time, offsets
current_time = pygame.time.get_ticks()
if current_time > next_target_time and len(offsets) > 0:
target_sprites.add(Target(100, 100, offsets[0]))
offsets.pop(0)
next_target_time = current_time + threshold

评估一个目标是否必须在循环中被杀死(kill):

for item in all_bullets_keep:
for target in target_sprites:
if item['y']-30 < (target.rect.y) + 100 and item['y']+30 > target.rect.y:
if item['x']+10 > target.rect.x and item['x']-30 < (target.rect.x) + 100:
target.kill()

关于python - 我试图用子弹击中永久 'kill' 目标,但在使用 '.kill()' (PYGAME) 后它们重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60902378/

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