SaveText.Ru

Без имени
  1. import pygame, os
  2. from gifread import *
  3.  
  4. #global
  5. #colors
  6. BLACK = (0, 0, 0)
  7. WHITE = (255, 255, 255)
  8. GREEN = (0, 255, 0)
  9. RED = (255, 0, 0)
  10. BLUE = (0, 0, 255)
  11. #screen
  12. SCREEN_WIDTH = 800
  13. SCREEN_HEIGHT = 600
  14. #directory from file
  15. iconDir = os.path.dirname(__file__)
  16. class Player(pygame.sprite.Sprite):
  17.     #Methods
  18.     def __init__(self):
  19.         #constructor
  20.  
  21.         super().__init__()
  22.  
  23.         # Create an image of the block, and fill it with a color.
  24.         # This could also be an image loaded from the disk.
  25.         self.image = pygame.image.load("%simgshec.gif" % iconDir)
  26.         #self.image = GIFImage("%simgshec.gif" % iconDir)
  27.         width = 40
  28.         height = 60
  29.         self.image = pygame.Surface([width, height])
  30.         self.image.fill(RED)
  31.  
  32.         # Set a referance to the image rect.
  33.         self.rect = self.image.get_rect()
  34.  
  35.         # Set speed vector of player
  36.         self.change_x = 0
  37.         self.change_y = 0
  38.  
  39.         # List of sprites we can bump against
  40.         self.level = None
  41.  
  42.     def update(self):
  43.         """ Move the player. """
  44.         # Gravity
  45.         self.calc_grav()
  46.  
  47.         # Move left/right
  48.         self.rect.x += self.change_x
  49.  
  50.         # See if we hit anything
  51.         block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  52.         for block in block_hit_list:
  53.             # If we are moving right,
  54.             # set our right side to the left side of the item we hit
  55.             if self.change_x > 0:
  56.                 self.rect.right = block.rect.left
  57.             elif self.change_x < 0:
  58.                 # Otherwise if we are moving left, do the opposite.
  59.                 self.rect.left = block.rect.right
  60.  
  61.         # Move up/down
  62.         self.rect.y += self.change_y
  63.  
  64.         # Check and see if we hit anything
  65.         block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  66.         for block in block_hit_list:
  67.  
  68.             # Reset our position based on the top/bottom of the object.
  69.             if self.change_y > 0:
  70.                 self.rect.bottom = block.rect.top
  71.             elif self.change_y < 0:
  72.                 self.rect.top = block.rect.bottom
  73.  
  74.             # Stop our vertical movement
  75.             self.change_y = 0
  76.  
  77.     def calc_grav(self):
  78.         """ Calculate effect of gravity. """
  79.         if self.change_y == 0:
  80.             self.change_y = 1
  81.         else:
  82.             self.change_y += .35
  83.  
  84.         # See if we are on the ground.
  85.         if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
  86.             self.change_y = 0
  87.             self.rect.y = SCREEN_HEIGHT - self.rect.height
  88.  
  89.     def jump(self):
  90.         """ Called when user hits 'jump' button. """
  91.  
  92.         # move down a bit and see if there is a platform below us.
  93.         # Move down 2 pixels because it doesn't work well if we only move down 1
  94.         # when working with a platform moving down.
  95.         self.rect.y += 2
  96.         platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  97.         self.rect.y -= 2
  98.  
  99.         # If it is ok to jump, set our speed upwards
  100.         if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
  101.             self.change_y = -10
  102.  
  103.     # Player-controlled movement:
  104.     def go_left(self):
  105.         """ Called when the user hits the left arrow. """
  106.         self.change_x = -6
  107.  
  108.     def go_right(self):
  109.         """ Called when the user hits the right arrow. """
  110.         self.change_x = 6
  111.  
  112.     def stop(self):
  113.         """ Called when the user lets off the keyboard. """
  114.         self.change_x = 0
  115.  
  116.  
  117. class Platform(pygame.sprite.Sprite):
  118.     """ Platform the user can jump on """
  119.  
  120.     def __init__(self, width, height):
  121.         """ Platform constructor. Assumes constructed with user passing in
  122.             an array of 5 numbers like what's defined at the top of this code.
  123.             """
  124.         super().__init__()
  125.  
  126.         self.image = pygame.Surface([width, height])
  127.         self.image.fill(GREEN)
  128.  
  129.         self.rect = self.image.get_rect()
  130.  
  131.  
  132. class Level():
  133.     """ This is a generic super-class used to define a level.
  134.         Create a child class for each level with level-specific
  135.         info. """
  136.  
  137.     def __init__(self, player):
  138.         """ Constructor. Pass in a handle to player. Needed for when moving
  139.             platforms collide with the player. """
  140.         self.platform_list = pygame.sprite.Group()
  141.         self.enemy_list = pygame.sprite.Group()
  142.         self.player = player
  143.  
  144.         # How far this world has been scrolled left/right
  145.         self.world_shift = 0
  146.  
  147.     # Update everythign on this level
  148.     def update(self):
  149.         """ Update everything in this level."""
  150.         self.platform_list.update()
  151.         self.enemy_list.update()
  152.  
  153.     def draw(self, screen):
  154.         """ Draw everything on this level. """
  155.  
  156.         # Draw the background
  157.         screen.fill(BLUE)
  158.  
  159.         # Draw all the sprite lists that we have
  160.         self.platform_list.draw(screen)
  161.         self.enemy_list.draw(screen)
  162.  
  163.     def shift_world(self, shift_x):
  164.         """ When the user moves left/right and we need to scroll
  165.         everything: """
  166.  
  167.         # Keep track of the shift amount
  168.         self.world_shift += shift_x
  169.  
  170.         # Go through all the sprite lists and shift
  171.         for platform in self.platform_list:
  172.             platform.rect.x += shift_x
  173.  
  174.         for enemy in self.enemy_list:
  175.             enemy.rect.x += shift_x
  176.  
  177.  
  178. # Create platforms for the level
  179. class Level_01(Level):
  180.     """ Definition for level 1. """
  181.  
  182.     def __init__(self, player):
  183.         """ Create level 1. """
  184.  
  185.         # Call the parent constructor
  186.         Level.__init__(self, player)
  187.  
  188.         self.level_limit = -1000
  189.  
  190.         # Array with width, height, x, and y of platform
  191.         level = [[210, 70, 500, 500],
  192.                  [210, 70, 800, 400],
  193.                  [210, 70, 1000, 500],
  194.                  [210, 70, 1120, 280],
  195.                  ]
  196.  
  197.         # Go through the array above and add platforms
  198.         for platform in level:
  199.             block = Platform(platform[0], platform[1])
  200.             block.rect.x = platform[2]
  201.             block.rect.y = platform[3]
  202.             block.player = self.player
  203.             self.platform_list.add(block)
  204.  
  205.  
  206. # Create platforms for the level
  207. class Level_02(Level):
  208.     """ Definition for level 2. """
  209.  
  210.     def __init__(self, player):
  211.         """ Create level 1. """
  212.  
  213.         # Call the parent constructor
  214.         Level.__init__(self, player)
  215.  
  216.         self.level_limit = -1000
  217.  
  218.         # Array with type of platform, and x, y location of the platform.
  219.         level = [[210, 30, 450, 570],
  220.                  [210, 30, 850, 420],
  221.                  [210, 30, 1000, 520],
  222.                  [210, 30, 1120, 280],
  223.                  ]
  224.  
  225.         # Go through the array above and add platforms
  226.         for platform in level:
  227.             block = Platform(platform[0], platform[1])
  228.             block.rect.x = platform[2]
  229.             block.rect.y = platform[3]
  230.             block.player = self.player
  231.             self.platform_list.add(block)
  232.  
  233.  
  234. def main():
  235.     """ Main Program """
  236.     pygame.init()
  237.  
  238.     # Set the height and width of the screen
  239.     size = [SCREEN_WIDTH, SCREEN_HEIGHT]
  240.     screen = pygame.display.set_mode(size)
  241.  
  242.     pygame.display.set_caption("Side-scrolling Platformer")
  243.  
  244.     # Create the player
  245.     player = Player()
  246.  
  247.     # Create all the levels
  248.     level_list = []
  249.     level_list.append(Level_01(player))
  250.     level_list.append(Level_02(player))
  251.  
  252.     # Set the current level
  253.     current_level_no = 0
  254.     current_level = level_list[current_level_no]
  255.  
  256.     active_sprite_list = pygame.sprite.Group()
  257.     player.level = current_level
  258.  
  259.     player.rect.x = 340
  260.     player.rect.y = SCREEN_HEIGHT - player.rect.height
  261.     active_sprite_list.add(player)
  262.  
  263.     # Loop until the user clicks the close button.
  264.     done = False
  265.  
  266.     # Used to manage how fast the screen updates
  267.     clock = pygame.time.Clock()
  268.  
  269.     # -------- Main Program Loop -----------
  270.     while not done:
  271.         for event in pygame.event.get():
  272.             if event.type == pygame.QUIT:
  273.                 done = True
  274.  
  275.             if event.type == pygame.KEYDOWN:
  276.                 if event.key == pygame.K_LEFT:
  277.                     player.go_left()
  278.                 if event.key == pygame.K_RIGHT:
  279.                     player.go_right()
  280.                 if event.key == pygame.K_UP:
  281.                     player.jump()
  282.  
  283.             if event.type == pygame.KEYUP:
  284.                 if event.key == pygame.K_LEFT and player.change_x < 0:
  285.                     player.stop()
  286.                 if event.key == pygame.K_RIGHT and player.change_x > 0:
  287.                     player.stop()
  288.  
  289.         # Update the player.
  290.         active_sprite_list.update()
  291.  
  292.         # Update items in the level
  293.         current_level.update()
  294.  
  295.         # If the player gets near the right side, shift the world left (-x)
  296.         if player.rect.right >= 500:
  297.             diff = player.rect.right - 500
  298.             player.rect.right = 500
  299.             current_level.shift_world(-diff)
  300.  
  301.         # If the player gets near the left side, shift the world right (+x)
  302.         if player.rect.left <= 120:
  303.             diff = 120 - player.rect.left
  304.             player.rect.left = 120
  305.             current_level.shift_world(diff)
  306.  
  307.         # If the player gets to the end of the level, go to the next level
  308.         current_position = player.rect.x + current_level.world_shift
  309.         if current_position < current_level.level_limit:
  310.             player.rect.x = 120
  311.             if current_level_no < len(level_list)-1:
  312.                 current_level_no += 1
  313.                 current_level = level_list[current_level_no]
  314.                 player.level = current_level
  315.  
  316.         #draw below
  317.         current_level.draw(screen)
  318.         active_sprite_list.draw(screen)
  319.  
  320.         #draw above
  321.         #limit to 60 frames per second
  322.         clock.tick(60)
  323.         pygame.display.flip()
  324.     pygame.quit()
  325.  
  326. if __name__ == "__main__":
  327.     main()

Share with your friends:

Print