Advertisement
bosrsf04

PyGame

Jan 30th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3. import pygame
  4. import sys
  5.  
  6. from player import Player
  7.  
  8.  
  9. class Game(object):
  10.     '''
  11.    A basic game
  12.    '''
  13.  
  14.     FPS = 30
  15.     '''
  16.    The frame per second cap for the game
  17.    '''
  18.  
  19.     def __init__(self, screen):
  20.         '''
  21.        Start the game
  22.        '''
  23.         # Initialize objects
  24.         clock = pygame.time.Clock()
  25.         player = Player()
  26.         # Main game loop
  27.         while True:
  28.             # Cap at FPS
  29.             dt = clock.tick(Game.FPS)
  30.             # Check for events
  31.             for event in pygame.event.get():
  32.                 # If the window is closed, end the game
  33.                 if event.type == pygame.QUIT:
  34.                     sys.exit(0)
  35.                 # Check for keyboard input
  36.                 if event.type == pygame.KEYDOWN:
  37.                     # Pass the key down event to the player
  38.                     if player.keydown(event):
  39.                         break
  40.                     # Key not handled; Ignore it
  41.                 elif event.type == pygame.KEYUP:
  42.                     # Pass the key up event to the player
  43.                     if player.keyup(event):
  44.                         break
  45.                     # Key not handled; Ignore it
  46.             # Clear the buffer
  47.             screen.fill((200, 200, 200))
  48.             # Update the player state
  49.             player.update(dt / 1000.0)
  50.             # Paint the player
  51.             player.draw(screen)
  52.             # Flip the buffer and screen
  53.             pygame.display.flip()
  54.  
  55. if __name__ == "__main__":
  56.     pygame.init()
  57.     pygame.display.set_caption("Example")
  58.     screen = pygame.display.set_mode((640, 480))
  59.     Game(screen)
  60.  
  61.  
  62. #!/usr/bin/python2
  63.  
  64. import pygame
  65.  
  66.  
  67. class Player(pygame.sprite.Sprite):
  68.     '''
  69.    A player instance
  70.    '''
  71.  
  72.     def __init__(self):
  73.         '''
  74.        Initialize the player
  75.        '''
  76.         # Initialize Parent
  77.         pygame.sprite.Sprite.__init__(self)
  78.         # Create a singleton group for the player and add the player to it
  79.         self.__group = pygame.sprite.GroupSingle()
  80.         self.__group.add(self)
  81.         # Set the image and bounds
  82.         self.image = pygame.image.load("resources/rsffavicon.png")
  83.         self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
  84.         # Initialize key states
  85.         self.left = False
  86.         self.right = False
  87.         self.up = False
  88.         self.down = False
  89.  
  90.     def draw(self, surface):
  91.         '''
  92.        Draw the player on the specified surface
  93.        '''
  94.         self.__group.draw(surface)
  95.  
  96.     def keydown(self, event):
  97.         '''
  98.        Handles a key down event.
  99.        Returns: Whether the key has been handled
  100.        '''
  101.         # Initialize a default return value
  102.         result = False
  103.         # Check to see if we can handle the key
  104.         if event.key in (pygame.K_a, pygame.K_LEFT):
  105.             self.left = True
  106.             result = True
  107.         elif event.key in (pygame.K_d, pygame.K_RIGHT):
  108.             self.right = True
  109.             result = True
  110.         elif event.key in (pygame.K_w, pygame.K_UP):
  111.             self.up = True
  112.             result = True
  113.         elif event.key in (pygame.K_s, pygame.K_DOWN):
  114.             self.down = True
  115.             result = True
  116.         # Return whether the key has been processed
  117.         return result
  118.  
  119.     def keyup(self, event):
  120.         '''
  121.        Handles a key up event.
  122.        Returns: Whether the key has been handled
  123.        '''
  124.         # Initialize a default return value
  125.         result = False
  126.         # Check to see if we can handle the key
  127.         if event.key in (pygame.K_a, pygame.K_LEFT):
  128.             self.left = False
  129.             result = True
  130.         elif event.key in (pygame.K_d, pygame.K_RIGHT):
  131.             self.right = False
  132.             result = True
  133.         elif event.key in (pygame.K_w, pygame.K_UP):
  134.             self.up = False
  135.             result = True
  136.         elif event.key in (pygame.K_s, pygame.K_DOWN):
  137.             self.down = False
  138.             result = True
  139.         # Return whether the key has been processed
  140.         return result
  141.  
  142.     def update(self, dt):
  143.         '''
  144.        Update the player's state
  145.        '''
  146.         if self.left:
  147.             self.rect.x -= 300 * dt
  148.         if self.right:
  149.             self.rect.x += 300 * dt
  150.         if self.up:
  151.             self.rect.y -= 300 * dt
  152.         if self.down:
  153.             self.rect.y += 300 * dt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement