RootOfTheNull

Python [pygame] 15 Organizing Our Code

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