如何检测单击了哪个图像,或如何添加它们的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"
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"
--
顺便说一句:您的下一步是学习和使用class
and Sprite
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句