2014-06-21 2 views
-1

클릭 한 이미지를 감지하는 방법이나 ID를 추가하는 방법은 내가 볼 수있는 모든 예제가 있는데, 나는 운이없는 것으로 검색했다.파이 게임에서 같은 속성을 가진 많은 인스턴스를 만드는 방법

import pygame , random 
from pygame.locals import * 

pygame.init() 

screen = pygame.display.set_mode((800,600),16) 
screen.fill((60,120,160)) 

instances = 10 

while instances > 0: 
    image = pygame.image.load("image.png").convert() 
    image_rect = image.get_rect() 
    image_rect[0] = random.randint(10,700) 
    image_rect[1] = random.randint(10,500) 
    screen.blit(image,image_rect) 
    instances -= 1 

pygame.display.update() 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      exit() 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      if event.button == 1: 
       if image_rect.collidepoint(event.pos): 
        print "image 1 clicked" 
+1

목록을 사용하십시오. 그런 식으로 루프하면 덮어 씁니다. – aIKid

답변

1
instances = [] 

for _ in range(10): 
    image = pygame.image.load("image.png").convert() 
    image_rect = image.get_rect() 
    image_rect.x = random.randint(10,700) 
    image_rect.y = random.randint(10,500) 
    screen.blit(image, image_rect) 
    instances.append((image, image_rect)) 

지금 당신이 목록 instances
의 모든 이미지가 있고 instances[number]

if event.button == 1: 
     for index, (image, image_rect) in enumerate(instances): 
      if image_rect.collidepoint(event.pos): 
       print "image", index, "clicked" 

에 의해 이미지를 얻을 수 있습니다 - BTW

: 당신의 다음 단계는 배우고 사용하는 것입니다 classSprite.

관련 문제