2017-07-31 38 views
0

私はpygameについてのコードを読んでいました。pygameのちょっとした試合の一部です。「clicked_smileys = [s.rect.collidepoint(pos)]のコードをsprite_listに入れてください」と理解できません。最後の8行目。誰が理解できますか? 私は今はエラーがあると思っていましたが、IDEはそれを実行できます。これはpython3のコードです。s.rect.collidepoint(pos)が意味する場合、sprite_listには何がありますか?

count_smileys = 0 
count_popped = 0 
class Smiley(pygame.sprite.Sprite): 
    pos =(0,0) 
    xvel = 1 
    yvel = 1 
    scale = 100 
    def __init__(self, pos, xvel, yvel): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pic 
     self.scale = random.randrange(10,100) 
     self.image = pygame.transform.scale(self.image, (self.scale,self.scale)) 
     self.rect = self.image.get_rect() 
     self.pos = pos 
     self.rect.x = pos[0] - self.scale/2 
     self.rect.y = pos[1] - self.scale/2 
     self.xvel = xvel 
     self.yvel = yvel 
    def update(self): 
     self.rect.x += self.xvel 
     self.rect.y += self.yvel 
     if self.rect.x <= 0 or self.rect.x > screen.get_width()- self.scale: 
      self.xvel = -self.xvel 
     if self.rect.y <= 0 or self.rect.y > screen.get_height()- self.scale: 
      self.yvel = -self.yvel 
while keep_going: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      keep_going = False 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      if pygame.mouse.get_pressed()[0]:  # Left mouse button, draw 
       mousedown = True 
      elif pygame.mouse.get_pressed()[2]: # Right mouse button, pop 
       pos = pygame.mouse.get_pos() 
       clicked_smileys = [s for s in sprite_list if s.rect.collidepoint(pos)] 
       sprite_list.remove(clicked_smileys) 
     if len(clicked_smileys)> 0: 
      pop.play() 
      count_popped += len(clicked_smileys) 
     if event.type == pygame.MOUSEBUTTONUP: 
       mousedown = False 
    screen.fill(BLACK) 

答えて

1

clicked_smileys = [s for s in sprite_list if s.rect.collidepoint(pos)]

clicked_smileys = [] 
for s in sprite_list: 
    if s.rect.collidepoint(pos): 
     clicked_smileys.append(s) 
+0

真のlist comprehension形です!Pythonは本当に便利です。 – Inteyerry

関連する問題