Skip to content
Advertisement

‘event’ has no “key” attribute

enter image description here ** this is a part of a code **

    def _cheak_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys(exit)

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = True
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = True
            
            elif event.key == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = False
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = False 

** error shows that: 38, in _cheak_events elif event.key == pygame.KEYDOWN: AttributeError: ‘Event’ object has no attribute ‘key’

Advertisement

Answer

Replace all event.key with event.type

Advertisement