Ejemplo n.º 1
0
    def update_rect(self):
        pos = Vector2()
        if self.pos:
            pos = self.pos
        if self.screen_relative_pos:
            pos = pos+self.screen_relative_pos*engine.get_screen_size()
        self.rect = Rect(pos, self.size, self.angle)

        center_pos = Vector2()
        if self.body:
            center_pos = physics_manager.get_body_position(self.body)
            if self.screen_relative_pos:
                center_pos = center_pos - self.screen_relative_pos*engine.get_screen_size()

            self.not_rot_pos = center_pos

        v = self.size/2
        v.rotate(-self.angle)
        if self.angle > 0:
            pos = self.not_rot_pos + (v-self.size/2)
        else:
            pos = self.not_rot_pos + (self.size/2-v)

        size_y = Vector2(self.size.x, 0)
        size_y.rotate(self.angle)
        v = Vector2(self.size.x, self.size.y + math.fabs(size_y.y))
        self.click_rect = Rect(self.not_rot_pos-v/2, v)
Ejemplo n.º 2
0
 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))))
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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)
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
 def copy(self):
     """ Returns the copy of this """
     sd = SolidPlatform(Hitbox(Rect(0, 0, 0, 0)))
     self.paste_in(sd)
     return sd
Ejemplo n.º 12
0
class Camera:
    """ Camera object """
    def __init__(self):
        self.rect = Rect()
        self.fen = None  #Undefined yet
        self.angle = 0
        self.rotation_effect = False  #Timed effect of rotation
        self.remove_rotation_effect = True  #Possibility to remove it

    def move(self, v):
        """ Translates the camera by vector v"""
        self.rect.translate(v)

    def link_world(self, w):
        """ Set the world attribute """
        self.world = w

    def set_position(self, pos):
        """ Sets the position of the camera in the GameLevel"""
        self.rect.set_position(pos)
        if not (self.fen is None):
            self.compute_distorsion()

    def set_dimension(self, size):
        """ Set the dimension of the Camera (useful to zoom).
        Note that size is a Vector. """
        self.rect.set_dimension(size)
        if not (self.fen is None):
            self.compute_distorsion()

    def get_position(self):
        """ Returns the position of the camera """
        return self.rect.get_position()

    def get_dimension(self):
        """ Returns the dimension of the camera """
        return self.rect.get_dimension()

    def set_fen(self, fen):
        """ Set the window in which the camera will output """
        self.fen = fen
        self.compute_distorsion()

    def get_fen(self):
        """ Returns the windows associated with the camera """
        return self.fen

    def set_distorsion(self, dis):
        """ Set the distorsion of the camera (rescale objects and """
        self.distorsion = dis

    def get_distorsion(self):
        """ Returns the distorsion of the camera """
        return self.distorsion

    def compute_distorsion(self):
        """ Computes the distorsion of the camera """
        (width, height) = (self.fen.get_width(), self.fen.get_height())
        dim = self.get_dimension()
        pos = self.get_position()

        distorsion_scale = Vector(width / dim.x, height / dim.y)
        distorsion_translate = -pos
        self.distorsion = (distorsion_scale, distorsion_translate)

    def is_in_camera(self, rect):
        """ Returns true if the polygon is completely in the camera's rect or if it intersects a side """
        r = self.rect.intersect(rect)
        return r

    def center_on(self, pos):
        """ Centers the camera on a position """
        pos = pos
        pos = pos + (-self.get_dimension() / 2)
        self.set_position(pos)

    def threeforth_on(self, pos):
        """ Makes a 3/4 on the position (this position will be on the left and 3/4 of the screen is free on the right -> usefull for a Canabalt style game """
        dim = self.get_dimension()
        pos += (-Vector(dim.x / 4, dim.y / 2))
        self.set_position(pos)

    def flashblack(self):
        """ Fill the camera with black in order to blit images right after """
        self.get_fen().fill((0, 0, 0))

    def reset_rotation(self):
        """ Reset the rotation """
        self.angle = 0

    def rotate(self, angle):
        """ Add the rotation of [angle] to the camera """
        self.angle += angle

    def set_rotation_effect(self):
        self.rotation_effect = True
        self.rotate(0.5)

    def rotate_view(self, angle):
        """ Rotates the current content of the window by "angle" degrees """
        if self.rotation_effect:
            self.rotate(0.5)
            if self.angle % 360 == 0 and self.remove_rotation_effect:
                self.rotation_effect = False
        center = self.get_fen().get_rect().center
        rotated_fen = pygame.transform.rotate(self.get_fen(), angle)
        rotated_rect = rotated_fen.get_rect()
        rotated_rect.center = center

        self.flashblack()
        self.get_fen().blit(rotated_fen, rotated_rect.topleft)

    def aff(self, objects, bg, score, dt):
        """ Show all objects of the given argument that are in the camera as well as the background and the score """
        #Starts with a flashblack
        if not (self.get_fen() is None):
            self.flashblack()
        #Shows the Background (see Background)
        bg.show()
        #Shows all objects that are in the camera
        to_discard = []
        for o in objects:
            if self.is_in_camera(o.get_hit_box().get_world_rect()
                                 ):  #Checks if the hitbox is in the camera
                o.aff(self.get_fen(), self.get_distorsion(), dt)
            elif not (self.rect.collidex(o.get_hit_box().get_world_rect())):
                if o.stase(0) == 0:
                    to_discard.append(o)
            elif self.rect.dist(
                    o.get_hit_box().get_world_rect()) > self.rect.radius() * 2:
                if o.stase(1) == 0:
                    to_discard.append(o)
        for o in to_discard:
            self.world.dynamic_objects.discard(o)
            del o
        #Show the score
        d = self.get_dimension()
        x = int(d.x * 15 / 16)  #Computes where to put it
        y = int(d.y * 1 / 16)
        distorsion_scale = self.get_distorsion()[0]
        vpos = Vector(x, y) * distorsion_scale

        T(self.get_fen(), str(score), vpos.x, vpos.y, 255, 255, 255, size=45)
        if self.world.player.poisoned_timeout > 0:
            T(self.get_fen(),
              str("Poison!"),
              vpos.x - 150,
              vpos.y,
              91 + (140 - self.world.player.poisoned_timeout * 14),
              47,
              82,
              size=45)

        T(self.get_fen(),
          str(self.world.name),
          vpos.x - 400,
          vpos.y,
          255,
          255,
          255,
          size=45)

        # Rotation
        self.rotate_view(self.angle)

    def __repr__(self):
        txt = "Camera(" + str(self.rect) + ")"
        if self.fen is None: txt += "(not init)"
        return txt
Ejemplo n.º 13
0
 def __init__(self):
     self.rect = Rect()
     self.fen = None  #Undefined yet
     self.angle = 0
     self.rotation_effect = False  #Timed effect of rotation
     self.remove_rotation_effect = True  #Possibility to remove it
Ejemplo n.º 14
0
class GameObject:
    def __init__(self):
        self.id = ""
        self.angle = 0
        self.screen_relative = False
        self.screen_relative_pos = None
        self.body = None
        self.screen_factor = 1
        self.img_loop = False
        self.fixtures = []
        self.pos = Vector2()
        self.not_rot_pos = Vector2()
        self.size = Vector2()
        self.rect = None
        self.click_rect = None
        self.remove = False
        self.event = None

    def scale(self, x, y):

        self.size = self.size*Vector2(x*CONST.scale_speed+1,
                                      y*CONST.scale_speed+1)
        """update body"""
        if self.body:
            physics_manager.remove_body(self.body)
            self.body = physics_manager.add_body(self.not_rot_pos, BodyType.static)
            physics_manager.add_box(self.body,Vector2(),self.size/2)
        self.update_rect()
    
    def update_rect(self):
        pos = Vector2()
        if self.pos:
            pos = self.pos
        if self.screen_relative_pos:
            pos = pos+self.screen_relative_pos*engine.get_screen_size()
        self.rect = Rect(pos, self.size, self.angle)

        center_pos = Vector2()
        if self.body:
            center_pos = physics_manager.get_body_position(self.body)
            if self.screen_relative_pos:
                center_pos = center_pos - self.screen_relative_pos*engine.get_screen_size()

            self.not_rot_pos = center_pos

        v = self.size/2
        v.rotate(-self.angle)
        if self.angle > 0:
            pos = self.not_rot_pos + (v-self.size/2)
        else:
            pos = self.not_rot_pos + (self.size/2-v)

        size_y = Vector2(self.size.x, 0)
        size_y.rotate(self.angle)
        v = Vector2(self.size.x, self.size.y + math.fabs(size_y.y))
        self.click_rect = Rect(self.not_rot_pos-v/2, v)

    def move(self, delta):
        self.pos = self.pos+delta
        if self.body:
            body_pos = physics_manager.get_body_position(self.body)
            physics_manager.set_body_position(self.body,body_pos+delta)
        self.update_rect()

    def set_pos(self,init_pos,delta_pos):
        delta_move = delta_pos-(self.pos-init_pos)
        self.move(delta_move)

    def set_angle(self, angle, center=True):
        pos = Vector2()

        self.angle = angle % 360
        if self.angle > 180:
            self.angle -= 360

        if self.body:
            self.body.angle = self.angle*math.pi/180
        if center:
            if self.body:
                pos = physics_manager.get_body_position(self.body)
                if self.screen_relative_pos:
                    pos = pos - self.screen_relative_pos*physics_manager.get_screen_size()
                self.not_rot_pos = pos - self.size/2
                v = self.size/2
                v.rotate(self.angle)
                self.pos = self.not_rot_pos - v + self.size/2
        else:
            '''TODO: Readjust body pos'''
            pass

        self.update_rect()
        
    def check_click(self, mouse_pos, screen_pos):
        if CONST.debug:
            from game_object.text import Text
            if isinstance(self, Text):
                log("Check click for text: "+self.text)
        if not self.screen_relative:
            point_pos = screen_pos + mouse_pos
        else:
            point_pos = mouse_pos
        if self.click_rect and not self.screen_relative:
            return self.click_rect.collide_point(point_pos)
        elif self.rect:
            return self.rect.collide_point(point_pos)
        else:
            return False

    def execute_event(self):
        if self.event:
            self.event.execute()

    def loop(self,screen):

        screen_pos = level_manager.level.screen_pos
        pos = (0,0)
        if self.pos:
            pos = self.pos
        
        if self.screen_relative_pos is not None:
            pos = pos + self.screen_relative_pos*engine.get_screen_size()
        
        if self.screen_relative:
            pos = self.pos
        else:
            pos = pos-screen_pos
        if CONST.debug and False:
            self.update_rect()
            if not self.screen_relative:
                img_manager.draw_rect(screen, screen_pos, self.rect, (0,0,255,200), self.angle)
                if self.click_rect:
                    img_manager.draw_rect(screen,screen_pos,self.click_rect,(0,255,0,100))

    def set_position(self,pos):
        if self.screen_relative_pos:
            self.pos = (pos[0]-self.screen_relative_pos[0],pos[1]-self.screen_relative_pos[1])
        else:
            self.pos = pos