本文整理汇总了Python中Camera.get_viewport方法的典型用法代码示例。如果您正苦于以下问题:Python Camera.get_viewport方法的具体用法?Python Camera.get_viewport怎么用?Python Camera.get_viewport使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Camera
的用法示例。
在下文中一共展示了Camera.get_viewport方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GameWorld
# 需要导入模块: import Camera [as 别名]
# 或者: from Camera import get_viewport [as 别名]
class GameWorld():
### Constructors ###
## Constructs a game world instance with the initial world specified by
# the input name parameter.
#
# @param world_name The identifier for the initial world to be loaded.
def __init__( self, world_name="" ):
self._world = World()
segment = self._world.levels[ "2" ].segments[ "2.1" ]
segment_dims = segment.get_pixel_dims()
self._tilemap = segment.get_tiles()
self._entities = []
# TODO: Update this logic to more elegantly designate the entity that
# will be followed by the camera.
player_entity = None
for ( (idx_x, idx_y), entity_class ) in segment.get_entities():
entity_pos = ( TILE_DIMS[0] * idx_x, TILE_DIMS[1] * idx_y )
entity_delta = PhysicalState( CompositeHitbox(entity_pos[0], entity_pos[1]) )
entity = Entity( entity_class, entity_delta )
if entity_class == "player":
player_entity = entity
self._entities.append( entity )
self._camera = Camera( target=player_entity.get_hitbox().get_hitbox(),
new_border=PG.Rect(0, 0, segment_dims[0], segment_dims[1]) )
self._collision_detector = SpatialDictionary( segment_dims[0] / 16,
segment_dims[0], segment_dims[1] )
self._setup_collision_detector()
### Methods ###
## Updates the state of the game world based on the given time delta,
# which represents the amount of time that has passed since the last update.
#
# @param time_delta The amount of game time that has passed in the frame.
def update( self, time_delta ):
# TODO: Handle the events passed back by the `Entity` updates.
for entity in self._entities:
entity.update( time_delta )
self._collision_detector.update()
for entity_collision in self._collision_detector.get_all_collisions():
self._resolve_entity_collision( list(entity_collision) )
for entity in self._entities:
self._resolve_tile_collisions( entity )
self._camera.update( time_delta )
## Notifies the game world of the given event, which is propogated to
# all proper entities on the next update.
#
# @param event The event of which the game world will be notified.
# @param entities An optional listing of entities to be notified of the
# event. If this list is empty, the event will be broadcasted.
def notify_of( self, event, entities=[] ):
entities_to_notify = self._entities if len(entities) == 0 else entities
for entity in entities_to_notify:
entity.notify_of( event )
## @return A listing of all the entity objects contained within the world
# (of type `Entity` list).
def get_entities( self ):
return self._entities
## @return A 2D matrix of strings where each string represents the
# identifier of the corresponding tile in the game world.
def get_tilemap( self ):
return self._tilemap
## @return The rectangular view representing the player viewpoint of the
# game world (of type `pygame.Rect`).
def get_viewport( self ):
return self._camera.get_viewport()
### Helper Functions ###
## Establishes the proper infrastructure to get the collision detection
# system for the world instance up and running.
def _setup_collision_detector( self ):
self._cdrepr2entity_dict = {}
[ self._add_to_collision_detector( entity ) for entity in self._entities ]
## Adds the given entity to the collision detection system.
#
# @param entity The `Entity` object instance to be added to the collision
# detection system for the game world.
def _add_to_collision_detector( self, entity ):
for hitbox in entity.get_hitbox().get_hitboxes():
self._cdrepr2entity_dict[ hitbox ] = entity
self._collision_detector.add( hitbox )
#.........这里部分代码省略.........