title: 2D sprite face the direction it's moving in Pygame
body: <p>I'm just learning Python and Pygame and have been working on a very simple game where you play a mouse that has to avoid snakes that spawn every 8 seconds and roam across the screen. Right now I'm trying to get my mouse sprite to face the direction it's moving. The image has the mouse facing down, and I've tried a couple ideas on how to get it to face the right direction but nothing has worked, was hoping someone here could help me out.</p>
  
  <p>Here's the code, no collision detection just yet but the basic code for the mouse is still there.</p>
  
  <pre><code>#!/usr/bin/python
  
  #Imports
  import os, sys, math
  import pygame, random
  from pygame.locals import *
  
  #Sets path for game images
  main_dir = os.path.split(os.path.abspath(__file__))[0]
  data_dir = os.path.join(main_dir, 'data')
  
  pygame.init()
  
  #Main screen and background
  width, height = 1000, 800
  screen = pygame.display.set_mode((width, height))
  pygame.display.set_caption("Don't Get Eaten")
  background = pygame.image.load(os.path.join(data_dir, 'liquid.bmp'))
  background = pygame.transform.scale(background, (width, height)) 
  screen.blit(background, (0,0))
  
  #Game time setup
  clock = pygame.time.Clock()
  pygame.time.set_timer(USEREVENT+1, 8000)
  
  #Functions to assist in image loading
  def load_image(file):
      file = os.path.join(data_dir, file)
      surface = pygame.image.load(file)
      return surface.convert_alpha()
  
  def load_images(*files):
      imgs = []
      for file in files:
          imgs.append(load_image(file))
      return imgs
  
  #Class for player mouse
  class Mouse(pygame.sprite.Sprite):
      speed = 6
      def __init__(self):
          image = load_image('mouse.png')
          image = pygame.transform.scale(image, (60,60))
          pygame.sprite.Sprite.__init__(self,self.groups)
          self.image = image
          self.rect = self.image.get_rect()
          surface = pygame.display.get_surface()
          self.rect.center = (width/2, height/2)
          self.area = surface.get_rect()
  
      def movex(self, directionx):
          self.rect.move_ip(directionx*self.speed, 0)
  
      def movey(self, directiony):
          self.rect.move_ip(0, -directiony*self.speed)        
  
      def update(self):
          pass
  
  #Class for the snake enemy
  class Snake(pygame.sprite.Sprite):
  
      def __init__(self):
          pygame.sprite.Sprite.__init__(self,self.groups)
          image = load_image(random.choice(['snake1.png','snake2.png','snake3.png']))
          image = pygame.transform.scale(image, (85,85))
          self.image = image
          self.rect = self.image.get_rect()
          surface = pygame.display.get_surface()
          self.area = surface.get_rect()
          self.movex = random.randint(1,5)
          self.movey = random.randint(1,5)
  
      def update(self):
          self.rect = self.rect.move(self.movex,self.movey)
          if self.rect.left &lt; self.area.left or self.rect.right &gt; self.area.right:
              self.movex = -self.movex
              newpos = self.rect.move((self.movex, 0))
              self.image = pygame.transform.flip(self.image, 1, 0)
              self.rect = newpos
          if self.rect.top &lt; self.area.top or self.rect.bottom &gt; self.area.bottom:
              self.movey = -self.movey
              newpos = self.rect.move((0, self.movey))
              self.rect = newpos
  
  
  
  
  #Pregame setup
  allsprites = pygame.sprite.Group()
  snakesprites = pygame.sprite.Group()
  mousesprites = pygame.sprite.Group()
  Snake.groups = allsprites, snakesprites
  Mouse.groups = allsprites, mousesprites
  mouse = Mouse()
  snake = Snake()
  
  def main():
      #Main game loop
      while 1:    
  
          #Setup and blits        
          seconds = clock.tick()/1000.0
          screen.blit(background, (0,0))
  
          #Game events
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  sys.exit()
              elif event.type == USEREVENT+1:
                  Snake()
          keystate = pygame.key.get_pressed()
  
          directionx = keystate[K_RIGHT] - keystate[K_LEFT]
          directiony = keystate[K_UP] - keystate[K_DOWN]
          mouse.movex(directionx)
          mouse.movey(directiony)
  
          allsprites.update()
          allsprites.draw(screen)
          pygame.display.update()
          pygame.display.flip
          clock.tick(60)
  
  
  
  if __name__ == '__main__':
      main()
  </code></pre>