0

in my game I have a class to control the player item witch he can throw. the objective is that when the key is pressed the item should be displayed on screen and then using a while loop its position should be updated until either it collided with an enemy or with the world.

class Fight():
    def __init__(self):
        self.image = pygame.image.load('Wepon.png')
        self.pressed = False
        self.rect = self.image.get_rect()
        self.rect.x = player.rect.x +16
        self.rect.y = player.rect.y +14
        i = 0
        key = pygame.key.get_pressed()
        if (key[pygame.K_x] ) and (self.pressed == False):
            self.pressed =True
        while self.pressed == True:
            screen.blit(self.image,(self.rect.x,self.rect.y))
            self.rect.y = self.rect.y + 1
            self.rect.x = self.rect.x + 1
            i = i + 1
            if i == 200 :
                    self.pressed = False
            print('I am working') 

so the code runs when the key is pressed how ever the item is only displayed on the screen after the i reaches 200 and its on all the positions not just one if that makes sense

  • `screen.blit` just draws to the internal buffer. It's not shown until you call `pygame.display.update`. However, you can't have a loop like this in a GUI. You must update the position and draw the image in discrete frames. In other words, keep track of where the sword is as an attribute, update it just one step (preferrable in a `update` method) and the store the new position as an attribute before the method exit. Then call this `update` method the next frame, over and over, until it has reached its target or you've released the button. – Ted Klein Bergman Mar 19 '22 at 16:20

0 Answers0