0

My program allows the an image to follow my mouse cursor but I am unable to draw the circle with the the Attack method because i have to suface.fill in the move method ( the move method is followMeLittleBoy) I can get the circle to draw for a split second but only while moving and it just barley. This is my full code other than my imports and such

class Hero ():

    def __init__(self):
        self.dead = False
        pygame.mouse.set_visible(False)
        self.X_MOVE_AMT = 5
        self.Y_MOVE_AMT = 5
        self.space = pygame.image.load ("hero_sprite.jpg")
        self.spaceRect = self.space.get_rect()
        self.spaceRect.topleft = (100,100)
        surface.blit (self.space, self.spaceRect)
        pygame.display.update()

    def followMeLittleBoy (self):
        amntTuple = pygame.mouse.get_pos()
        self.X_MOVE_AMT = math.ceil((amntTuple[0] - self.spaceRect.left)*0.2)
        self.Y_MOVE_AMT = math.ceil((amntTuple[1] - self.spaceRect.top)*0.2)
        self.spaceRect.left += self.X_MOVE_AMT
        self.spaceRect.top += self.Y_MOVE_AMT
        surface.blit (self.space, self.spaceRect)
        pygame.display.update()


    def Attack (self):
        surface.fill ((255,255,255))
        amntTuple = pygame.mouse.get_pos()
        pygame.draw.circle(surface, pygame.Color(0,0,255), amntTuple, 20, 2)
        surface.blit (self.space, self.spaceRect)





var = Hero ()

while True:
    surface.fill((255,255,255))
    for event in pygame.event.get():
        var.followMeLittleBoy ()
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                var.Attack()
4

2 回答 2

1

上一个问题中,我建议您在事件期间更新状态,并且每次迭代仅从主循环调用一次绘制例程,这对于surface.fill调用特别重要。

现在我强烈建议遵循这种方法,否则这种问题会不断出现。

于 2013-06-10T18:54:08.467 回答
0

您需要解决的问题很少。

  1. var.followMeLittleBoy()应该每个循环调用一次,而不是每个事件。

  2. 你应该有一个单独的方法来在你的 Hero 类中绘图。

  3. 每个循环只调用一次 pygame.display.update()。

我不确定您要完成什么,但您可以创建一个要绘制的圆圈列表,当您按空格键时,一个圆圈会添加到列表中。

然后你可以循环你的圈子列表,并在不消失的情况下绘制它们中的每一个。

于 2013-06-10T18:08:06.867 回答