title: Spritesheet help in pygame
body: <p>I am currently working on a game, trying to improve my beginner python skills. My game is functioning at its most basic level - I can't bring myself to move forward in its development until I can animate my sprite. It's given me the worst headache -- induced by hours of staring at other peoples code, copy and pasting to no avail, and watching countless youtube videos. For some reason, I just cant seem to understand the basic concept of how to strip images from a spritesheet. I've seen the pygame website for it, I just don't understand it. I was following along with kids can code, but his spritesheet had an 'xml'? file attached so he could just copy and paste the coordinates. My spritesheet is 192x192 pixels so I was trying to load/split images by coordinates separated by 48. For example - (0,0,48,0). I was assuming these variables were  x, y, previousSpriteEndingx, previousSpriteEndingy by his example, and also because that's just the only thing I could get to make sense. I grouped each sprite location into lists of corresponding direction - up, down, left, and right. (I didnt want to translate and flip my L/R, but now that I'm thinking about it, would this make my game faster? Since it wouldn't have to load 4 new images?)</p>
  
  <p>I finally got it to actually run, but instead of showing my sprite, it's just a black box that increases in width as it moves, but looks like it's too large to be my sprite. So I know I did something wrong with the coordinates (The box disappears when I press down). If someone could give me a working example that I could copy and paste to run and dissect, explain it so that a beginner could easily understand, or (I saved the best for last) implement something into the code pasted below, I would be forever grateful. Last time I posted here I got great help, and I'm hoping to replicate those results. This is a link to my SpriteSheet
  <a href="https://ibb.co/eZbd2G" rel="nofollow noreferrer">https://ibb.co/eZbd2G</a></p>
  
  <pre><code>import pygame
  
  WIDTH = 1000
  HEIGHT = 700
  FPS = 60
  
  WHITE = (255, 255, 255)
  BLACK = (0, 0, 0)
  RED = (255, 0, 0)
  GREEN = (0, 255, 0)
  BLUE = (0, 0, 255)
  
  pygame.init()
  pygame.mixer.init()
  screen = pygame.display.set_mode((WIDTH, HEIGHT))
  pygame.display.set_caption("Game")
  clock = pygame.time.Clock()
  
  class Player(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          width = 25
          height = 25
          image = pygame.image.load("CharacterSprite.png")
          self.image = pygame.transform.scale(image, (width, height))
          self.image.set_colorkey(BLACK)
          self.rect = self.image.get_rect()
          self.rect.centerx = WIDTH / 2 - 480   #center of rectangle
          self.rect.bottom = HEIGHT - 5  #pixels up from the bottom
          self.speedx = 0
          self.speedy = 0
          self.walkingright = []
          self.walkingleft = []
          self.walkingup = []
          self.walkingdown = []
          self.direction = 'R'
  
  
      #Facing Down
      sprite_sheet = SpriteSheet('CharacterSprite.png')
      image = sprite_sheet.get_image(0,0,48,0)
      self.walkingdown.append(image) 
      image = sprite_sheet.get_image(48,0,96,0)
      self.walkingdown.append(image)
      image = sprite_sheet.get_image(96,0,144,0)
      self.walkingdown.append(image)
      image = sprite_sheet.get_image(144,0,192,0)
      self.walkingdown.append(image)
  
  
      #Facing Up
      image = sprite_sheet.get_image(192,144,48,144)
      self.walkingup.append(image) 
      image = sprite_sheet.get_image(48,144,96,144)
      self.walkingup.append(image)
      image = sprite_sheet.get_image(96,144,144,144)
      self.walkingup.append(image)
      image = sprite_sheet.get_image(144,144,192,144)
      self.walkingup.append(image)
  
  
      #Facing Right
      image = sprite_sheet.get_image(192,96,48,96)
      self.walkingright.append(image)
      image = sprite_sheet.get_image(48,96,96,96)
      self.walkingright.append(image)
      image = sprite_sheet.get_image(96,96,144,96)
      self.walkingright.append(image)
      image = sprite_sheet.get_image(144,96,192,96)
      self.walkingright.append(image)
  
      #FacingLeft
      image = sprite_sheet.get_image(192,48,48,48)
      self.walkingleft.append(image)
      image = sprite_sheet.get_image(48,48,96,48)
      self.walkingleft.append(image)
      image = sprite_sheet.get_image(96,48,144,48)
      self.walkingleft.append(image)
      image = sprite_sheet.get_image(144,48,192,48)
      self.walkingleft.append(image)
  
  
  def update(self):
      pos = self.rect.x
      if self.direction == "R":
          frame = (pos // 30) % len(self.walkingright)
          self.image = self.walkingright[frame]
      if self.direction == "L":
          frame = (pos // 30) % len(self.walkingleft)
          self.image = self.walkingleft[frame]
      if self.direction == "U":
          frame = (pos // 30) % len(self.walkingup)
          self.image = self.walkingup[frame]
      if self.direction == "D":
          frame = (pos // 30) % len(self.walkingdown)
          self.image = self.walkingdown[frame]
  
      self.speedx = 0 #Need these to make sure
      self.speedy = 0 #Sprite stops moving on keyup
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
          self.speedx = -5
          self.direction = 'L'
      if keystate[pygame.K_RIGHT]:
          self.speedx = 5
          self.direction = 'R'
      if keystate[pygame.K_UP]:
          self.speedy = -5
          self.direction = 'U'
      if keystate[pygame.K_DOWN]:
          self.speedy = 5
          self.direction = 'D'
      self.rect.x += self.speedx
      self.rect.y += self.speedy
  
      #Set Walls for Width and Height
      if self.rect.right &gt; WIDTH:
          self.rect.rect = WIDTH
      if self.rect.left &lt; 0:
          self.rect.left = 0
      if self.rect.top &lt; 0:
          self.rect.top = 0
      if self.rect.bottom &gt; HEIGHT:
          self.rect.bottom = HEIGHT
  
  
  class SpriteSheet(object):
      def __init__(self, file_name):
          self.sprite_sheet = pygame.image.load(file_name)
      def get_image(self, x, y, width, height):
          image = pygame.Surface([width, height])
          image.blit(self.sprite_sheet, (0,0), (x, y, width, height))
          image.set_colorkey(BLUE)
          return image
  
  def update(self):
      if self.rect.right &gt; WIDTH:
          self.rect.right = WIDTH
      if self.rect.top &lt; 0:
          self.rect.top = 0
      if self.rect.bottom &gt; HEIGHT:
          self.rect.bottom = HEIGHT
      self.rect.x += self.speedx
      #kill if it goes off screen
      if self.rect.left &gt; WIDTH:
          self.kill()  
  
  #Loading Graphics
  all_sprites = pygame.sprite.Group()
  player = Player()
  all_sprites.add(player)
  projectiles = pygame.sprite.Group()
  
  #Spawn x amount of mobs, add to all sprites and mobs
  running = True
  while running:
      clock.tick(FPS)
  for event in pygame.event.get():
      if event.type == pygame.QUIT:
          running = False
      if event.type == pygame.KEYDOWN:
          if event.key == pygame.K_SPACE:
              player.shoot()
  
   #Update Game Loop           
  all_sprites.update()
  screen.fill(WHITE)
  all_sprites.draw(screen)
  pygame.display.flip()
  pygame.quit()
  </code></pre>