Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Animated moving sprite

trodland Apr 10th, 2020 214 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. pygame.init()
  3. WIDTH = 1100
  4. HEIGHT = 690
  5.  
  6. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  7. pygame.display.set_caption("ZOMBIES  VS  KNITS")
  8. bg = pygame.image.load('RAMI/BG_02.png')
  9. clock = pygame.time.Clock()
  10.  
  11. playergroup = pygame.sprite.Group()  # Alwas creat group for different kinds og spriteobject. This helps alot with collison/hit tests!
  12. enemygroup = pygame.sprite.Group()
  13.  
  14. class Player(pygame.sprite.Sprite):
  15.     def __init__(self):
  16.         pygame.sprite.Sprite.__init__(self)
  17.         self.idle_imagefiles = ['RAMI/Idle (1).png','RAMI/Idle (2).png','RAMI/Idle (3).png','RAMI/Idle (4).png','RAMI/Idle (5).png','RAMI/Idle (6).png','RAMI/Idle (7).png','RAMI/Idle (8).png','RAMI/Idle (9).png','RAMI/Idle (10).png']
  18.         self.run_imagefiles = ['RAMI/Run (1).png','RAMI/Run (2).png','RAMI/Run (3).png','RAMI/Run (4).png','RAMI/Run (5).png','RAMI/Run (6).png','RAMI/Run (7).png','RAMI/Run (8).png','RAMI/Run (9).png','RAMI/Run (10).png']
  19.         self.walk_imagefiles = ['RAMI/Walk (1).png','RAMI/Walk (2).png','RAMI/Walk (3).png','RAMI/Walk (4).png','RAMI/Walk (5).png','RAMI/Walk (6).png','RAMI/Walk (7).png','RAMI/Walk (8).png','RAMI/Walk (9).png','RAMI/Walk (10).png',]
  20.         self.jump_imagefiles = ['RAMI/Jump (1).png','RAMI/Jump (2).png','RAMI/Jump (3).png','RAMI/Jump (4).png','RAMI/Jump (5).png','RAMI/Jump (6).png','RAMI/Jump (7).png','RAMI/Jump (8).png','RAMI/Jump (9).png','RAMI/Jump (10).png',]
  21.        
  22.         self.image = pygame.image.load(self.idle_imagefiles[0]).convert_alpha()  #Setting the starting image to the first idle image.
  23.        
  24.         self.rect = self.image.get_rect() # Putting the image inside a rect. The rect is used to tell where the image will be placed on screen.
  25.         self.rect.x = WIDTH / 2         # center the sprite on screen
  26.         self.rect.bottom = HEIGHT - 20  # 20 pixels from the bottom of screen
  27.        
  28.         self.speed = 5
  29.         self.action = 'idle'  # Start as Idle
  30.         self.direction = 'right'
  31.         self.imagenumber = 0
  32.        
  33.         self.idle_images = []
  34.         self.run_images = []
  35.         self.walk_images = []
  36.         self.jump_images = []
  37.        
  38.         for image in self.idle_imagefiles: #loading and converting images for faster display
  39.             self.idle_images.append(pygame.image.load(image).convert_alpha())
  40.            
  41.         for image in self.run_imagefiles: #loading and converting images for faster display
  42.             self.run_images.append(pygame.image.load(image).convert_alpha())
  43.            
  44.         for image in self.walk_imagefiles: #loading and converting images for faster display
  45.             self.walk_images.append(pygame.image.load(image).convert_alpha())
  46.            
  47.         for image in self.jump_imagefiles: #loading and converting images for faster display
  48.             self.jump_images.append(pygame.image.load(image).convert_alpha())
  49.  
  50.     def setAction(self,action):
  51.         if action != self.action:
  52.             self.action = action
  53.             self.imagenumber = 0 # If the action has changed start a new imagenumber count.
  54.        
  55.     def setDirection(self,direction):
  56.         if direction != self.direction:
  57.             self.direction = direction
  58.             self.imagenumber = 0 # If the direction has changed start a new imagenumber count.
  59.        
  60.     def update(self):  ###### DO ALL THE MAGIC HERE! #######
  61.         if self.action == 'idle':
  62.             self.image = self.idle_images[self.imagenumber//3]
  63.             self.imagenumber = (self.imagenumber + 1) % 27
  64.                      
  65.         if self.action == 'walk':
  66.             self.image = self.walk_images[self.imagenumber//3]
  67.             self.imagenumber = (self.imagenumber + 1) % 27
  68.             if self.direction == 'right':
  69.                 self.rect.x += self.speed # move the character to the right
  70.             else:
  71.                 self.rect.x -= self.speed # move the character to the left.
  72.        
  73.         if self.action == 'run':
  74.             self.image = self.run_images[self.imagenumber//3]
  75.             self.imagenumber = (self.imagenumber + 1) % 27
  76.             if self.direction == 'right':
  77.                 self.rect.x += self.speed * 2# move the character to the right and quicker!
  78.             else:
  79.                 self.rect.x -= self.speed * 2 # move the character to the left and quicker!
  80.                 self.image = pygame.transform.flip(self.image, True, False) # Need to flip the image
  81.        
  82.         if self.action == 'jump':
  83.             self.image = self.jump_images[self.imagenumber//3]
  84.             self.imagenumber = (self.imagenumber + 1) % 27
  85.             if self.direction == 'right':
  86.                 self.rect.x += self.speed
  87.             else:
  88.                 self.rect.x -= self.speed
  89.            
  90.             if self.imagenumber == 0: # Set action to idle after a completed jump
  91.                 self.action = 'idle'
  92.        
  93.        
  94.        
  95.         if self.direction == 'left':
  96.             self.image = pygame.transform.flip(self.image, True, False) # Need to flip the image
  97.  
  98. ######################### DONE WITH PLAYER CLASS ##############################
  99.            
  100. man = Player()  # creating the man object.
  101. playergroup.add(man) #add the man to the playergroup. - See advantage in main loop.
  102.            
  103. run = True
  104.  
  105. ########################## MAIN LOOP OF GAME ##################################
  106.  
  107. while run:
  108.     clock.tick(27)
  109.    
  110.     keys = pygame.key.get_pressed()
  111.        
  112.     if keys[pygame.K_RIGHT]:
  113.         man.setAction('walk')
  114.         man.setDirection('right')
  115.    
  116.     if keys[pygame.K_LEFT]:
  117.         man.setAction('walk')
  118.         man.setDirection('left')
  119.    
  120.     if keys[pygame.K_SPACE]:
  121.         man.setAction('jump')
  122.        
  123.     playergroup.update()  #updates and animates ALL objects in playergroup
  124.     screen.blit(bg,(0,0))
  125.     playergroup.draw(screen) # draws all objects in playergroup.
  126.     pygame.display.update()
  127.    
  128.     for event in pygame.event.get():
  129.         if event.type == pygame.QUIT:
  130.             running = False
  131.  
  132.  
  133. pygame.quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top