SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. # physics
  5. def CollisionTest(Object1,ObjectList):
  6.     CollisionList = []
  7.     for Object in ObjectList:
  8.         if Object.colliderect(Object1):
  9.             CollisionList.append(Object)
  10.     return CollisionList
  11.  
  12. class PhysicsObject(object):
  13.    
  14.     def __init__(self,x,y,x_size,y_size):
  15.         self.width = x_size
  16.         self.height = y_size
  17.         self.rect = pygame.Rect(x,y,self.width,self.height)
  18.         self.x = x
  19.         self.y = y
  20.         self.hitbox = None
  21.  
  22.     def setup_hitbox(self,x_offset,y_offset,x_size,y_size):
  23.         self.hitbox = [x_offset,y_offset,x_size,y_size]
  24.  
  25.     def get_hitbox(self):
  26.         return pygame.Rect(self.x+self.hitbox[0],self.y+self.hitbox[1],self.hitbox[2],self.hitbox[3])
  27.        
  28.     def move(self,Movement,platforms,ramps):
  29.         self.x += Movement[0]
  30.         self.rect.x = int(self.x)
  31.         block_hit_list = CollisionTest(self.rect,platforms)
  32.         collision_types = {'top':False,'bottom':False,'right':False,'left':False,'slant_bottom':False}
  33.         for block in block_hit_list:
  34.             if Movement[0] > 0:
  35.                 self.rect.right = block.left
  36.                 collision_types['right'] = True
  37.             elif Movement[0] < 0:
  38.                 self.rect.left = block.right
  39.                 collision_types['left'] = True
  40.             self.x = self.rect.x
  41.         self.y += Movement[1]
  42.         self.rect.y = int(self.y)
  43.         block_hit_list = CollisionTest(self.rect,platforms)
  44.         for block in block_hit_list:
  45.             if Movement[1] > 0:
  46.                 self.rect.bottom = block.top
  47.                 collision_types['bottom'] = True
  48.             elif Movement[1] < 0:
  49.                 self.rect.top = block.bottom
  50.                 collision_types['top'] = True
  51.             self.change_y = 0
  52.             self.y = self.rect.y
  53.         for ramp in ramps:
  54.             rampR = pygame.Rect(ramp[0],ramp[1],20,20)
  55.             if self.rect.colliderect(rampR):
  56.                 if ramp[2] == 1:
  57.                     if self.rect.right-ramp[0]+self.rect.bottom-ramp[1] > 20:
  58.                         self.rect.bottom = ramp[1]+20-(self.rect.right-ramp[0])
  59.                         self.y = self.rect.y
  60.                         collision_types['slant_bottom'] = True
  61.                 if ramp[2] == 2:
  62.                     if ramp[0]+20-self.rect.left+self.rect.bottom-ramp[1] > 20:
  63.                         self.rect.bottom = ramp[1]+20-(ramp[0]+20-self.rect.left)
  64.                         self.y = self.rect.y
  65.                         collision_types['slant_bottom'] = True
  66.         return collision_types
  67.            
  68.     def Draw(self):
  69.         pygame.draw.rect(screen,(0,0,255),self.rect)
  70.        
  71.     def CollisionItem(self):
  72.         CollisionInfo = [self.rect.x,self.rect.y,self.width,self.height]
  73.         return CollisionInfo
  74.  
  75. def flip(img,boolean=True):
  76.     return pygame.transform.flip(img,boolean,False)
  77.  
  78. def blit_center(surf,surf2,pos):
  79.     x = int(surf2.get_width()/2)
  80.     y = int(surf2.get_height()/2)
  81.     surf.blit(surf2,(pos[0]-x,pos[1]-y))
  82.  
  83. class entity(object):
  84.     global animation_database, animation_higher_database
  85.    
  86.     def __init__(self,x,y,size_x,size_y,e_type): # x, y, size_x, size_y, type
  87.         self.x = x
  88.         self.y = y
  89.         self.size_x = size_x
  90.         self.size_y = size_y
  91.         self.obj = PhysicsObject(x,y,size_x,size_y)
  92.         self.animation = None
  93.         self.image = None
  94.         self.animation_frame = 0
  95.         self.animation_tags = []
  96.         self.flip = False
  97.         self.offset = [0,0]
  98.         self.rotation = 0
  99.         self.type = e_type # used to determine animation set among other things
  100.         self.action_timer = 0
  101.         self.action = ''
  102.         self.set_action('idle') # overall action for the entity
  103.         self.entity_data = {}
  104.         self.alpha = None
  105.  
  106.     def set_pos(self,x,y):
  107.         self.x = x
  108.         self.y = y
  109.         self.obj.x = x
  110.         self.obj.y = y
  111.         self.obj.rect.x = x
  112.         self.obj.rect.y = y
  113.  
  114.     def move(self,momentum,platforms,ramps):
  115.         collisions = self.obj.move(momentum,platforms,ramps)
  116.         self.x = self.obj.x
  117.         self.y = self.obj.y
  118.         return collisions
  119.  
  120.     def rect(self):
  121.         return pygame.Rect(self.x,self.y,self.size_x,self.size_y)
  122.  
  123.     def set_flip(self,boolean):
  124.         self.flip = boolean
  125.  
  126.     def set_animation_tags(self,tags):
  127.         self.animation_tags = tags
  128.  
  129.     def set_animation(self,sequence):
  130.         self.animation = sequence
  131.         self.animation_frame = 0
  132.  
  133.     def set_action(self,action_id,force=False):
  134.         if (self.action == action_id) and (force == False):
  135.             pass
  136.         else:
  137.             self.action = action_id
  138.             anim = animation_higher_database[self.type][action_id]
  139.             self.animation = anim[0]
  140.             self.set_animation_tags(anim[1])
  141.             self.animation_frame = 0
  142.  
  143.     def clear_animation(self):
  144.         self.animation = None
  145.  
  146.     def set_image(self,image):
  147.         self.image = image
  148.  
  149.     def set_offset(self,offset):
  150.         self.offset = offset
  151.  
  152.     def set_frame(self,amount):
  153.         self.animation_frame = amount
  154.  
  155.     def handle(self):
  156.         self.action_timer += 1
  157.         self.change_frame(1)
  158.  
  159.     def change_frame(self,amount):
  160.         self.animation_frame += amount
  161.         if self.animation != None:
  162.             while self.animation_frame < 0:
  163.                 if 'loop' in self.animation_tags:
  164.                     self.animation_frame += len(self.animation)
  165.                 else:
  166.                     self.animation = 0
  167.             while self.animation_frame >= len(self.animation):
  168.                 if 'loop' in self.animation_tags:
  169.                     self.animation_frame -= len(self.animation)
  170.                 else:
  171.                     self.animation_frame = len(self.animation)-1
  172.  
  173.     def get_current_img(self):
  174.         if self.animation == None:
  175.             if self.image != None:
  176.                 return flip(self.image,self.flip)
  177.             else:
  178.                 return None
  179.         else:
  180.             return flip(animation_database[self.animation[self.animation_frame]],self.flip)
  181.  
  182.     def display(self,surface,scroll):
  183.         image_to_render = None
  184.         if self.animation == None:
  185.             if self.image != None:
  186.                 image_to_render = flip(self.image,self.flip).copy()
  187.         else:
  188.             image_to_render = flip(animation_database[self.animation[self.animation_frame]],self.flip).copy()
  189.         if image_to_render != None:
  190.             center_x = image_to_render.get_width()/2
  191.             center_y = image_to_render.get_height()/2
  192.             image_to_render = pygame.transform.rotate(image_to_render,self.rotation)
  193.             if self.alpha != None:
  194.                 image_to_render.set_alpha(self.alpha)
  195.             blit_center(surface,image_to_render,(int(self.x)-scroll[0]+self.offset[0]+center_x,int(self.y)-scroll[1]+self.offset[1]+center_y))
  196.  
  197. # animation stuff
  198. global animation_database
  199. animation_database = {}
  200.  
  201. global animation_higher_database
  202. animation_higher_database = {}
  203.  
  204. # a sequence looks like [[0,1],[1,1],[2,1],[3,1],[4,2]]
  205. # the first numbers are the image name(as integer), while the second number shows the duration of it in the sequence
  206. def animation_sequence(sequence,base_path,colorkey=(255,255,255),transparency=255):
  207.     global animation_database
  208.     result = []
  209.     for frame in sequence:
  210.         image_id = base_path + base_path.split('/')[-2] + '_' + str(frame[0])
  211.         image = pygame.image.load(image_id + '.png').convert()
  212.         image.set_colorkey(colorkey)
  213.         image.set_alpha(transparency)
  214.         animation_database[image_id] = image.copy()
  215.         for i in range(frame[1]):
  216.             result.append(image_id)
  217.     return result
  218.  
  219.  
  220. def get_frame(ID):
  221.     global animation_database
  222.     return animation_database[ID]
  223.  
  224. def load_animations(path):
  225.     global animation_higher_database
  226.     f = open(path + 'entity_animations.txt','r')
  227.     data = f.read()
  228.     f.close()
  229.     for animation in data.split('\n'):
  230.         sections = animation.split(' ')
  231.         anim_path = sections[0]
  232.         entity_info = anim_path.split('/')
  233.         entity_type = entity_info[0]
  234.         animation_id = entity_info[1]
  235.         timings = sections[1].split(';')
  236.         tags = sections[2].split(';')
  237.         sequence = []
  238.         n = 0
  239.         for timing in timings:
  240.             sequence.append([n,int(timing)])
  241.             n += 1
  242.         anim = animation_sequence(sequence,path + anim_path)
  243.         if entity_type not in animation_higher_database:
  244.             animation_higher_database[entity_type] = {}
  245.         animation_higher_database[entity_type][animation_id] = [anim.copy(),tags]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top