Guest User

Untitled

a guest
Jun 13th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.17 KB | None | 0 0
  1. import pygame
  2.  
  3. BLACK = (0, 0, 0)
  4. WHITE = (255, 255, 255)
  5. BLUE = (0, 0, 255)
  6. GREEN = (0, 255, 0)
  7. RED = (255, 0, 0)
  8. GREY = (160, 160, 160)
  9. PURPLE = (127, 0, 255)
  10.  
  11. class SpriteSheet(object):
  12.     """ Class used to grab images out of a sprite sheet. """
  13.  
  14.     def __init__(self, file_name):
  15.         """ Constructor. Pass in the file name of the sprite sheet. """
  16.  
  17.         # Load the sprite sheet.
  18.         self.sprite_sheet = pygame.image.load(file_name).convert()
  19.  
  20.  
  21.     def get_image(self, x, y, width, height):
  22.         """ Grab a single image out of a larger spritesheet
  23.            Pass in the x, y location of the sprite
  24.            and the width and height of the sprite. """
  25.  
  26.         # Create a new blank image
  27.         image = pygame.Surface([width, height]).convert()
  28.  
  29.         # Copy the sprite from the large sheet onto the smaller image
  30.         image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
  31.  
  32.         # Assuming black works as the transparent color
  33.         image.set_colorkey(BLACK)
  34.  
  35.         # Return the image
  36.         return image
  37.  
  38. class Wall(pygame.sprite.Sprite):
  39.     """ This class represents the walls that confine the player """
  40.  
  41.     def __init__(self, x, y, width, height, color):
  42.         """ Constructor function """
  43.  
  44.         # Call the parent's constructor
  45.         super().__init__()
  46.  
  47.         # Setting width, height, and color of wall
  48.         self.image = pygame.Surface([width, height])
  49.         self.image.fill(color)
  50.  
  51.         # Make our top-left corner the passed-in location.
  52.         self.rect = self.image.get_rect()
  53.         self.rect.y = y
  54.         self.rect.x = x
  55.  
  56. class Enemy(pygame.sprite.Sprite):
  57.     """ This class represents the enemy sprites """
  58.  
  59.     def __init__(self, x, y, width, height, color):
  60.  
  61.         super().__init__()
  62.  
  63.         self.image = pygame.Surface([width, height])
  64.         self.image.fill(color)
  65.  
  66.         self.rect = self.image.get_rect()
  67.         self.rect.y = y
  68.         self.rect.x = x
  69.  
  70.  
  71. class Player(pygame.sprite.Sprite):
  72.     """ This class represents the character you play as """
  73.  
  74.     # Set speed vector
  75.  
  76.     def __init__(self, x, y):
  77.         """ Constructor function """
  78.  
  79.         # Call the parent's constructor
  80.         super().__init__()
  81.         self.change_x = 0
  82.         self.change_y = 0
  83.  
  84.         self.walking_frames_l = []
  85.         self.walking_frames_r = []
  86.         self.walking_frames_u = []
  87.         self.walking_frames_d = []
  88.  
  89.         self.direction = "R"
  90.  
  91.         sprite_sheet = SpriteSheet("character.png")
  92.         image = sprite_sheet.get_image(1, 6, 15, 22)
  93.         self.walking_frames_d.append(image)
  94.         image = sprite_sheet.get_image(17, 6, 15, 22)
  95.         self.walking_frames_d.append(image)
  96.         image = sprite_sheet.get_image(33, 6, 15, 22)
  97.         self.walking_frames_d.append(image)
  98.         image = sprite_sheet.get_image(49, 6, 15, 22)
  99.         self.walking_frames_d.append(image)
  100.  
  101.         image = sprite_sheet.get_image(0, 69, 15, 23)
  102.         self.walking_frames_u.append(image)
  103.         image = sprite_sheet.get_image(16, 69, 15, 23)
  104.         self.walking_frames_u.append(image)
  105.         image = sprite_sheet.get_image(32, 69, 15, 23)
  106.         self.walking_frames_u.append(image)
  107.         image = sprite_sheet.get_image(48, 69, 15, 23)
  108.         self.walking_frames_u.append(image)
  109.  
  110.         image = sprite_sheet.get_image(2, 38, 13, 22)
  111.         self.walking_frames_r.append(image)
  112.         image = sprite_sheet.get_image(18, 38, 13, 22)
  113.         self.walking_frames_r.append(image)
  114.         image = sprite_sheet.get_image(34, 38, 13, 22)
  115.         self.walking_frames_r.append(image)
  116.         image = sprite_sheet.get_image(50, 38, 13, 22)
  117.         self.walking_frames_r.append(image)
  118.  
  119.         image = sprite_sheet.get_image(1, 102, 13, 22)
  120.         self.walking_frames_l.append(image)
  121.         image = sprite_sheet.get_image(17, 102, 13, 22)
  122.         self.walking_frames_l.append(image)
  123.         image = sprite_sheet.get_image(33, 102, 13, 22)
  124.         self.walking_frames_l.append(image)
  125.         image = sprite_sheet.get_image(49, 102, 13, 22)
  126.         self.walking_frames_l.append(image)
  127.        
  128.  
  129.         self.image = self.walking_frames_r[0]
  130.  
  131.         self.rect = self.image.get_rect()
  132.  
  133.         self.rect.y = y
  134.         self.rect.x = x
  135.  
  136.     def move(self, walls):
  137.         """ Moves player """
  138.  
  139.         # Move left/right
  140.         self.change_x = 0
  141.  
  142.         pos = self.rect.x
  143.         keystate = pygame.key.get_pressed()
  144.         if keystate[pygame.K_a]:
  145.             self.change_x = -3
  146.             self.direction = "L"
  147.         if keystate[pygame.K_d]:
  148.             self.change_x = 3
  149.             self.direction = "R"
  150.  
  151.         self.rect.x += self.change_x
  152.  
  153.         if self.direction == "R":
  154.             frame = (pos // 30) % len(self.walking_frames_r)
  155.             self.image = self.walking_frames_r[frame]
  156.  
  157.         else:
  158.             frame = (pos // 30) % len(self.walking_frames_l)
  159.             self.image = self.walking_frames_l[frame]
  160.            
  161.  
  162.         # Did this update cause us to hit a wall?
  163.         block_hit_list = pygame.sprite.spritecollide(self, walls, False)
  164.         for block in block_hit_list:
  165.             # If we are moving right, set our right side to the left side of
  166.             # the item we hit
  167.             if self.change_x > 0:
  168.                 self.rect.right = block.rect.left
  169.             else:
  170.                 # Otherwise if we are moving left, do the opposite.
  171.                 self.rect.left = block.rect.right
  172.  
  173.         # Move up/down
  174.         self.change_y = 0
  175.  
  176.         pos = self.rect.y
  177.         keystate = pygame.key.get_pressed()
  178.         if keystate[pygame.K_w]:
  179.             self.change_y = -3
  180.             self.direction = "U"
  181.         if keystate[pygame.K_s]:
  182.             self.change_y = 3
  183.             self.direction = "D"
  184.  
  185.         self.rect.y += self.change_y
  186.  
  187.         if self.direction == "D":
  188.             frame = (pos // 30) % len(self.walking_frames_d)
  189.             self.image = self.walking_frames_d[frame]
  190.  
  191.         else:
  192.             frame = (pos // 30) % len(self.walking_frames_u)
  193.             self.image = self.walking_frames_u[frame]
  194.  
  195.         # Check and see if we hit anything
  196.         block_hit_list = pygame.sprite.spritecollide(self, walls, False)
  197.         for block in block_hit_list:
  198.  
  199.             # Reset our position based on the top/bottom of the object.
  200.             if self.change_y > 0:
  201.                 self.rect.bottom = block.rect.top
  202.             else:
  203.                 self.rect.top = block.rect.bottom
  204.  
  205. class Level(object):
  206.     """ Base class for all levels. """
  207.  
  208.     # Each room has a list of walls, and of enemy sprites.
  209.     wall_list = None
  210.     enemy_list = None
  211.  
  212.     def __init__(self):
  213.         """ Constructor, create our lists. """
  214.         self.wall_list = pygame.sprite.Group()
  215.         self.enemy_list = pygame.sprite.Group()
  216.  
  217.     def draw(self, screen):
  218.         """ Draw everything on this level. """
  219.  
  220.         # Draw the background
  221.         # We don't shift the background as much as the sprites are shifted
  222.         # to give a feeling of depth.
  223.         screen.fill(BLUE)
  224.         screen.blit(self.background, (0, 0))
  225.  
  226.  
  227. class Level_01(Level):
  228.     """This creates all the walls in room 1"""
  229.     def __init__(self):
  230.         super().__init__()
  231.         # Make the walls. (x_pos, y_pos, width, height)
  232.         self.background = pygame.image.load("gamemap.png").convert()
  233.  
  234.         # This is a list of walls. Each is in the form [x, y, width, height]
  235.         walls = [[0, 32, 64, 800, GREEN],
  236.                  [0, 0, 384, 96, GREEN],
  237.                  [448, 0, 400, 96, GREEN],
  238.                  [736, 32, 64, 768, GREEN],
  239.                  [32, 704, 736, 96, GREEN],
  240.                  [320, 96, 32, 32, GREY],
  241.                  [480, 96, 32, 32, GREY]
  242.                  ]
  243.                    
  244.  
  245.         # Loop through the list. Create the wall, add it to the list
  246.         for item in walls:
  247.             wall = Wall(item[0], item[1], item[2], item[3], item[4])
  248.             self.wall_list.add(wall)
  249.  
  250. class Level_02(Level):
  251.     """This creates all the walls in room 2"""
  252.     def __init__(self):
  253.         super().__init__()
  254.  
  255.         self.background = pygame.image.load("gamemap2.png").convert()
  256.  
  257.        
  258.  
  259.         walls = [[0, 0, 800, 96, GREY],
  260.                  [0, 32, 32, 736, GREY],
  261.                  [0, 736, 384, 64, GREY],
  262.                  [448, 736, 368, 64, GREY],
  263.                  [768, 32, 32, 736, GREY]
  264.                 ]
  265.  
  266.         for item in walls:
  267.             wall = Wall(item[0], item[1], item[2], item[3], item[4])
  268.             self.wall_list.add(wall)
  269.  
  270. def main():
  271.     """ Main Program """
  272.  
  273.     # Initializing Pygame
  274.     pygame.init()
  275.  
  276.     # Window Size
  277.     screen = pygame.display.set_mode([800, 800])
  278.  
  279.     # Game Name
  280.     pygame.display.set_caption('Game')
  281.  
  282.     all_sprites_list = pygame.sprite.Group()
  283.  
  284.     # Creating Player
  285.     player = Player(384, 384)
  286.     player_sprite = pygame.sprite.Group()
  287.     player_sprite.add(player)
  288.     all_sprites_list.add(player)
  289.  
  290.     levels = []
  291.  
  292.     level = Level_01()
  293.     levels.append(level)
  294.  
  295.     level = Level_02()
  296.     levels.append(level)
  297.  
  298.     current_level_no = 0
  299.     current_level = levels[current_level_no]
  300.  
  301.     clock = pygame.time.Clock()
  302.  
  303.     done = False
  304.  
  305.     while not done:
  306.  
  307.         # --- Event Processing ---
  308.  
  309.         for event in pygame.event.get():
  310.             if event.type == pygame.QUIT:
  311.                 done = True
  312.  
  313.         # --- Game Logic ---
  314.  
  315.         player.move(current_level.wall_list)
  316.  
  317.         if player.rect.y < 64:
  318.             if current_level_no == 0:
  319.                 current_level_no = 1
  320.                 current_level = levels[current_level_no]
  321.                 player.rect.y = 790
  322.            
  323.         if player.rect.y > 800:
  324.             if current_level_no == 1:
  325.                 current_level_no = 0
  326.                 current_level = levels[current_level_no]
  327.                 player.rect.y = 65
  328.  
  329.         # --- Drawing ---
  330.         current_level.draw(screen)
  331.         player_sprite.draw(screen)
  332.         all_sprites_list.update()
  333.  
  334.         pygame.display.flip()
  335.  
  336.         clock.tick(60)
  337.  
  338.     pygame.quit()
  339.  
  340. if __name__ == "__main__":
  341.     main()
Add Comment
Please, Sign In to add comment