python - How to properly get a sprite object in pygame to look at the mouse -


so in program able move spaceship around screen using wasd keys, , works fine. problem here want spaceship sprite rotate based on mouse pointer is, want @ mouse. , kind of got work, wiggles bit when move mouse on screen, doesn't rotate. section angle calculating is, in get_angle() , rotate() methods spaceship.

my code:

import pygame livewires import games, color import math  games.init(screen_width = 700, screen_height = 650, fps = 50)  class create_spaceship(games.sprite):     '''a spaceship controlled arrow keys'''             def update(self):         '''move coordinates'''         key = pygame.key.get_pressed()         dist = 4          if key[pygame.k_w]:             self.y -= dist         if key[pygame.k_s]:             self.y += dist         if key[pygame.k_a]:             self.x -= dist         if key[pygame.k_d]:             self.x += dist          #check see if spaceship oustside boundries         if self.left < 0:             self.left = 0         if self.right > games.screen.width:             self.right = games.screen.width         if self.top < 0:             self.top = 0         if self.bottom > games.screen.height:             self.bottom = games.screen.height          self.get_angle()      def get_angle(self):         '''will angle between mouse , image'''         mousex = games.mouse.x         mousey = games.mouse.y         spaceshipx = self.x         spaceshipy = self.y          self.angle = math.atan2(mousex - spaceshipx, mousey - spaceshipy)          print self.angle     def rotate(self):         '''rotates sprite @ angle found in get_angle()'''         self.angle = self.angle   def main():     screen_background = games.load_image('resized_stars background.png', transparent = false)     games.screen.background = screen_background      spaceship_image = games.load_image('8-bit_spaceship_withbg.png')     spaceship = create_spaceship(image=spaceship_image,                           x = games.mouse.x,                           y = games.mouse.y)     games.screen.add(spaceship)     games.mouse.is_visible = true      games.screen.mainloop()  main() 

use pygame.transform.rotate(spaceship.angle)

https://www.pygame.org/docs/ref/transform.html