RootOfTheNull

Python [pygame] 17 Scrolling Platformer

Mar 8th, 2014
361
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.  
  12.         self.image.fill( color )
  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.     def update( self, collidable = pygame.sprite.Group(), event = None ):
  38.  
  39.         self.experience_gravity()
  40.  
  41.         self.rect.x += self.hspeed
  42.         collision_list = pygame.sprite.spritecollide( self, collidable, False )
  43.  
  44.         for collided_object in collision_list:
  45.  
  46.             if ( self.hspeed > 0 ):
  47.                
  48.                 self.rect.right = collided_object.rect.left
  49.  
  50.             elif ( self.hspeed < 0 ):
  51.                
  52.                 self.rect.left = collided_object.rect.right
  53.  
  54.         self.rect.y += self.vspeed
  55.         collision_list = pygame.sprite.spritecollide( self, collidable, False )
  56.  
  57.         for collided_object in collision_list:
  58.  
  59.             if ( self.vspeed > 0 ):
  60.                
  61.                 self.rect.bottom = collided_object.rect.top
  62.                 self.vspeed = 0
  63.  
  64.             elif ( self.vspeed < 0 ):
  65.                
  66.                 self.rect.top = collided_object.rect.bottom
  67.                 self.vspeed = 0
  68.  
  69.         if not ( event == None ):
  70.             if ( event.type == pygame.KEYDOWN ):
  71.                 if ( event.key == pygame.K_LEFT ):
  72.  
  73.                     self.hspeed = -self.speed
  74.  
  75.                 if ( event.key == pygame.K_RIGHT ):
  76.                    
  77.                     self.hspeed = self.speed
  78.  
  79.                 if ( event.key == pygame.K_UP ):
  80.                     if ( len( collision_list ) >= 1 ):
  81.                         self.vspeed = -( self.speed )*2
  82.  
  83.                
  84.             if ( event.type == pygame.KEYUP ):
  85.                 if ( event.key == pygame.K_LEFT ):
  86.                     if ( self.hspeed < 0 ): self.hspeed = 0
  87.  
  88.                 if ( event.key == pygame.K_RIGHT ):
  89.                     if ( self.hspeed > 0 ): self.hspeed = 0
  90.  
  91.  
  92.     def experience_gravity( self, gravity = .35 ):
  93.         if ( self.vspeed == 0 ): self.vspeed = 1
  94.         else: self.vspeed += gravity
  95.  
  96.  
  97. class Block( pygame.sprite.Sprite ):
  98.  
  99.     def __init__( self, x, y, width, height, color = blue ):
  100.  
  101.         super( Block, self ).__init__()
  102.         self.image = pygame.Surface( ( width, height ) )
  103.  
  104.         self.image.fill( color )
  105.  
  106.         self.rect = self.image.get_rect()
  107.  
  108.         self.rect.x = x
  109.         self.rect.y = y
  110.  
  111. class Level( object ):
  112.  
  113.     def __init__( self, player_object ):
  114.  
  115.         self.object_list = pygame.sprite.Group()
  116.  
  117.         self.player_object = player_object
  118.  
  119.         self.player_start = self.player_start_x, self.player_start_y = \
  120.                             0, 0
  121.  
  122.         self.world_shift_x = self.world_shift_y = 0
  123.  
  124.         self.left_viewbox = window_width/2 - window_width/8
  125.         self.right_viewbox = window_width/2 + window_width/10
  126.  
  127.         self.up_viewbox = window_height/5
  128.         self.down_viewbox = window_height/4  + window_height/12
  129.  
  130.     def update( self ):
  131.  
  132.         self.object_list.update()
  133.  
  134.     def draw( self, window ):
  135.  
  136.         window.fill( white )
  137.  
  138.         self.object_list.draw( window )
  139.  
  140.     def shift_world( self, shift_x, shift_y ):
  141.  
  142.         self.world_shift_x += shift_x
  143.         self.world_shift_y += shift_y
  144.  
  145.         for each_object in self.object_list:
  146.             each_object.rect.x += shift_x
  147.             each_object.rect.y += shift_y
  148.  
  149.     def run_viewbox( self ):
  150.  
  151.         if ( self.player_object.rect.x <= self.left_viewbox ):
  152.             view_difference = self.left_viewbox - self.player_object.rect.x
  153.             self.player_object.rect.x = self.left_viewbox
  154.             self.shift_world( view_difference, 0 )
  155.  
  156.         if ( self.player_object.rect.x >= self.right_viewbox ):
  157.             view_difference = self.right_viewbox - self.player_object.rect.x
  158.             self.player_object.rect.x = self.right_viewbox
  159.             self.shift_world( view_difference, 0 )
  160.  
  161.         if ( self.player_object.rect.y <= self.up_viewbox ):
  162.             view_difference = self.up_viewbox - self.player_object.rect.y
  163.             self.player_object.rect.y = self.up_viewbox
  164.             self.shift_world( 0, view_difference )
  165.  
  166.         if ( self.player_object.rect.y >= self.down_viewbox ):
  167.             view_difference = self.down_viewbox - self.player_object.rect.y
  168.             self.player_object.rect.y = self.down_viewbox
  169.             self.shift_world( 0, view_difference )
  170.  
  171. class Level_01( Level ):
  172.  
  173.     def __init__( self, player_object ):
  174.  
  175.         super( Level_01, self ).__init__( player_object )
  176.  
  177.         self.player_start = self.player_start_x, self.player_start_y = \
  178.                             100, 0
  179.  
  180.         level = [
  181.             # [ x, y, width, height, color ]
  182.             [ 2, 124, 365, 47, black ],
  183.             [ 200, 424, 280, 47, black ],
  184.         ]
  185.  
  186.         for block in level:
  187.             block = Block( block[0], block[1], block[2], block[3], block[4] )
  188.             self.object_list.add( block )
  189.  
  190.  
  191. if ( __name__ == "__main__" ):
  192.     pygame.init()
  193.  
  194.     window_size = window_width, window_height = 640, 480
  195.     window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
  196.  
  197.     pygame.display.set_caption( "Subscribe!" )
  198.  
  199.     clock = pygame.time.Clock()
  200.     frames_per_second = 60
  201.  
  202.  
  203.  
  204.     active_object_list = pygame.sprite.Group()
  205.     player = Player()
  206.     player.set_position( 40, 40 )
  207.  
  208.     active_object_list.add( player )
  209.  
  210.     level_list = []
  211.     level_list.append( Level_01( player ) )
  212.  
  213.     current_level_number = 0
  214.     current_level = level_list[ current_level_number ]
  215.  
  216.     player.set_level( current_level )
  217.  
  218.  
  219.  
  220.  
  221.     running = True
  222.     while ( running ):
  223.         for event in pygame.event.get():
  224.             if ( event.type == pygame.QUIT ) or \
  225.             ( event.type == pygame.KEYDOWN and \
  226.             ( event.key == pygame.K_ESCAPE or event.key == pygame.K_q ) ):
  227.                 running = False        
  228.  
  229.  
  230.  
  231.         # Update functions
  232.  
  233.         player.update( current_level.object_list, event )
  234.         event = None
  235.  
  236.         current_level.update()
  237.  
  238.         # Logic Testing
  239.  
  240.         current_level.run_viewbox()
  241.  
  242.         # Draw everything
  243.  
  244.         current_level.draw( window )
  245.         active_object_list.draw( window )
  246.  
  247.         # Delay Framerate
  248.  
  249.         clock.tick( frames_per_second )
  250.  
  251.         # Update the screen
  252.  
  253.         pygame.display.update()
  254.  
  255.     pygame.quit()
RAW Paste Data