trodland

Help - pygame facebook - M. Otsmane - complete

Apr 6th, 2020
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3. screen = pygame.display.set_mode((1080,720))
  4.  
  5. background = pygame.image.load('assets/bg.jpg').convert()
  6.  
  7. class Player(pygame.sprite.Sprite):
  8.     def __init__(self):
  9.         super().__init__()
  10.         self.health = 100
  11.         self.maxhealt = 100
  12.         self.attack = 8
  13.         self.velocity = 5
  14.         self.image = pygame.image.load('assets/player.png').convert_alpha()
  15.         self.rect = self.image.get_rect()
  16.         self.rect.x = 400
  17.         self.rect.y = 500
  18.        
  19.     def move_right(self):
  20.         self.rect.x += self.velocity
  21.    
  22.     def move_left(self):
  23.         self.rect.x -= self.velocity
  24.        
  25.     def update(self):
  26.         # for future use. Here you can add automatic movement / animation / collision detection......
  27.         pass
  28.        
  29. # ---- PLAYER CLASS DONE -------
  30.  
  31. player = Player() #Creating a playerobject
  32.  
  33.        
  34.  
  35. while running:
  36.     screen.blit(background,(0,-200))
  37.    
  38.     player.update() #for future use, for instance animation
  39.     player.draw()
  40.  
  41.     pressed = pygame.key.get_pressed()
  42.  
  43.     if pressed[pygame.K_LEFT]:
  44.         game.player.move_left()
  45.     if pressed[pygame.K_RIGHT]:
  46.         game.player.move_right()
  47.        
  48.     pygame.display.flip()
  49.    
  50.    
  51.    for event in pygame.event.get():
  52.         if event.type == pygame.QUIT:
  53.             running = False
  54.             pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment