Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的函数从列表中删除这些目标后,它们会重新出现?_Python_Pygame - Fatal编程技术网

Python 为什么我的函数从列表中删除这些目标后,它们会重新出现?

Python 为什么我的函数从列表中删除这些目标后,它们会重新出现?,python,pygame,Python,Pygame,我正在设计一款射击游戏。参考以下代码,“时钟”达到每个目标阈值后,“所有_目标”中的项目应添加到目标_精灵中(为了延迟)。然后,我使用另一个函数从所有与子弹碰撞的目标中删除目标。然后我使用“self.destromed”来区分显示的是哪一个游戏结束屏幕,赢家屏幕显示6个目标是否全部命中,输家屏幕显示的是第6个目标离开竞技场后6个目标是否全部命中。这在一秒钟内有效,但随后它们都重新出现。我试着把所有的目标列表放在while循环中,但仍然不起作用 clock = 0 py_clock = pygam

我正在设计一款射击游戏。参考以下代码,“时钟”达到每个目标阈值后,“所有_目标”中的项目应添加到目标_精灵中(为了延迟)。然后,我使用另一个函数从所有与子弹碰撞的目标中删除目标。然后我使用“self.destromed”来区分显示的是哪一个游戏结束屏幕,赢家屏幕显示6个目标是否全部命中,输家屏幕显示的是第6个目标离开竞技场后6个目标是否全部命中。这在一秒钟内有效,但随后它们都重新出现。我试着把所有的目标列表放在while循环中,但仍然不起作用

clock = 0
py_clock = pygame.time.Clock()

#Using pygame.sprite to create a class for the player sprite.
player_img = pygame.image.load('santa.png')

class Player(pygame.sprite.Sprite):
    def __init__(self, width, height):
        pygame.sprite.Sprite.__init__(self, player_sprites)
        self.image = pygame.Surface([width, height])
        self.image = player_img
        self.rect = self.image.get_rect()
        self.rect.center = (800 / 2, 545)

player_sprites = pygame.sprite.Group()

player_sprite = Player(64,64)
player_sprites.add(player_sprite)

#Using pygame.sprite to create a class for the target sprites.
target_img = pygame.image.load('Christmas Target.png')

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
        self.destroyed = False
        self.spawned = False


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)

destroyed_targets = []

def wrecked():
  for item in all_targets:
    if item.destroyed == True:
      all_targets.remove(item)
      destroyed_targets.append(item)


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

def movement():
  for item in target_sprites:
    item.rect.x += 1

#Creating a function which will deal with redrawing all sprites and updating the screen.
def refresh_window():
  if len(target_sprites) > 0:
   window.blit(bgr, (0,0))
  if len(all_targets) == 0 and len(destroyed_targets) == 6:
    window.blit(winner, (0,0))
  if len(target_sprites) > 0 and target_6.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_5.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_4.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_3.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_2.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_1.rect.x >= 575:
    window.blit(loser, (0,0))
  player_sprites.draw(window)
  target_sprites.draw(window)
  for item in all_bullets:
    pygame.draw.rect(window, BLUE, (item['x']-5, item['y']-5, 10, 10))
    b_hitbox = (item['x']-10, item['y']-10, 20, 20)
    pygame.draw.rect(window, BLUE, b_hitbox, 2)
  pygame.display.update()

#The game itself is contained in this while loop (game loop)
while exec:
  current_time = pygame.time.get_ticks() 

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
            exec = False

  if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            dx = event.pos[0] - (player_sprite.rect.x+ 100//2)
            dy = event.pos[1] - player_sprite.rect.y
            direction = pygame.math.Vector2(dx, dy).normalize()
            bullet = {'x': player_sprite.rect.x+42, 'y': player_sprite.rect.y, 'direction': direction}
            all_bullets.append(bullet)

  all_bullets_keep = []

  for item in all_bullets:
    item['x'] += item['direction'][0] # item['direction'][0] * 2
    item['y'] += item['direction'][1] # item['direction'][1] * 2

    if 0 < item['x'] < 800 and 0 < item['y'] < 575:
          all_bullets_keep.append(item)

  all_bullets = all_bullets_keep

  all_targets = [target_1, target_2, target_3, target_4, target_5, target_6]

  target_delay()

  movement()

  wrecked()
clock=0
py_clock=pygame.time.clock()
#使用pygame.sprite为玩家sprite创建一个类。
player\u img=pygame.image.load('santa.png')
职业玩家(pygame.sprite.sprite):
定义初始值(自身、宽度、高度):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我,玩家\u精灵)
self.image=pygame.Surface([宽度,高度])
self.image=player\u img
self.rect=self.image.get_rect()
self.rect.center=(800/2545)
player_sprites=pygame.sprite.Group()
玩家\精灵=玩家(64,64)
玩家精灵。添加(玩家精灵)
#使用pygame.sprite为目标精灵创建类。
target\u img=pygame.image.load('Christmas target.png')
类目标(pygame.sprite.sprite):
定义初始值(自身、宽度、高度、偏移、阈值):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface([宽度,高度])
self.image=target\u img
self.rect=self.image.get_rect()
self.rect.center=(self.rect.x+50,self.rect.y+offset)
self.threshold=阈值
自我毁灭=错误
self.sprowned=False
target_sprites=pygame.sprite.Group()
目标_1=目标(100100100,0)
目标_2=目标(100100300150)
目标_3=目标(100100200300)
目标4=目标(100100100450)
目标_5=目标(100100400600)
目标_6=目标(100100250750)
销毁的_目标=[]
def失事():
对于所有_目标中的项目:
如果item.destromed==True:
所有_目标。删除(项目)
销毁目标。追加(项目)
def target_delay():
全球时钟
时钟+=1
对于所有_目标中的项目:
如果时钟>=项阈值:
目标\u精灵。添加(项目)
def移动():
对于目标精灵中的项目:
item.rect.x+=1
#创建一个函数,该函数将处理重绘所有精灵和更新屏幕。
def refresh_窗口():
如果len(目标精灵)>0:
窗位(bgr,(0,0))
如果len(所有_目标)=0且len(已摧毁_目标)=6:
窗口。blit(获胜者,(0,0))
如果len(target_sprites)>0且target_6.rect.x>=575:
窗口blit(失败者,(0,0))
如果len(target_sprites)>0且target_5.rect.x>=575:
窗口blit(失败者,(0,0))
如果len(target_sprites)>0且target_4.rect.x>=575:
窗口blit(失败者,(0,0))
如果len(target_sprites)>0且target_3.rect.x>=575:
窗口blit(失败者,(0,0))
如果len(target_sprites)>0且target_2.rect.x>=575:
窗口blit(失败者,(0,0))
如果len(target_sprites)>0且target_1.rect.x>=575:
窗口blit(失败者,(0,0))
玩家\精灵绘制(窗口)
目标_精灵绘制(窗口)
对于所有项目符号中的项目:
pygame.draw.rect(窗口,蓝色,(项目['x']-5,项目['y']-5,10,10))
b_hitbox=(项目['x']-10,项目['y']-10,20,20)
pygame.draw.rect(窗口,蓝色,b_-hitbox,2)
pygame.display.update()
#游戏本身包含在此while循环(游戏循环)中
执行董事:
当前时间=pygame.time.get_ticks()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
exec=False
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
dx=event.pos[0]-(player_sprite.rect.x+100//2)
dy=事件位置[1]-玩家\u精灵.rect.y
direction=pygame.math.Vector2(dx,dy).normalize()
bullet={'x':player_sprite.rect.x+42,'y':player_sprite.rect.y,'direction':direction}
所有项目符号。追加(项目符号)
所有子弹保持=[]
对于所有项目符号中的项目:
项目['x']+=项目['direction'][0]#项目['direction'][0]*2
项目['y']+=项目['direction'][1]#项目['direction'][1]*2
如果0
我投票结束这个问题,因为堆栈溢出不是调试服务。请阅读。我投票结束这个问题,因为堆栈溢出不是调试服务。请阅读。