Advertisement
Guest User

Untitled

a guest
Feb 14th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.84 KB | None | 0 0
  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(surf2,pos,render_priority,display_renders): # blit surf not necessary in Carenlian Blade's structure
  79.     x = int(surf2.get_width()/2)
  80.     y = int(surf2.get_height()/2)
  81.     display_blit(surf2,(pos[0]-x,pos[1]-y),render_priority,display_renders)
  82.  
  83. def display_blit(surface,pos,render_priority,display_renders,special_flags=None):
  84.     display_renders.append([render_priority,pos,surface,special_flags])
  85.  
  86. class entity(object):
  87.     global animation_database, animation_higher_database
  88.    
  89.     def __init__(self,x,y,size_x,size_y,e_type): # x, y, size_x, size_y, type
  90.         self.x = x
  91.         self.y = y
  92.         self.size_x = size_x
  93.         self.size_y = size_y
  94.         self.obj = PhysicsObject(x,y,size_x,size_y)
  95.         self.animation = None
  96.         self.image = None
  97.         self.animation_frame = 0
  98.         self.animation_tags = []
  99.         self.flip = False
  100.         self.offset = [0,0]
  101.         self.rotation = 0
  102.         self.type = e_type # used to determine animation set among other things
  103.         self.action_timer = 0
  104.         self.action = ''
  105.         self.set_action('idle') # overall action for the entity
  106.  
  107.     def set_pos(self,x,y):
  108.         self.x = x
  109.         self.y = y
  110.         self.obj.x = x
  111.         self.obj.y = y
  112.         self.obj.rect.x = x
  113.         self.obj.rect.y = y
  114.  
  115.     def move(self,momentum,platforms,ramps):
  116.         collisions = self.obj.move(momentum,platforms,ramps)
  117.         self.x = self.obj.x
  118.         self.y = self.obj.y
  119.         return collisions
  120.  
  121.     def rect(self):
  122.         return pygame.Rect(self.x,self.y,self.size_x,self.size_y)
  123.  
  124.     def set_flip(self,boolean):
  125.         self.flip = boolean
  126.  
  127.     def set_animation_tags(self,tags):
  128.         self.animation_tags = tags
  129.  
  130.     def set_animation(self,sequence):
  131.         self.animation = sequence
  132.         self.animation_frame = 0
  133.  
  134.     def set_action(self,action_id,force=False):
  135.         if (self.action == action_id) and (force == False):
  136.             pass
  137.         else:
  138.             self.action = action_id
  139.             anim = animation_higher_database[self.type][action_id]
  140.             self.animation = anim[0]
  141.             self.set_animation_tags(anim[1])
  142.             self.animation_frame = 0
  143.  
  144.     def clear_animation(self):
  145.         self.animation = None
  146.  
  147.     def set_image(self,image):
  148.         self.image = image
  149.  
  150.     def set_offset(self,offset):
  151.         self.offset = offset
  152.  
  153.     def set_frame(self,amount):
  154.         self.animation_frame = amount
  155.  
  156.     def handle(self):
  157.         self.action_timer += 1
  158.         self.change_frame(1)
  159.  
  160.     def change_frame(self,amount):
  161.         self.animation_frame += amount
  162.         if self.animation != None:
  163.             while self.animation_frame < 0:
  164.                 if 'loop' in self.animation_tags:
  165.                     self.animation_frame += len(self.animation)
  166.                 else:
  167.                     self.animation = 0
  168.             while self.animation_frame >= len(self.animation):
  169.                 if 'loop' in self.animation_tags:
  170.                     self.animation_frame -= len(self.animation)
  171.                 else:
  172.                     self.animation_frame = len(self.animation)-1
  173.  
  174.     def get_current_img(self):
  175.         if self.animation == None:
  176.             if self.image != None:
  177.                 return flip(self.image,self.flip)
  178.             else:
  179.                 return None
  180.         else:
  181.             return flip(animation_database[self.animation[self.animation_frame]],self.flip)
  182.  
  183.     def display(self,scroll,render_priority,display_renders):
  184.         image_to_render = None
  185.         if self.animation == None:
  186.             if self.image != None:
  187.                 image_to_render = flip(self.image,self.flip).copy()
  188.         else:
  189.             image_to_render = flip(animation_database[self.animation[self.animation_frame]],self.flip).copy()
  190.         if image_to_render != None:
  191.             center_x = image_to_render.get_width()/2
  192.             center_y = image_to_render.get_height()/2
  193.             image_to_render = pygame.transform.rotate(image_to_render,self.rotation)
  194.             blit_center(image_to_render,(int(self.x)-scroll[0]+self.offset[0]+center_x,int(self.y)-scroll[1]+self.offset[1]+center_y),render_priority,display_renders)
  195.  
  196. # animation stuff
  197. global animation_database
  198. animation_database = {}
  199.  
  200. global animation_higher_database
  201. animation_higher_database = {}
  202.  
  203. # a sequence looks like [[0,1],[1,1],[2,1],[3,1],[4,2]]
  204. # the first numbers are the image name(as integer), while the second number shows the duration of it in the sequence
  205. def animation_sequence(sequence,base_path,colorkey=(0,0,0),transparency=255):
  206.     global animation_database
  207.     result = []
  208.     for frame in sequence:
  209.         image_id = base_path + base_path.split('/')[-2] + '_' + str(frame[0])
  210.         image = pygame.image.load(image_id + '.png').convert()
  211.         image.set_colorkey(colorkey)
  212.         image.set_alpha(transparency)
  213.         animation_database[image_id] = image.copy()
  214.         for i in range(frame[1]):
  215.             result.append(image_id)
  216.     return result
  217.  
  218.  
  219. def get_frame(ID):
  220.     global animation_database
  221.     return animation_database[ID]
  222.  
  223. def load_animations(path):
  224.     global animation_higher_database
  225.     f = open(path + 'entity_animations.txt','r')
  226.     data = f.read()
  227.     f.close()
  228.     for animation in data.split('\n'):
  229.         sections = animation.split(' ')
  230.         anim_path = sections[0]
  231.         entity_info = anim_path.split('/')
  232.         entity_type = entity_info[0]
  233.         animation_id = entity_info[1]
  234.         timings = sections[1].split(';')
  235.         tags = sections[2].split(';')
  236.         sequence = []
  237.         n = 0
  238.         for timing in timings:
  239.             sequence.append([n,int(timing)])
  240.             n += 1
  241.         anim = animation_sequence(sequence,path + anim_path)
  242.         if entity_type not in animation_higher_database:
  243.             animation_higher_database[entity_type] = {}
  244.         animation_higher_database[entity_type][animation_id] = [anim.copy(),tags]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement