Ark Angel
Ark Angel

Reputation: 61

Issues with surface.fill () in pygame

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()

Upvotes: 0

Views: 208

Answers (2)

pmoleri
pmoleri

Reputation: 4449

In a previous question I recommended you to update the state during events and call the draw routines from the main loop only once per iteration, this is specially important for the surface.fill call.

Now I strongly recommend to follow that aproach, otherwise this kind of problems will continue to arise.

Upvotes: 1

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11180

There are few things that you need to fix.

  1. var.followMeLittleBoy() should be called once per loop, not for every event.

  2. You should have a seperate method for drawing in your Hero class.

  3. Call pygame.display.update() only once per loop.

I am not sure what you are trying to accomplish, but you could create a list of circles that are to be drawn, and when you press spacebar, a circle is added to a list.

Then you can loop your circle list,and draw each of them without them dissapearing.

Upvotes: 0

Related Questions