Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.51 KB | None | 0 0
  1.  
  2. import pygame
  3.  
  4. # Global constants
  5.  
  6. # Colors
  7. BLACK    = (   0,   0,   0)
  8. WHITE    = ( 255, 255, 255)
  9. BLUE     = (   0,   0, 255)
  10. RED      = ( 255,   0,   0)
  11. GREEN    = (   0, 255,   0)
  12.  
  13. # Screen dimensions
  14. SCREEN_WIDTH  = 800
  15. SCREEN_HEIGHT = 600
  16.  
  17. class Player(pygame.sprite.Sprite):
  18.     """ This class represents the bar at the bottom that the player
  19.        controls. """
  20.  
  21.     # -- Methods
  22.     def __init__(self):
  23.         """ Constructor function """
  24.  
  25.         # Call the parent's constructor
  26.         super().__init__()
  27.  
  28.         # Create an image of the block, and fill it with a color.
  29.         # This could also be an image loaded from the disk.
  30.         width = 40
  31.         height = 60
  32.         self.image = pygame.Surface([width, height])
  33.         self.image.fill(RED)
  34.  
  35.         # Set a referance to the image rect.
  36.         self.rect = self.image.get_rect()
  37.  
  38.         # Set speed vector of player
  39.         self.change_x = 0
  40.         self.change_y = 0
  41.  
  42.         # List of sprites we can bump against
  43.         self.level = None
  44.  
  45.  
  46.     def update(self):
  47.         """ Move the player. """
  48.         # Gravity
  49.         self.calc_grav()
  50.  
  51.         # Move left/right
  52.         self.rect.x += self.change_x
  53.  
  54.         # See if we hit anything
  55.         block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  56.         for block in block_hit_list:
  57.             # If we are moving right,
  58.             # set our right side to the left side of the item we hit
  59.             if self.change_x > 0:
  60.                 self.rect.right = block.rect.left
  61.             elif self.change_x < 0:
  62.                 # Otherwise if we are moving left, do the opposite.
  63.                 self.rect.left = block.rect.right
  64.  
  65.         # Move up/down
  66.         self.rect.y += self.change_y
  67.  
  68.         # Check and see if we hit anything
  69.         block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  70.         for block in block_hit_list:
  71.  
  72.             # Reset our position based on the top/bottom of the object.
  73.             if self.change_y > 0:
  74.                 self.rect.bottom = block.rect.top
  75.             elif self.change_y < 0:
  76.                 self.rect.top = block.rect.bottom
  77.  
  78.             # Stop our vertical movement
  79.             self.change_y = 0
  80.  
  81.     def calc_grav(self):
  82.         """ Calculate effect of gravity. """
  83.         if self.change_y == 0:
  84.             self.change_y = 1
  85.         else:
  86.             self.change_y += .35
  87.  
  88.         # See if we are on the ground.
  89.         if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
  90.             self.change_y = 0
  91.             self.rect.y = SCREEN_HEIGHT - self.rect.height
  92.  
  93.     def jump(self):
  94.         """ Called when user hits 'jump' button. """
  95.  
  96.         # move down a bit and see if there is a platform below us.
  97.         # Move down 2 pixels because it doesn't work well if we only move down 1
  98.         # when working with a platform moving down.
  99.         self.rect.y += 2
  100.         platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  101.         self.rect.y -= 2
  102.  
  103.         # If it is ok to jump, set our speed upwards
  104.         if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
  105.             self.change_y = -10
  106.  
  107.     # Player-controlled movement:
  108.     def go_left(self):
  109.         """ Called when the user hits the left arrow. """
  110.         self.change_x = -6
  111.  
  112.     def go_right(self):
  113.         """ Called when the user hits the right arrow. """
  114.         self.change_x = 6
  115.  
  116.     def stop(self):
  117.         """ Called when the user lets off the keyboard. """
  118.         self.change_x = 0
  119.  
  120. class Platform(pygame.sprite.Sprite):
  121.     """ Platform the user can jump on """
  122.  
  123.     def __init__(self, width, height):
  124.         """ Platform constructor. Assumes constructed with user passing in
  125.            an array of 5 numbers like what's defined at the top of this
  126.            code. """
  127.         super().__init__()
  128.  
  129.         self.image = pygame.Surface([width, height])
  130.         self.image.fill(GREEN)
  131.  
  132.         self.rect = self.image.get_rect()
  133.  
  134. class Level(object):
  135.     """ This is a generic super-class used to define a level.
  136.        Create a child class for each level with level-specific
  137.        info. """
  138.  
  139.     # Lists of sprites used in all levels. Add or remove
  140.     # lists as needed for your game.
  141.     platform_list = None
  142.     enemy_list = None
  143.  
  144.     # Background image
  145.     background = None
  146.  
  147.     def __init__(self, player):
  148.         """ Constructor. Pass in a handle to player. Needed for when moving platforms
  149.            collide with the player. """
  150.         self.platform_list = pygame.sprite.Group()
  151.         self.enemy_list = pygame.sprite.Group()
  152.         self.player = player
  153.  
  154.     # Update everythign on this level
  155.     def update(self):
  156.         """ Update everything in this level."""
  157.         self.platform_list.update()
  158.         self.enemy_list.update()
  159.  
  160.     def draw(self, screen):
  161.         """ Draw everything on this level. """
  162.  
  163.         # Draw the background
  164.         screen.fill(BLUE)
  165.  
  166.         # Draw all the sprite lists that we have
  167.         self.platform_list.draw(screen)
  168.         self.enemy_list.draw(screen)
  169.  
  170.  
  171. # Create platforms for the level
  172. class Level_01(Level):
  173.     """ Definition for level 1. """
  174.  
  175.     def __init__(self, player):
  176.         """ Create level 1. """
  177.  
  178.         # Call the parent constructor
  179.         Level.__init__(self, player)
  180.  
  181.         # Array with width, height, x, and y of platform
  182.         level = [[210, 70, 500, 500],
  183.                  [210, 70, 200, 400],
  184.                  [210, 70, 600, 300],
  185.                  ]
  186.  
  187.         # Go through the array above and add platforms
  188.         for platform in level:
  189.             block = Platform(platform[0], platform[1])
  190.             block.rect.x = platform[2]
  191.             block.rect.y = platform[3]
  192.             block.player = self.player
  193.             self.platform_list.add(block)
  194.  
  195. def main():
  196.     """ Main Program """
  197.     pygame.init()
  198.  
  199.     # Set the height and width of the screen
  200.     size = [SCREEN_WIDTH, SCREEN_HEIGHT]
  201.     screen = pygame.display.set_mode(size)
  202.  
  203.     pygame.display.set_caption("Platformer Jumper")
  204.  
  205.     # Create the player
  206.     player = Player()
  207.  
  208.     # Create all the levels
  209.     level_list = []
  210.     level_list.append( Level_01(player) )
  211.  
  212.     # Set the current level
  213.     current_level_no = 0
  214.     current_level = level_list[current_level_no]
  215.  
  216.     active_sprite_list = pygame.sprite.Group()
  217.     player.level = current_level
  218.  
  219.     player.rect.x = 340
  220.     player.rect.y = SCREEN_HEIGHT - player.rect.height
  221.     active_sprite_list.add(player)
  222.  
  223.     #Loop until the user clicks the close button.
  224.     done = False
  225.  
  226.     # Used to manage how fast the screen updates
  227.     clock = pygame.time.Clock()
  228.  
  229.     # -------- Main Program Loop -----------
  230.     while not done:
  231.         for event in pygame.event.get(): # User did something
  232.             if event.type == pygame.QUIT: # If user clicked close
  233.                 done = True # Flag that we are done so we exit this loop
  234.  
  235.             if event.type == pygame.KEYDOWN:
  236.                 if event.key == pygame.K_LEFT:
  237.                     player.go_left()
  238.                 if event.key == pygame.K_RIGHT:
  239.                     player.go_right()
  240.                 if event.key == pygame.K_UP:
  241.                     player.jump()
  242.             print(player.rect.y)
  243.             if event.type == pygame.KEYUP:
  244.                 if event.key == pygame.K_LEFT and player.change_x < 0:
  245.                     player.stop()
  246.                 if event.key == pygame.K_RIGHT and player.change_x > 0:
  247.                     player.stop()
  248.  
  249.         # Update the player.
  250.         active_sprite_list.update()
  251.  
  252.         # Update items in the level
  253.         current_level.update()
  254.  
  255.         # If the player gets near the right side, shift the world left (-x)
  256.         if player.rect.right > SCREEN_WIDTH:
  257.             player.rect.right = SCREEN_WIDTH
  258.  
  259.         # If the player gets near the left side, shift the world right (+x)
  260.         if player.rect.left < 0:
  261.             player.rect.left = 0
  262.  
  263.         # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
  264.         current_level.draw(screen)
  265.         active_sprite_list.draw(screen)
  266.  
  267.         # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
  268.  
  269.         # Limit to 60 frames per second
  270.         clock.tick(60)
  271.  
  272.         # Go ahead and update the screen with what we've drawn.
  273.         pygame.display.flip()
  274.  
  275.     # Be IDLE friendly. If you forget this line, the program will 'hang'
  276.     # on exit.
  277.     pygame.quit()
  278.  
  279. if __name__ == "__main__":
  280.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement