Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- WIDTH = 1100
- HEIGHT = 690
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("ZOMBIES VS KNITS")
- bg = pygame.image.load('RAMI/BG_02.png')
- clock = pygame.time.Clock()
- playergroup = pygame.sprite.Group() # Alwas creat group for different kinds og spriteobject. This helps alot with collison/hit tests!
- enemygroup = pygame.sprite.Group()
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- 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']
- 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']
- 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',]
- 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',]
- self.image = pygame.image.load(self.idle_imagefiles[0]).convert_alpha() #Setting the starting image to the first idle image.
- 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.
- self.rect.x = WIDTH / 2 # center the sprite on screen
- self.rect.bottom = HEIGHT - 20 # 20 pixels from the bottom of screen
- self.speed = 5
- self.action = 'idle' # Start as Idle
- self.direction = 'right'
- self.imagenumber = 0
- self.idle_images = []
- self.run_images = []
- self.walk_images = []
- self.jump_images = []
- for image in self.idle_imagefiles: #loading and converting images for faster display
- self.idle_images.append(pygame.image.load(image).convert_alpha())
- for image in self.run_imagefiles: #loading and converting images for faster display
- self.run_images.append(pygame.image.load(image).convert_alpha())
- for image in self.walk_imagefiles: #loading and converting images for faster display
- self.walk_images.append(pygame.image.load(image).convert_alpha())
- for image in self.jump_imagefiles: #loading and converting images for faster display
- self.jump_images.append(pygame.image.load(image).convert_alpha())
- def setAction(self,action):
- if action != self.action:
- self.action = action
- self.imagenumber = 0 # If the action has changed start a new imagenumber count.
- def setDirection(self,direction):
- if direction != self.direction:
- self.direction = direction
- self.imagenumber = 0 # If the direction has changed start a new imagenumber count.
- def update(self): ###### DO ALL THE MAGIC HERE! #######
- if self.action == 'idle':
- self.image = self.idle_images[self.imagenumber//3]
- self.imagenumber = (self.imagenumber + 1) % 27
- if self.action == 'walk':
- self.image = self.walk_images[self.imagenumber//3]
- self.imagenumber = (self.imagenumber + 1) % 27
- if self.direction == 'right':
- self.rect.x += self.speed # move the character to the right
- else:
- self.rect.x -= self.speed # move the character to the left.
- if self.action == 'run':
- self.image = self.run_images[self.imagenumber//3]
- self.imagenumber = (self.imagenumber + 1) % 27
- if self.direction == 'right':
- self.rect.x += self.speed * 2# move the character to the right and quicker!
- else:
- self.rect.x -= self.speed * 2 # move the character to the left and quicker!
- self.image = pygame.transform.flip(self.image, True, False) # Need to flip the image
- if self.action == 'jump':
- self.image = self.jump_images[self.imagenumber//3]
- self.imagenumber = (self.imagenumber + 1) % 27
- if self.direction == 'right':
- self.rect.x += self.speed
- else:
- self.rect.x -= self.speed
- if self.imagenumber == 0: # Set action to idle after a completed jump
- self.action = 'idle'
- if self.direction == 'left':
- self.image = pygame.transform.flip(self.image, True, False) # Need to flip the image
- ######################### DONE WITH PLAYER CLASS ##############################
- man = Player() # creating the man object.
- playergroup.add(man) #add the man to the playergroup. - See advantage in main loop.
- run = True
- ########################## MAIN LOOP OF GAME ##################################
- while run:
- clock.tick(27)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_RIGHT]:
- man.setAction('walk')
- man.setDirection('right')
- if keys[pygame.K_LEFT]:
- man.setAction('walk')
- man.setDirection('left')
- if keys[pygame.K_SPACE]:
- man.setAction('jump')
- playergroup.update() #updates and animates ALL objects in playergroup
- screen.blit(bg,(0,0))
- playergroup.draw(screen) # draws all objects in playergroup.
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement