RootOfTheNull

Python [pygame] 16 Cleaning and Player Start

Mar 8th, 2014
315
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import pygame
  4. from colors import *
  5.  
  6. class Player( pygame.sprite.Sprite ):
  7.     def __init__( self, color = blue, width = 32, height = 48 ):
  8.  
  9.         super( Player, self ).__init__()
  10.         self.image = pygame.Surface( ( width, height ) )
  11.         self.image.fill( color )
  12.  
  13.         self.set_properites()
  14.  
  15.         self.hspeed = 0
  16.         self.vspeed = 0
  17.  
  18.         self.level = None
  19.  
  20.     def set_properites( self ):
  21.  
  22.         self.rect = self.image.get_rect()
  23.         self.origin_x = self.rect.centerx
  24.         self.origin_y = self.rect.centery
  25.  
  26.         self.speed = 5
  27.  
  28.  
  29.     def set_position( self, x, y ):
  30.         self.rect.x = x - self.origin_x
  31.         self.rect.y = y - self.origin_y
  32.  
  33.     def set_level( self, level ):
  34.         self.level = level
  35.         self.set_position( level.player_start_x, level.player_start_y )
  36.  
  37.  
  38.     def update( self, collidable = pygame.sprite.Group(), event = None ):
  39.  
  40.         self.experience_gravity()
  41.  
  42.         self.rect.x += self.hspeed
  43.         collision_list = pygame.sprite.spritecollide( self, collidable, False )
  44.  
  45.         for collided_object in collision_list:
  46.  
  47.             if ( self.hspeed > 0 ):
  48.                
  49.                 self.rect.right = collided_object.rect.left
  50.  
  51.             elif ( self.hspeed < 0 ):
  52.                
  53.                 self.rect.left = collided_object.rect.right
  54.  
  55.         self.rect.y += self.vspeed
  56.         collision_list = pygame.sprite.spritecollide( self, collidable, False )
  57.  
  58.         for collided_object in collision_list:
  59.  
  60.             if ( self.vspeed > 0 ):
  61.                
  62.                 self.rect.bottom = collided_object.rect.top
  63.                 self.vspeed = 0
  64.  
  65.             elif ( self.vspeed < 0 ):
  66.                
  67.                 self.rect.top = collided_object.rect.bottom
  68.                 self.vspeed = 0
  69.  
  70.         if not ( event == None ):
  71.             if ( event.type == pygame.KEYDOWN ):
  72.                 if ( event.key == pygame.K_LEFT ):
  73.  
  74.                     self.hspeed = -self.speed
  75.  
  76.                 if ( event.key == pygame.K_RIGHT ):
  77.                    
  78.                     self.hspeed = self.speed
  79.  
  80.                 if ( event.key == pygame.K_UP ):
  81.                     if ( len( collision_list ) >= 1 ):
  82.                         self.vspeed = -( self.speed )*2
  83.  
  84.                
  85.             if ( event.type == pygame.KEYUP ):
  86.                 if ( event.key == pygame.K_LEFT ):
  87.                     if ( self.hspeed < 0 ): self.hspeed = 0
  88.  
  89.                 if ( event.key == pygame.K_RIGHT ):
  90.                     if ( self.hspeed > 0 ): self.hspeed = 0
  91.  
  92.  
  93.     def experience_gravity( self, gravity = .35 ):
  94.         if ( self.vspeed == 0 ): self.vspeed = 1
  95.         else: self.vspeed += gravity
  96.  
  97.  
  98. class Block( pygame.sprite.Sprite ):
  99.  
  100.     def __init__( self, x, y, width, height, color = blue ):
  101.  
  102.         super( Block, self ).__init__()
  103.         self.image = pygame.Surface( ( width, height ) )
  104.  
  105.         self.image.fill( color )
  106.  
  107.         self.rect = self.image.get_rect()
  108.  
  109.         self.rect.x = x
  110.         self.rect.y = y
  111.  
  112. class Level( object ):
  113.  
  114.     def __init__( self, player_object ):
  115.  
  116.         self.object_list = pygame.sprite.Group()
  117.  
  118.         self.player_object = player_object
  119.  
  120.         self.player_start = self.player_start_x, self.player_start_y = \
  121.                             0, 0
  122.  
  123.     def update( self ):
  124.  
  125.         self.object_list.update()
  126.  
  127.     def draw( self, window ):
  128.  
  129.         window.fill( white )
  130.  
  131.         self.object_list.draw( window )
  132.  
  133. class Level_01( Level ):
  134.  
  135.     def __init__( self, player_object ):
  136.  
  137.         super( Level_01, self ).__init__( player_object )
  138.  
  139.         self.player_start = self.player_start_x, self.player_start_y = \
  140.                             100, 0
  141.  
  142.         level = [
  143.             # [ x, y, width, height, color ]
  144.             [ 2, 124, 365, 47, black ],
  145.             [ 200, 424, 280, 47, black ],
  146.         ]
  147.  
  148.         for block in level:
  149.             block = Block( block[0], block[1], block[2], block[3], block[4] )
  150.             self.object_list.add( block )
  151.  
  152.  
  153. if ( __name__ == "__main__" ):
  154.     pygame.init()
  155.  
  156.     window_size = window_width, window_height = 640, 480
  157.     window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
  158.  
  159.     pygame.display.set_caption( "Subscribe!" )
  160.  
  161.     clock = pygame.time.Clock()
  162.     frames_per_second = 60
  163.  
  164.  
  165.  
  166.     active_object_list = pygame.sprite.Group()
  167.     player = Player()
  168.     player.set_position( 40, 40 )
  169.  
  170.     active_object_list.add( player )
  171.  
  172.     level_list = []
  173.     level_list.append( Level_01( player ) )
  174.  
  175.     current_level_number = 0
  176.     current_level = level_list[ current_level_number ]
  177.  
  178.     player.set_level( current_level )
  179.  
  180.  
  181.  
  182.  
  183.     running = True
  184.     while ( running ):
  185.         for event in pygame.event.get():
  186.             if ( event.type == pygame.QUIT ) or \
  187.             ( event.type == pygame.KEYDOWN and \
  188.             ( event.key == pygame.K_ESCAPE or event.key == pygame.K_q ) ):
  189.                 running = False        
  190.  
  191.  
  192.  
  193.         # Update functions
  194.  
  195.         player.update( current_level.object_list, event )
  196.         event = None
  197.  
  198.         current_level.update()
  199.  
  200.         # Logic Testing
  201.  
  202.         # Draw everything
  203.  
  204.         current_level.draw( window )
  205.         active_object_list.draw( window )
  206.  
  207.         # Delay Framerate
  208.  
  209.         clock.tick( frames_per_second )
  210.  
  211.         # Update the screen
  212.  
  213.         pygame.display.update()
  214.  
  215.     pygame.quit()
RAW Paste Data