ClearCode

surfacemaker.py

Mar 22nd, 2022
1,592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import pygame
  2. from settings import *
  3. from os import walk
  4.  
  5. class SurfaceMaker:
  6.     def __init__(self):
  7.         # import all the graphics
  8.         for index, info in enumerate(walk('../graphics/blocks')):
  9.             if index == 0:
  10.                 self.assets = {color:{} for color in info[1]}
  11.             else:
  12.                 for image_name in info[2]:
  13.                     color_type = list(self.assets.keys())[index - 1]
  14.                     full_path = '../graphics/blocks' + f'/{color_type}/' + image_name
  15.                     surf = pygame.image.load(full_path).convert_alpha()
  16.                     self.assets[color_type][image_name.split('.')[0]] = surf
  17.  
  18.     def get_surf(self,block_type,size):
  19.        
  20.         # create one surface with the graphics with any size
  21.         image = pygame.Surface(size)
  22.         image.set_colorkey((0,0,0))
  23.         sides = self.assets[block_type]
  24.        
  25.         # 4 corners
  26.         image.blit(sides['topleft'],(0,0))
  27.         image.blit(sides['topright'],(size[0] - sides['topright'].get_width(),0))
  28.         image.blit(sides['bottomleft'],(0,size[1] - sides['bottomleft'].get_height()))
  29.         image.blit(sides['bottomright'],(size[0] - sides['bottomright'].get_width(),size[1] - sides['bottomleft'].get_height()))
  30.  
  31.         # top side
  32.         top_width = size[0] - (sides['topleft'].get_width() + sides['topright'].get_width())
  33.         scaled_top_surf = pygame.transform.scale(sides['top'],(top_width,sides['top'].get_height()))
  34.         image.blit(scaled_top_surf,(sides['topleft'].get_width(),0))
  35.  
  36.         # left side
  37.         left_height = size[1] - (sides['topleft'].get_height() + sides['bottomleft'].get_height())
  38.         scaled_left_surf = pygame.transform.scale(sides['left'],(sides['left'].get_width(),left_height))
  39.         image.blit(scaled_left_surf,(0,sides['topleft'].get_height()))
  40.  
  41.         # right side
  42.         right_height = size[1] - (sides['topright'].get_height() + sides['bottomright'].get_height())
  43.         scaled_right_surf = pygame.transform.scale(sides['right'],(sides['right'].get_width(),right_height))
  44.         image.blit(scaled_right_surf,(size[0] - sides['right'].get_width(),sides['topright'].get_height()))
  45.  
  46.         # bottom side
  47.         bottom_width = size[0] - (sides['bottomleft'].get_width() + sides['bottomright'].get_width())
  48.         scaled_bottom_surf = pygame.transform.scale(sides['bottom'],(bottom_width,sides['bottom'].get_height()))
  49.         image.blit(scaled_bottom_surf,(sides['topleft'].get_width(),size[1] - sides['bottom'].get_height()))
  50.        
  51.         # center color
  52.         center_height = size[1] - (sides['top'].get_height() + sides['bottom'].get_height())
  53.         center_width = size[0] - (sides['right'].get_width() + sides['left'].get_width())
  54.         scaled_center = pygame.transform.scale(sides['center'],(center_width,center_height))
  55.         image.blit(scaled_center,sides['topleft'].get_size())
  56.  
  57.         # return that image to the blocks or the player
  58.         return image
Advertisement
Add Comment
Please, Sign In to add comment