0
是否有可能将雪碧放置在我点击的位置?Pygame-雪碧设置位置与鼠标点击
class sprite_to_place(pygame.sprite.Sprite):
def __init__(self, x_start_position , y_start_position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("a_picture.png")
self.rect = self.image.get_rect()
self.rect.x = x_start_position # x where I clicked
self.rect.y = y_start_position # y where I clicked
当我初始化sprite_to_place时,我会使用pygame.mouse.get_pos()
。
,并在主回路我把它用:
if event.type == pygame.MOUSEBUTTONDOWN:
sprite_to_place_group.draw(gameDisplay)
但我怎么能得到精灵的位置,如果我想改变其位置def update()
? (我用allsprites_group.update()
)
def update(self, startpos=(x_start_position, y_start_position)): # how can I tell the function where the sprite is on the map?
self.pos = [startpos[0], startpos[1]]
self.rect.x = round(self.pos[0] - cornerpoint[0], 0) #x
self.rect.y = round(self.pos[1] - cornerpoint[1], 0) #y
如果我想在我的例子不喜欢它,它说,x_start_position
和y_start_position
没有定义。
谢谢!
好。但是仍然无法设置精灵的位置,我点击了它。在我能检查鼠标在主循环中的位置之前,我必须定义我的Sprite的位置。 – Holla
所以,只需将您的主循环中精灵的位置设置为鼠标位置即可。问题在哪里? – sloth
问题是,我想用鼠标点击鼠标的位置在屏幕上创建精灵。所以如果我正确理解你的解决方案,这个精灵已经放置在地图'pos'的位置上,并且它会被移动。想象一下,在战略游戏中放置一座建筑物,那就是我所需要的。但是,非常感谢帮助我:) – Holla