def __init__(self): super().__init__() #----------------------------- # Collisions #----------------------------- self.__rigid_body = False #Boolean if it's a rigid body self.__collide = False #Boolean if it's a collide body self.__collide_hit_box = Hitbox(Rect(0,0,0,0)) #Collide hit box (see Hit Box) self.__rigid_hit_box = Hitbox(Rect(0,0,0,0)) #Rigid hit box self.rigid_size_factor = 0.999 #Scale factor for rigid body hit box
def platform(x, y, xmax): """ Crée une plateforme de longueur length et de hauteur 12 ayant son coin supérieur gauche aux coordonnées x,y Renvoie un SolidPlatform """ plat = SolidPlatform(Hitbox(Rect(0, 0, xmax - x, 12))) plat.translate(Vector(x, y)) return plat
def add_coins(objects, x0, y0, width, height, number): for i in range(number): if (number > 1): coeff = i / (number - 1) else: coeff = 0.5 #objects.append(Coin(Hitbox(Rect(x0,y0,10,10)))) objects.append( Coin(Hitbox(Rect(x0 + coeff * width, y0 + coeff * height, 10, 10))))
def __init__(self): super().__init__() self.damages = 1 hb = Hitbox(Rect(0, 0, 4, 4)) self.set_hit_box(hb) self.set_collide(True) self.lifecollide = True self.projcollide = True self.solidcollide = True
def __init__(self): hb = Hitbox(Rect(0, 0, 7, 7)) super().__init__(hb) self.set_rigid_body(True) self.max_pv = 1 self.pv = 1 self.small = True self.create_sps("Rhombus") self.animation_speed = 0.2 self.controller = LaserTurretBotController(self)
def __init__(self): hb = Hitbox(Rect(0, 0, 9, 12)) self.mult_offset = 1.5 super().__init__(hb) self.max_pv = 3 self.pv = 3 self.small = True self.create_sps("skeleton") self.sps_right = self.sps self.create_sps("skeleton_inverse") self.sps_left = self.sps self.animation_speed = 0.2 self.set_state("r") self.controller = ZombieController(self) self.collide_plat = None
def __init__(self): super().__init__() self.set_hit_box(Hitbox(Rect(-1, -2, 12, 16))) #Specific Hit box self.set_rigid_body(True) #it's a rigid body self.create_sps("player") #Set sprite self.animation_speed = 0.05 self.set_state("r") #First state : runing ('r') self.controller = PlayerController( self) #Controller for the player (see below) self.score = 0 #Score of the player self.small = False #Life bar self.inventory = defaultdict( int) #Ref to inventory to give items to Campaign mod self.score_to_add = 0 #Score to add for animations
def generate_level(filename, name_of_level='', para=True, limgpar=[]): """ Génère un niveau associé à la musique du fichier filename Renvoie un GameLevel """ (first_beat, tempos, nb_beats) = bpm_info(filename) platforms = [] jump_points = [0] tempo_index = 0 for tmp in tempos: speed = get_speed(tmp) jump_points.append(jump_points[-1] + speed * 60 / tmp) print("generated level length = ", jump_points[-1] / speed, " seconds") y = 0 #initially the height is at 500 #It's at 0 now ^^ jump_points[0] = -1000 # Beginning platform for i in range(nb_beats): #pourquoi +50 et +24 ? #La taille des plateformes non ? platforms.append( platform(jump_points[i] + 50, y, jump_points[i + 1] + 24)) dy = random.randint(-24, 24) y += dy #at each point the y coordinate changes def player_pos(t): return t * speed objects = [] objects.append( Flag(Hitbox(Rect(jump_points[-1] + 24 - 10, y - 20 - dy, 10, 20)))) # Add flag for i in range(len(platforms)): rd = random.random() if (rd < 0.25): (x, y, w, h) = platforms[i].get_hit_box().get_world_rect().get_coord() w -= 10 nb = 3 if (rd < 0.10): nb = 5 add_coins(objects, x + w / 3, y - h, w / 3, 0, nb) for i in range(len(platforms) - 1): rd = random.random() if (rd < 0.33): (x1, y1, w1, h1) = platforms[i].get_hit_box().get_world_rect().get_coord() (x2, y2, w2, h2) = platforms[i + 1].get_hit_box().get_world_rect().get_coord() w1 -= 10 w2 -= 10 start_x = x1 + w1 end_x = x2 start_y = y1 - 45 end_y = y2 - 45 add_coins(objects, start_x, start_y, end_x - start_x, end_y - start_y, random.randint(2, 3)) return GameLevel(platforms + objects, player_pos, name=name_of_level, parallax=para, limgpar=limgpar, music=filename)
def __init__(self,shield,name='spike'): hb = Hitbox(Rect(0,0,10,10)) PickableNode.__init__(self,hb,name) self.create_sps("spike") self.shield = shield
def copy(self): """ Returns the copy of this """ sd = SolidPlatform(Hitbox(Rect(0, 0, 0, 0))) self.paste_in(sd) return sd
class CollideTransformable(MovableNode): def __init__(self): super().__init__() #----------------------------- # Collisions #----------------------------- self.__rigid_body = False #Boolean if it's a rigid body self.__collide = False #Boolean if it's a collide body self.__collide_hit_box = Hitbox(Rect(0,0,0,0)) #Collide hit box (see Hit Box) self.__rigid_hit_box = Hitbox(Rect(0,0,0,0)) #Rigid hit box self.rigid_size_factor = 0.999 #Scale factor for rigid body hit box def copy(self): """ Returns the copy of this with right deep and shallow copies of arguments """ t = CollideTransformable() self.paste_in(t) return t def center_hit_box(self): """ Center its Hitbox -> it's very important for the physics""" #Centers hit boxes tc = self.get_hit_box().center() tr = self.get_rigid_hit_box().center() #Translates the transformable so that this last operation doesn't affect it's position in the world assert tr == tc*self.rigid_size_factor #Verify that rigid_hit_box is included in the collide one self.translate(-tc) def paste_in(self,t): """ Paste this object in t (side effect)""" MovableNode.paste_in(self,t) t.set_collide(self.get_collide()) t.set_rigid_body(self.get_rigid_body()) Hb = self.get_hit_box().copy() rHb = self.get_rigid_hit_box().copy() t.set_hit_box(Hb) t.set_rigid_hit_box(rHb) def set_rigid_body(self,val): """ Sets whether it's a rigid body or not """ self.__rigid_body = val if val: self.__collide = True #A rigid body collides def get_rigid_body(self): """ Returns if it's a rigid body """ return self.__rigid_body def set_collide(self,val): """ Sets whether this can collide """ self.__collide = val def get_collide(self): """ Returns if this collides """ return self.__collide def set_hit_box(self,val): """ Set the collide hit box of this """ #Centers the hit box tr = val.center() #Assign the collide box self.__collide_hit_box = val self.__collide_hit_box.link(self) #Link the hit box to this collidetransformable (see hitbox) #Computes the rigid hit box inside rigidhb = val.copy() rigidhb.rescale(self.rigid_size_factor) self.set_rigid_hit_box(rigidhb) #Translates the transformable so that the given hit box stays the same after beeing centered self.translate(-tr) def get_hit_box(self): """ Compute the hit box according to the position / rotation / scale """ return self.__collide_hit_box def set_rigid_hit_box(self,val): """ Set the rigid body hit box -- Don't use it if you don't know what you're doing """ self.__rigid_hit_box = val self.__rigid_hit_box.link(self) def get_rigid_hit_box(self): """ Returns the rigid hit box """ return self.__rigid_hit_box def apply_solid_reaction(self,support): """ Computes physics when this specific node rigid_collides with support""" assert self.get_rigid_hit_box().collide(support.get_rigid_hit_box()) #Get how to remove the collision correction = self.get_rigid_hit_box().remove_collide(support.get_rigid_hit_box()) #Get how to correct the speed speed = correction.normalise()*self.get_speed() #Correct position and speed self.translate(correction) self.set_speed(self.get_speed()+speed)