Exemple #1
0
 def __init__(self, loaderTile, surface, xIndex, yIndex, layer):
     """
     Create a new GameTile form the U{tiledtmxloader's<http://pygame.org/project-map+loader+for+'tiled'-1158-.html>}
     tile.
     
     @type  loaderTile:    L{Tile<tiledtmxloader.Tile>}
     @param loaderTile:    The the loader's tile object, which is converted to our own class.
     
     @type  surface:       C{U{pygame.Surface<http://www.pygame.org/docs/ref/surface.html>}}
     @param surface:       This GameTile's graphic.
     
     @type  xIndex:        C{int}
     @param xIndex:        X coordinate in the Tiled editor.
     
     @type  yIndex:        C{int}
     @param yIndex:        Y coordinate in the Tiled editor. 
     
     @type  layer:         L{GameLayer<Map.GameLayer.GameLayer>}
     @param layer:         The GameLayer this tile is on.
     """
     rect = surface.get_rect()
     rect.topleft = (xIndex * rect.width, yIndex * rect.height)
     name = 'Game Tile'
     GameObject.__init__(self, rect.topleft, rect.width, rect.height, layer, name, surface)
     
     # When creating tiles from a large image, loader does not associate Tile objects with the surfaces.
     # Handling that.
     if loaderTile == None:
         self.properties = {}
     else:
         self.properties = loaderTile.properties
Exemple #2
0
    def __init__(self):
        Scene.__init__(self)

        # The order in which components are created determine the order of script execution

        demo_background = GameObject(self, name="Demo Background")

        Transform(demo_background, scale=Vector2D(2, 2))
        SpriteRenderer(
            demo_background,
            Sprite.from_path("./demo_assets/sprites/demo_background.jpg"),
            update_on_draw=False)

        demo_player = GameObject(self, name="Demo Player")

        Transform(demo_player, scale=Vector2D(2, 2))
        SpriteRenderer(
            demo_player,
            Sprite.from_path("./demo_assets/sprites/demo_player.png"))
        DemoMovement(demo_player)

        main_camera = GameObject(self, name="Main Camera", parent=demo_player)

        Transform(main_camera)
        Camera(
            main_camera,
            [0, 0, Screen().screen_size()[0],
             Screen().screen_size()[1]])

        demo_text = GameObject(self, name="Demo Text")

        Transform(demo_text,
                  position=Vector2D(-Screen().screen_size()[0] / 2,
                                    -Screen().screen_size()[1] / 2))
        SpriteRenderer(demo_text, align_x="right", align_y="bottom")
        Text(demo_text,
             font="./demo_assets/fonts/calling_code.ttf",
             font_size=40,
             text="Demo Text",
             color=Color.RGBA(255, 87, 51),
             background_color=Color.RGBA(0, 0, 0, 200))

        # The order in which game_objects are added to the list determines the order of rendering and execution
        self.game_objects.append(demo_background)
        self.game_objects.append(demo_player)
        self.game_objects.append(demo_text)
        self.game_objects.append(main_camera)
Exemple #3
0
    def __init__(self, position, width, height, name, collisionGroupNames, transferName, stateMappings, startStateName, image=None, animationMappings=None, soundMappings=None, stateTransitionRestrictions=None):
        """
        Creates a new Actor.  Initializes all Actor variables, but should be extended by derived classes.
        
        @type  position:             C{(int, int)} | L{Vector<Utilities.vector.Vector>}
        @param position:             World coordinates of the top left corner of the object's bounding box.
        
        @type  width:                C{int}
        @param width:                Width of the object's bounding box in pixels.
        
        @type  height:               C{int}
        @param height:               Height of the object's bounding box in pixels.
        
        @type  name:                 C{str}
        @param name:                 Name for this Actor.
        
        @type  collisionGroupNames:  C{list}
        @param collisionGroupNames:  List of names (C{str}) of L{CollisionGroup<CollisionGroup.CollisionGroup>}s that
                                     this Actor should be part of.
                                     
        @type  transferName:         C{str}
        @param transferName:         Name of the Actor to transfer attributes from when switching between maps. 
        
        @type  stateMappings:        C{list}
        @param stateMappings:        List of pairs in the form C{str, L{State<State.State>}} which are the States available to
                                     this Actor.  In detail they are:
                                         - C{str} - Name to use to refer to this State.
                                         - C{State} - The State object.
        
        @type  startStateName:       C{str}
        @param startStateName:       Name of the L{State<State.State>} the Actor will start in.
        
        @type  image:                U{C{pygame.Surface}<http://www.pygame.org/docs/ref/surface.html>}
        @param image:                Static image that should be drawn at the object's L{Position}.
        
        @type  animationMappings:    C{list}
        @param animationMappings:    List of tuples in the form C{(str, L{Animation<Animation.Animation>}, (int, int))}.
                                     In detail they are:
                                         - C{str} - Name to use to refer to this Animation.
                                         - C{Animation} - Animation object itself.
                                         - C{(int, int)} - Offset from the GameObject's L{Position} where the top-left of the Animation's frames
                                         should be drawn.

        @type  soundMappings:        C{list}
        @param soundMappings:        List of tuples in the form C{(str, L{Sound<Sound.Sound>})}.
                                     In detail they are:
                                         - C{str} - Name to use to refer to this Sound.
                                         - C{Sound} - Sound object itself.
                                         
                                         
        @type  stateTransitionRestrictions:    C{list}
        @param stateTransitionRestrictions:    List of tuples in the form C{(str, str)}.  The first string is the originating state name
                                               and the second is the target state name.  If the first state is trying to transition into
                                               the second it will be ignored.
        """
        GameObject.__init__(self, position, width, height, None, name, image, animationMappings, soundMappings)
        
        # collision groups
        self.cgroupNames = collisionGroupNames
        self._collisionGroups = []
        self._collisions = set()
        
        # states
        self._stateMappings = {}
        for name, state in stateMappings:
            self._stateMappings[name] = state
        
        # state restrictions
        self._restrictedStateTrans = []
        if self._restrictedStateTrans:
            for statePair in stateTransitionRestrictions:
                self._restrictedStateTrans.append(statePair)
        
        # velocities are in pixels/second
        self._velocity = Vector((0, 0))
        self._prevPosition = Vector(self.Position)
        
        # some maximum values
        self._maxXVel = Constants.ActorConstants.MAX_X_VELOCITY
        self._maxYVel = Constants.ActorConstants.MAX_Y_VELOCITY
        
        # make sure the state is setup correctly
        self._currentState = self._stateMappings[startStateName]
        self._currentState.OnEnter()
        
        # effects
        self._effects = []
        
        # map transition
        self._transferFromName = transferName