o
    OZh                     @   sn  d Z ddlmZmZ ddlmZ ddlmZ ddlZddl	m
Z
 ddlmZ ddlmZ G d	d
 d
ZG dd deZG dd deZG dd deeZG dd deed ZG dd deZeZeZG dd deZG dd deZG dd deZG dd deZG dd deZd d! ZG d"d# d#Zd$d% ZG d&d' d'Zd(d) Z d0d*d+Z!d0d,d-Z"d0d.d/Z#dS )1a	  pygame module with basic game object classes

This module contains several simple classes to be used within games. There
are the main Sprite class and several Group classes that contain Sprites.
The use of these classes is entirely optional when using Pygame. The classes
are fairly lightweight and only provide a starting place for the code
that is common to most games.

The Sprite class is intended to be used as a base class for the different
types of objects in the game. There is also a base Group class that simply
stores sprites. A game could create new types of Group classes that operate
on specially customized Sprite instances they contain.

The basic Sprite class can draw the Sprites it contains to a Surface. The
Group.draw() method requires that each Sprite have a Surface.image attribute
and a Surface.rect. The Group.clear() method requires these same attributes
and can be used to erase all the Sprites with background. There are also
more advanced Groups: pygame.sprite.RenderUpdates() and
pygame.sprite.OrderedUpdates().

Lastly, this module contains several collision functions. These help find
sprites inside multiple groups that have intersecting bounding rectangles.
To find the collisions, the Sprites are required to have a Surface.rect
attribute assigned.

The groups are designed for high efficiency in removing and adding Sprites
to them. They also allow cheap testing to see if a Sprite already exists in
a Group. A given Sprite can exist in any number of groups. A game could use
some groups to control object rendering, and a completely separate set of
groups to control interaction or player movement. Instead of adding type
attributes or bools to a derived Sprite class, consider keeping the
Sprites inside organized Groups. This will allow for easier lookup later
in the game.

Sprites and Groups manage their relationships with the add() and remove()
methods. These methods can accept a single or multiple group arguments for
membership.  The default initializers for these classes also take a
single group or list of groups as arguments for initial membership. It is safe
to repeatedly add and remove the same Sprite from a Group.

While it is possible to design sprite and group classes that don't derive
from the Sprite and AbstractGroup classes below, it is strongly recommended
that you extend those when you create a new Sprite or Group class.

Sprites are not thread safe, so lock them yourself if using threads.

    )GenericTypeVar)WeakSet)warnN)Rect)	get_ticks)from_surfacec                   @   sz   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zedd Zejdd ZdS )Spritea  simple base class for visible game objects

    pygame.sprite.Sprite(*groups): return Sprite

    The base class for visible game objects. Derived classes will want to
    override the Sprite.update() method and assign Sprite.image and Sprite.rect
    attributes.  The initializer can accept any number of Group instances that
    the Sprite will become a member of.

    When subclassing the Sprite class, be sure to call the base initializer
    before adding the Sprite to Groups.

    c                 G   s   t  | _|r| j|  d S d S N)set
_Sprite__gaddselfgroups r   </var/www/auris/lib/python3.10/site-packages/pygame/sprite.py__init__r   s   zSprite.__init__c                 G   sH   | j j}|D ]}t|dr||s||  | | q| j|  qdS )zadd the sprite to groups

        Sprite.add(*groups): return None

        Any number of Group instances can be passed as arguments. The
        Sprite will be added to the Groups it is not already a member of.

        _spritegroupN)r   __contains__hasattradd_internalr   r   r   hasgroupr   r   r   r   w      	


z
Sprite.addc                 G   sH   | j j}|D ]}t|dr||r||  | | q| j|  qdS )zremove the sprite from groups

        Sprite.remove(*groups): return None

        Any number of Group instances can be passed as arguments. The Sprite
        will be removed from the Groups it is currently a member of.

        r   N)r   r   r   remove_internalremover   r   r   r   r      r   zSprite.removec                 C      | j | dS )zr
        For adding this sprite to a group internally.

        :param group: The group we are adding to.
        N)r   r   r   r   r   r   r   r         zSprite.add_internalc                 C   r   )zz
        For removing this sprite from a group internally.

        :param group: The group we are removing from.
        N)r   r   r   r   r   r   r      r    zSprite.remove_internalc                 O   s   dS )a  method to control sprite behavior

        Sprite.update(*args, **kwargs):

        The default implementation of this method does nothing; it's just a
        convenient "hook" that you can override. This method is called by
        Group.update() with whatever arguments you give it.

        There is no need to use this method if not using the convenience
        method by the same name in the Group class.

        Nr   )r   argskwargsr   r   r   update   s    zSprite.updatec                 C   s$   | j D ]}||  q| j   dS )aT  remove the Sprite from all Groups

        Sprite.kill(): return None

        The Sprite is removed from all the Groups that contain it. This won't
        change anything about the state of the Sprite. It is possible to
        continue to use the Sprite after this method has been called, including
        adding it to Groups.

        N)r   r   clearr   r   r   r   kill   s   
zSprite.killc                 C   
   t | jS )zlist of Groups that contain this Sprite

        Sprite.groups(): return group_list

        Returns a list of all the Groups that contain this Sprite.

        )listr   r   r   r   r   r         
zSprite.groupsc                 C   r&   )zdoes the sprite belong to any groups

        Sprite.alive(): return bool

        Returns True when the Sprite belongs to one or more Groups.
        )boolr   r(   r   r   r   alive      
zSprite.alivec                 C   s   d| j j dt| j dS )N<z Sprite(in 	 groups)>)	__class____name__lenr   r(   r   r   r   __repr__   s   zSprite.__repr__c                 C      | j S )a  
        Dynamic, read only property for protected _layer attribute.
        This will get the _layer variable if it exists.

        If you try to get it before it is set it will raise an attribute error.

        Layer property can only be set before the sprite is added to a group,
        after that it is read only and a sprite's layer in a group should be
        set via the group's change_layer() method.

        :return: layer as an int, or raise AttributeError.
        _layerr(   r   r   r   layer   s   zSprite.layerc                 C      |   s	|| _d S tdNzbCan't set layer directly after adding to group. Use group.change_layer(sprite, new_layer) instead.r+   r5   AttributeErrorr   valuer   r   r   r6      
   
N)r0   
__module____qualname____doc__r   r   r   r   r   r#   r%   r   r+   r2   propertyr6   setterr   r   r   r   r	   c   s     
	
r	   c                       s    e Zd ZdZ fddZ  ZS )
WeakSpritezA subclass of Sprite that references its Groups weakly. This
    means that any group this belongs to that is not referenced anywhere
    else is garbage collected automatically.
    c                    s    t  j|  t| j| jd< d S )Nr   )superr   r   r   __dict__r   r/   r   r   r     s   zWeakSprite.__init__)r0   r>   r?   r@   r   __classcell__r   r   rF   r   rC      s    rC   c                   @   sd   e Zd ZdZdd Zdd Zdd Zedd	 Zej	d
d	 Zedd Z
e
j	dd Z
dd ZdS )DirtySpritea  a more featureful subclass of Sprite with more attributes

    pygame.sprite.DirtySprite(*groups): return DirtySprite

    Extra DirtySprite attributes with their default values:

    dirty = 1
        If set to 1, it is repainted and then set to 0 again.
        If set to 2, it is always dirty (repainted each frame;
        flag is not reset).
        If set to 0, it is not dirty and therefore not repainted again.

    blendmode = 0
        It's the special_flags argument of Surface.blit; see the blendmodes in
        the Surface.blit documentation

    source_rect = None
        This is the source rect to use. Remember that it is relative to the top
        left corner (0, 0) of self.image.

    visible = 1
        Normally this is 1. If set to 0, it will not be repainted. (If you
        change visible to 1, you must set dirty to 1 for it to be erased from
        the screen.)

    _layer = 0
        0 is the default value but this is able to be set differently
        when subclassing.

    c                 G   s<   d| _ d| _d| _t| dd| _d | _tj| g|R   d S )N   r   r5   )dirty	blendmode_visiblegetattrr5   source_rectr	   r   r   r   r   r   r   &  s   zDirtySprite.__init__c                 C   s   || _ | jdk rd| _dS dS )z9set the visible value (0 or 1) and makes the sprite dirty   rI   N)rL   rJ   )r   valr   r   r   _set_visible2  s   

zDirtySprite._set_visiblec                 C   r3   )z'return the visible value of that sprite)rL   r(   r   r   r   _get_visible8  s   zDirtySprite._get_visiblec                 C      |   S )z
        You can make this sprite disappear without removing it from the group
        assign 0 for invisible and 1 for visible
        )rR   r(   r   r   r   visible<  s   zDirtySprite.visiblec                 C      |  | d S r
   )rQ   r;   r   r   r   rT   D     c                 C   r3   )a  
        Layer property can only be set before the sprite is added to a group,
        after that it is read only and a sprite's layer in a group should be
        set via the group's change_layer() method.

        Overwrites dynamic property from sprite class for speed.
        r4   r(   r   r   r   r6   H  s   	zDirtySprite.layerc                 C   r7   r8   r9   r;   r   r   r   r6   S  r=   c                 C   s   d| j j dt|   dS )Nr-   z DirtySprite(in r.   )r/   r0   r1   r   r(   r   r   r   r2   _  s   zDirtySprite.__repr__N)r0   r>   r?   r@   r   rQ   rR   rA   rT   rB   r6   r2   r   r   r   r   rH     s    




rH   c                   @   s   e Zd ZdZdS )WeakDirtySpritez]A subclass of WeakSprite and DirtySprite that combines the benefits
    of both classes.
    N)r0   r>   r?   r@   r   r   r   r   rW   e  s    rW   c                   @   s   e Zd ZdZdZdd Zdd Z	d)dd	Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Z	d*ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( ZdS )+AbstractGroupaT  base class for containers of sprites

    AbstractGroup does everything needed to behave as a normal group. You can
    easily subclass a new group class from this or the other groups below if
    you want to add more features.

    Any AbstractGroup-derived sprite groups act like sequences and support
    iteration, len, and so on.

    Tc                 C   s   i | _ g | _d S r
   )
spritedictlostspritesr(   r   r   r   r   z  s   
zAbstractGroup.__init__c                 C   r&   )a~  get a list of sprites in the group

        Group.sprites(): return list

        Returns an object that can be looped over with a 'for' loop. (For now,
        it is always a list, but this could change in a future version of
        pygame.) Alternatively, you can get the same information by iterating
        directly over the sprite group, e.g. 'for sprite in group'.

        )r'   rY   r(   r   r   r   sprites~  s   
zAbstractGroup.spritesNc                 C   s   d| j |< dS )z
        For adding a sprite to this group internally.

        :param sprite: The sprite we are adding.
        :param layer: the layer to add to, if the group type supports layers
        NrY   r   spriter6   r   r   r   r     s   zAbstractGroup.add_internalc                 C   s&   | j | }|r| j| | j |= dS )zw
        For removing a sprite from this group internally.

        :param sprite: The sprite we are removing.
        N)rY   rZ   append)r   r^   Z	lost_rectr   r   r   r     s   
zAbstractGroup.remove_internalc                 C   s
   || j v S )z{
        For checking if a sprite is in this group internally.

        :param sprite: The sprite we are checking.
        r\   r   r^   r   r   r   has_internal     
zAbstractGroup.has_internalc                 C   s   |  |  S )zcopy a group with all the same sprites

        Group.copy(): return Group

        Returns a copy of the group that is an instance of the same class
        and has the same sprites in it.

        )r/   r[   r(   r   r   r   copy  s   	zAbstractGroup.copyc                 C      t |  S r
   )iterr[   r(   r   r   r   __iter__     zAbstractGroup.__iter__c                 C   s
   |  |S r
   )r   r`   r   r   r   r        
zAbstractGroup.__contains__c              
   G   s   |D ]T}t |tr| |s| | ||  qz| j|  W q ttfyV   t|drE| D ]}| |sC| | ||  q2n| |sT| | ||  Y qw dS )zadd sprite(s) to group

        Group.add(sprite, list, group, ...): return None

        Adds a sprite or sequence of sprites to a group.

        r   N	
isinstancer	   ra   r   r   	TypeErrorr:   r   r[   r   r[   r^   sprr   r   r   r     s,   










zAbstractGroup.addc              
   G   s   |D ]T}t |tr| |r| | ||  qz| j|  W q ttfyV   t|drE| D ]}| |rC| | ||  q2n| |rT| | ||  Y qw dS )zremove sprite(s) from group

        Group.remove(sprite, list, or group, ...): return None

        Removes a sprite or sequence of sprites from a group.

        r   N)	rj   r	   ra   r   r   rk   r:   r   r[   rl   r   r   r   r     s,   










zAbstractGroup.removec              
   G   s   |sdS |D ]F}t |tr| |s dS qz| j| s W  dS W q ttfyL   t|drA| D ]}| |s? Y  dS q3n	| |sJY  dS Y qw dS )a;  ask if group has a sprite or sprites

        Group.has(sprite or group, ...): return bool

        Returns True if the given sprite or sprites are contained in the
        group. Alternatively, you can get the same information using the
        'in' operator, e.g. 'sprite in group', 'subgroup in group'.

        Fr   T)rj   r	   ra   r   rk   r:   r   r[   rl   r   r   r   r     s.   







	zAbstractGroup.hasc                 O   s"   |   D ]
}|j|i | qdS )a  call the update method of every member sprite

        Group.update(*args, **kwargs): return None

        Calls the update method of every member sprite. All arguments that
        were passed to this method are passed to the Sprite update function.

        N)r[   r#   )r   r!   r"   r^   r   r   r   r#   "  s   	zAbstractGroup.updater   c              	      sn   |   }t|dr| jt|| fdd|D  n|D ]}||j|jd | j|< qg | _	| j	}|S )zdraw all sprites onto the surface

        Group.draw(surface, special_flags=0): return Rect_list

        Draws all of the member sprites onto the given surface.

        blitsc                 3   s     | ]}|j |jd  fV  qd S r
   )imagerect).0rm   special_flagsr   r   	<genexpr>=  s    
z%AbstractGroup.draw.<locals>.<genexpr>N)
r[   r   rY   r#   ziprn   blitro   rp   rZ   )r   surfacebgsurfrs   r[   rm   rJ   r   rr   r   draw.  s"   

	zAbstractGroup.drawc                 C   s   t |r | jD ]}||| q| j D ]	}|r||| qdS |j}| jD ]}|||| q&| j D ]
}|r>|||| q4dS )a}  erase the previous position of all sprites

        Group.clear(surface, bgd): return None

        Clears the area under every drawn sprite in the group. The bgd
        argument should be Surface which is the same dimensions as the
        screen surface. The bgd could also be a function which accepts
        the given surface and the area to be cleared as arguments.

        N)callablerZ   rY   valuesrv   )r   rw   bgdZlost_clear_rectZ
clear_rectsurface_blitr   r   r   r$   L  s    


zAbstractGroup.clearc                 C   s&   |   D ]}| | ||  qdS )zqremove all sprites

        Group.empty(): return None

        Removes all the sprites from the group.

        N)r[   r   r`   r   r   r   emptye  s   
zAbstractGroup.emptyc                 C   rd   r
   )r*   r[   r(   r   r   r   __bool__q  rg   zAbstractGroup.__bool__c                 C   rd   )zreturn number of sprites in group

        Group.len(group): return int

        Returns the number of sprites contained in the group.

        )r1   r[   r(   r   r   r   __len__t  s   zAbstractGroup.__len__c                 C   s   d| j j dt|  dS )Nr-   (z
 sprites)>)r/   r0   r1   r(   r   r   r   r2   ~  s   zAbstractGroup.__repr__r
   Nr   )r0   r>   r?   r@   r   r   r[   r   r   ra   rc   rf   r   r   r   r   r#   ry   r$   r~   r   r   r2   r   r   r   r   rX   k  s.    
# !

rX   Tc                   @   s   e Zd ZdZdd ZdS )Groupa  container class for many Sprites

    pygame.sprite.Group(*sprites): return Group

    A simple container for Sprite objects. This class can be subclassed to
    create containers with more specific behaviors. The constructor takes any
    number of Sprite arguments to add to the Group. The group supports the
    following standard Python operations:

        in      test if a Sprite is contained
        len     the number of Sprites contained
        bool    test if any Sprites are contained
        iter    iterate through all the Sprites

    The Sprites in the Group are not ordered, so the Sprites are drawn and
    iterated over in no particular order.

    c                 G   s   t |  | j|  d S r
   )rX   r   r   r   r[   r   r   r   r     s   
zGroup.__init__N)r0   r>   r?   r@   r   r   r   r   r   r     s    r   c                   @   s   e Zd ZdZdddZdS )RenderUpdateszGroup class that tracks dirty updates

    pygame.sprite.RenderUpdates(*sprites): return RenderUpdates

    This class is derived from pygame.sprite.Group(). It has an enhanced draw
    method that tracks the changed areas of the screen.

    Nr   c           
      C   s   |j }| j}g | _|j}|  D ]1}| j| }||j|jd |}	|r8|	|r/||	| n||	 || n||	 |	| j|< q|S r
   )	rv   rZ   r_   r[   rY   ro   rp   colliderectunion)
r   rw   rx   rs   r}   rJ   dirty_appendr^   old_rectZnew_rectr   r   r   ry     s   


zRenderUpdates.drawr   )r0   r>   r?   r@   ry   r   r   r   r   r     s    	r   c                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )OrderedUpdatesa{  RenderUpdates class that draws Sprites in order of addition

    pygame.sprite.OrderedUpdates(*sprites): return OrderedUpdates

    This class derives from pygame.sprite.RenderUpdates().  It maintains
    the order in which the Sprites were added to the Group for rendering.
    This makes adding and removing Sprites from the Group a little
    slower than regular Groups.

    c                 G   s   g | _ tj| g|R   d S r
   )_spritelistr   r   r   r   r   r   r     s   zOrderedUpdates.__init__c                 C   
   | j  S r
   r   rc   r(   r   r   r   r[     rh   zOrderedUpdates.spritesNc                 C      t | | | j| d S r
   )r   r   r   r_   r]   r   r   r   r        zOrderedUpdates.add_internalc                 C   r   r
   )r   r   r   r   r`   r   r   r   r     r   zOrderedUpdates.remove_internalr
   )r0   r>   r?   r@   r   r[   r   r   r   r   r   r   r     s    
r   c                   @   s   e Zd ZdZeddddZdd Zd*ddZdd	 Zd
d Z	dd Z
d+ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdS ),LayeredUpdateszLayeredUpdates Group handles layers, which are drawn like OrderedUpdates

    pygame.sprite.LayeredUpdates(*sprites, **kwargs): return LayeredUpdates

    This group is fully compatible with pygame.sprite.Sprite.
    New in pygame 1.8.0

    r   c                 O   s8   i | _ g | _t|  |dd| _| j|i | dS )a+  initialize an instance of LayeredUpdates with the given attributes

        You can set the default layer through kwargs using 'default_layer'
        and an integer for the layer. The default layer is 0.

        If the sprite you add has an attribute _layer, then that layer will be
        used. If **kwarg contains 'layer', then the passed sprites will be
        added to that layer (overriding the sprite._layer attribute). If
        neither the sprite nor **kwarg has a 'layer', then the default layer is
        used to add the sprites.

        Zdefault_layerr   N)_spritelayersr   rX   r   get_default_layerr   )r   r[   r"   r   r   r   r     s
   
zLayeredUpdates.__init__Nc           	      C   s  | j | j|< |du r"z|j}W n ty!   | j}t|d| Y nw t|dr-t|d| | j}| j}|||< t	|}d }}|d }||krd||| d  }|||  |kr\|d }n|d }||ksG||k r|||  |kr|d7 }||k r|||  |ksp|
|| dS )gDo not use this method directly.

        It is used by the group to add a sprite internally.

        Nr5   r   rI   rO   )
_init_rectrY   r6   r:   r   setattrr   r   r   r1   insert)	r   r^   r6   r[   sprites_layerslenglowmidhighr   r   r   r     s4   


zLayeredUpdates.add_internalc              
   O   s   |sdS d|v r|d nd}|D ]Z}t |tr(| |s'| || ||  qz
| j|i | W q ttfyj   t|drX| D ]}| |sV| || ||  qDn| |sh| || ||  Y qw dS )a  add a sprite or sequence of sprites to a group

        LayeredUpdates.add(*sprites, **kwargs): return None

        If the sprite you add has an attribute _layer, then that layer will be
        used. If **kwarg contains 'layer', then the passed sprites will be
        added to that layer (overriding the sprite._layer attribute). If
        neither the sprite nor **kwarg has a 'layer', then the default layer is
        used to add the sprites.

        Nr6   r   ri   )r   r[   r"   r6   r^   rm   r   r   r   r     s2   







zLayeredUpdates.addc                 C   sX   | j | | j| }|| jur| j| t|dr"| j|j | j|= | j|= dS )zVDo not use this method directly.

        The group uses it to add a sprite.

        rp   N)	r   r   rY   r   rZ   r_   r   rp   r   )r   r^   r   r   r   r   r   I  s   


zLayeredUpdates.remove_internalc                 C   r   )ztreturn a ordered list of sprites (first back, last top).

        LayeredUpdates.sprites(): return sprites

        r   r(   r   r   r   r[   Z  rb   zLayeredUpdates.spritesc                 C   s   | j }|j}| j}g | _|j}| j}|  D ]1}	||	 }
||	j|	jd|}|
|u r.|| n||
r;||	|
 n|| ||
 |||	< q|S )zdraw all sprites in the right order onto the passed surface

        LayeredUpdates.draw(surface, special_flags=0): return Rect_list

        N)
rY   rv   rZ   r_   r   r[   ro   rp   r   r   )r   rw   rx   rs   rY   r}   rJ   r   	init_rectrm   recZnewrectr   r   r   ry   b  s"   


zLayeredUpdates.drawc                    s,   | j  t|d}| } fdd|D S )zreturn a list with all sprites at that position

        LayeredUpdates.get_sprites_at(pos): return colliding_sprites

        Bottom sprites are listed first; the top ones are listed last.

        )rI   rI   c                    s   g | ]} | qS r   r   )rq   i_spritesr   r   
<listcomp>  s    z1LayeredUpdates.get_sprites_at.<locals>.<listcomp>)r   r   collidelistall)r   posrp   Zcolliding_idxr   r   r   get_sprites_at|  s   

zLayeredUpdates.get_sprites_atc                 C   s
   | j | S )zreturn the sprite at the index idx from the groups sprites

        LayeredUpdates.get_sprite(idx): return sprite

        Raises IndexOutOfBounds if the idx is not within range.

        r   )r   idxr   r   r   
get_sprite  r)   zLayeredUpdates.get_spritec                 C   s   |  |}| j|  |S )zremove all sprites from a layer and return them as a list

        LayeredUpdates.remove_sprites_of_layer(layer_nr): return sprites

        )get_sprites_from_layerr   )r   Zlayer_nrr[   r   r   r   remove_sprites_of_layer  s   

z&LayeredUpdates.remove_sprites_of_layerc                 C   s   t t| j S )zireturn a list of unique defined layers defined.

        LayeredUpdates.layers(): return layers

        )sortedr   r   r{   r(   r   r   r   layers  s   zLayeredUpdates.layersc           	      C   s   | j }| j}|| || t|}d }}|d }||kr=||| d  }|||  |kr5|d }n|d }||ks ||k rY|||  |krY|d7 }||k rY|||  |ksI||| t|drjt|d| |||< dS )change the layer of the sprite

        LayeredUpdates.change_layer(sprite, new_layer): return None

        The sprite must have been added to the renderer already. This is not
        checked.

        r   rI   rO   r5   N)r   r   r   popr1   r   r   r   )	r   r^   	new_layerr[   r   r   r   r   r   r   r   r   change_layer  s(   	



zLayeredUpdates.change_layerc                 C   s   | j || jS )zreturn the layer that sprite is currently in

        If the sprite is not found, then it will return the default layer.

        )r   r   r   r`   r   r   r   get_layer_of_sprite  r    z"LayeredUpdates.get_layer_of_spritec                 C      | j | jd  S )zTreturn the top layer

        LayeredUpdates.get_top_layer(): return layer

        r   r   r(   r   r   r   get_top_layer  r    zLayeredUpdates.get_top_layerc                 C   r   )zZreturn the bottom layer

        LayeredUpdates.get_bottom_layer(): return layer

        r   r   r(   r   r   r   get_bottom_layer  r    zLayeredUpdates.get_bottom_layerc                 C   s   |  ||   dS )a  bring the sprite to front layer

        LayeredUpdates.move_to_front(sprite): return None

        Brings the sprite to front by changing the sprite layer to the top-most
        layer. The sprite is added at the end of the list of sprites in that
        top-most layer.

        N)r   r   r`   r   r   r   move_to_front  s   
zLayeredUpdates.move_to_frontc                 C   s   |  ||  d  dS )zmove the sprite to the bottom layer

        LayeredUpdates.move_to_back(sprite): return None

        Moves the sprite to the bottom layer by moving it to a new layer below
        the current bottom layer.

        rI   N)r   r   r`   r   r   r   move_to_back  s   	zLayeredUpdates.move_to_backc                 C   s
   | j d S )z[return the topmost sprite

        LayeredUpdates.get_top_sprite(): return Sprite

        r   r   r(   r   r   r   get_top_sprite  rb   zLayeredUpdates.get_top_spritec                 C   sH   g }|j }| j}| jD ]}|| |kr|| q|| |kr! |S q|S )a2  return all sprites from a layer ordered as they where added

        LayeredUpdates.get_sprites_from_layer(layer): return sprites

        Returns all sprites from a layer. The sprites are ordered in the
        sequence that they where added. (The sprites are not removed from the
        layer.

        )r_   r   r   )r   r6   r[   Zsprites_appendZsprite_layersrm   r   r   r   r     s   


z%LayeredUpdates.get_sprites_from_layerc                 C   s:   |  |}| |D ]}| || q
| j|d|i dS )zswitch the sprites from layer1_nr to layer2_nr

        LayeredUpdates.switch_layer(layer1_nr, layer2_nr): return None

        The layers number must exist. This method does not check for the
        existence of the given layers.

        r6   N)r   r   r   r   )r   Z	layer1_nrZ	layer2_nrZsprites1rm   r   r   r   switch_layer  s   
	zLayeredUpdates.switch_layerr
   r   )r0   r>   r?   r@   r   r   r   r   r   r   r[   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s,    	
%+

$r   c                   @   s~   e Zd ZdZdd ZdddZdddZed	d
 Zedd Z	dd Z
dd ZdddZdd Zdd Zdd Zdd ZdS )LayeredDirtya.  LayeredDirty Group is for DirtySprites; subclasses LayeredUpdates

    pygame.sprite.LayeredDirty(*sprites, **kwargs): return LayeredDirty

    This group requires pygame.sprite.DirtySprite or any sprite that
    has the following attributes:
        image, rect, dirty, visible, blendmode (see doc of DirtySprite).

    It uses the dirty flag technique and is therefore faster than
    pygame.sprite.RenderUpdates if you have many static sprites.  It
    also switches automatically between dirty rect updating and full
    screen drawing, so you do no have to worry which would be faster.

    As with the pygame.sprite.Group, you can specify some additional attributes
    through kwargs:
        _use_update: True/False   (default is False)
        _default_layer: default layer where the sprites without a layer are
            added
        _time_threshold: threshold time for switching between dirty rect mode
            and fullscreen mode; defaults to updating at 80 frames per second,
            which is equal to 1000.0 / 80.0

    New in pygame 1.8.0

    c                 O   sd   t j| g|R i | d| _d| _d| _d| _| D ]\}}|dv r/t| |r/t| || qdS )a   initialize group.

        pygame.sprite.LayeredDirty(*sprites, **kwargs): return LayeredDirty

        You can specify some additional attributes through kwargs:
            _use_update: True/False   (default is False)
            _default_layer: default layer where the sprites without a layer are
                added
            _time_threshold: threshold time for switching between dirty rect
                mode and fullscreen mode; defaults to updating at 80 frames per
                second, which is equal to 1000.0 / 80.0

        NFg      )@)_use_update_time_thresholdr   )	r   r   _clipr   r   _bgditemsr   r   )r   r[   r"   keyrP   r   r   r   r   A  s   
zLayeredDirty.__init__Nc                 C   sb   t |dst t |dst t |dst t|ts t |jdkr(d|_t| || dS )r   rJ   rT   rK   r   rI   N)r   r:   rj   rH   rk   rJ   r   r   r]   r   r   r   r   ]  s   




zLayeredDirty.add_internalc              	   C   sn  |  }| j}|du r|}| j}| j}| j}t}	|j}
|dur"|| _| j}|| t	 }| j
rd| |||	|||j| j |durU|du rGdn|}|D ]	}|
|||| qK| ||	||
|| t|}n5|durw|du rndn|}|
|dd| |D ]}|jr|du r|jn|}|
|j|j|j|||< qy|	|g}t	 }|| | jkrd| _
nd| _
g |dd< || |S )a  draw all sprites in the right order onto the given surface

        LayeredDirty.draw(surface, bgsurf=None, special_flags=None): return Rect_list

        You can pass the background too. If a self.bgd is already set to some
        value that is not None, then the bgsurf argument has no effect.
        Passing a value to special_flags will pass that value as the
        special_flags argument to pass to all Surface.blit calls, overriding
        the sprite.blendmode attribute

        Nr   )r   r   FT)get_clipr   r   rY   rZ   r   rv   r   set_clipr   r   _find_dirty_arear_   r   _draw_dirty_internalr'   rT   rK   ro   rp   rN   r   )r   rw   rx   rs   Z	orig_clipZlatest_clipZlocal_spritesZlocal_old_rectZlocal_updateZ	rect_typeZsurf_blit_funcZ	local_bgd
start_timeflagsr   Z	local_retrm   end_timer   r   r   ry   s  sn   



zLayeredDirty.drawc              
   C   s  |D ]}|d u r|j n|}|jdk rm|jrm|jd ur6||jj|jj}|jd |d  }	|jd |d  }
n|j}|d  }	|d  }
|j}||D ] }||| }||j	||d |	 |d |
 |d |d f| qKq|jr|||j	|j|j|| |< |jdkrd|_qd S )NrI   r   rO      )
rK   rJ   rT   rN   rp   topleftsizeclipr   ro   )	_old_rect_rectr   Z
_surf_blit_updateZ_special_flagsrm   r   Z	_spr_rectZrect_offset_xZrect_offset_yZ_spr_rect_clipr   r   r   r   r   r     s@   





z!LayeredDirty._draw_dirty_internalc                 C   s   |D ]q}|j dkrs|jr||jj|jj}n||j}|j}	|j}
|	|}|dkr:|
||  ||= |	|}|dks)|||  || |urs||| }|j}	|j}
|	|}|dkrl|
||  ||= |	|}|dks[|||  qd S )Nr   r   )rJ   rN   rp   r   r   ZcollidelistZunion_ipr   )r   r   r   r   r   Z_update_appendr   rm   Z_union_rectZ_union_rect_collidelistZ_union_rect_union_ipr   r   r   r   r     s6   

zLayeredDirty._find_dirty_areac                 C   s
   || _ dS )zOuse to set background

        Group.clear(surface, bgd): return None

        N)r   )r   rw   r|   r   r   r   r$     rb   zLayeredDirty.clearc                 C   s2   | j r| j|| j  dS | jt| dS )zrepaint the given area

        LayeredDirty.repaint_rect(screen_rect): return None

        screen_rect is in screen coordinates.

        N)r   rZ   r_   r   r   r   Zscreen_rectr   r   r   repaint_rect"  s   zLayeredDirty.repaint_rectc                 C   s*   |du rt j  | _n|| _d| _dS )zclip the area where to draw; pass None (default) to reset the clip

        LayeredDirty.set_clip(screen_rect=None): return None

        NF)pygamedisplayZget_surfaceZget_rectr   r   r   r   r   r   r   /  s   
zLayeredDirty.set_clipc                 C   r3   )z]get the area where drawing will occur

        LayeredDirty.get_clip(): return Rect

        )r   r(   r   r   r   r   ;  s   zLayeredDirty.get_clipc                 C   s&   t | || |jdkrd|_dS dS )r   r   rI   N)r   r   rJ   )r   r^   r   r   r   r   r   C  s   	

zLayeredDirty.change_layerc                 C   s   t dt | | dS )a  set the threshold in milliseconds

        set_timing_treshold(time_ms): return None

        Defaults to 1000.0 / 80.0. This means that the screen will be painted
        using the flip method rather than the update method if the update
        method is taking so long to update the screen that the frame rate falls
        below 80 frames per second.

        Raises TypeError if time_ms is not int or float.

        zHThis function will be removed, use set_timing_threshold function insteadN)r   DeprecationWarningset_timing_thresholdr   Ztime_msr   r   r   set_timing_tresholdP  s
   z LayeredDirty.set_timing_tresholdc                 C   s,   t |ttfr|| _dS td|jj d)a  set the threshold in milliseconds

        set_timing_threshold(time_ms): return None

        Defaults to 1000.0 / 80.0. This means that the screen will be painted
        using the flip method rather than the update method if the update
        method is taking so long to update the screen that the frame rate falls
        below 80 frames per second.

        Raises TypeError if time_ms is not int or float.

        zExpected numeric value, got z insteadN)rj   intfloatr   rk   r/   r0   r   r   r   r   r   c  s
   
z!LayeredDirty.set_timing_thresholdr
   )NN)r0   r>   r?   r@   r   r   ry   staticmethodr   r   r$   r   r   r   r   r   r   r   r   r   r   r   &  s     

\
*

r   c                   @   s~   e Zd ZdZdddZdd Zdd Zdd	d
Zdd Zdd Z	dd Z
edd Zejdd Zdd Zdd Zdd ZdS )GroupSinglea  A group container that holds a single most recent item.

    This class works just like a regular group, but it only keeps a single
    sprite in the group. Whatever sprite has been added to the group last will
    be the only sprite in the group.

    You can access its one sprite as the .sprite attribute.  Assigning to this
    attribute will properly remove the old sprite and then add the new one.

    Nc                 C   s*   t |  d | _|d ur| | d S d S r
   )rX   r   _GroupSingle__spriter   r`   r   r   r   r     s
   
zGroupSingle.__init__c                 C   r&   r
   )r   r   r(   r   r   r   rc     rh   zGroupSingle.copyc                 C   s   | j d ur	| j gS g S r
   r   r(   r   r   r   r[     s   
zGroupSingle.spritesc                 C   s,   | j d ur| j |  | | j  || _ d S r
   )r   r   r]   r   r   r   r     s   

zGroupSingle.add_internalc                 C   s
   | j d uS r
   r   r(   r   r   r   r     rh   zGroupSingle.__bool__c                 C   r3   r
   r   r(   r   r   r   _get_sprite  s   zGroupSingle._get_spritec                 C   s   |  | | |  |S r
   )r   r`   r   r   r   _set_sprite  s   

zGroupSingle._set_spritec                 C   rS   )zf
        Property for the single sprite contained in this group

        :return: The sprite.
        )r   r(   r   r   r   r^     s   zGroupSingle.spritec                 C   rU   r
   )r   )r   Zsprite_to_setr   r   r   r^     rV   c                 C   s.   || j u rd | _ || jv rt| | d S d S r
   )r   rY   rX   r   r`   r   r   r   r     s
   

zGroupSingle.remove_internalc                 C   
   | j |u S r
   r   r`   r   r   r   ra     rh   zGroupSingle.has_internalc                 C   r   r
   r   r`   r   r   r   r     rh   zGroupSingle.__contains__r
   )r0   r>   r?   r@   r   rc   r[   r   r   r   r   rA   r^   rB   r   ra   r   r   r   r   r   r   x  s     



r   c                 C   s   | j |j S )a  collision detection between two sprites, using rects.

    pygame.sprite.collide_rect(left, right): return bool

    Tests for collision between two sprites. Uses the pygame.Rect colliderect
    function to calculate the collision. It is intended to be passed as a
    collided callback function to the *collide functions. Sprites must have
    "rect" attributes.

    New in pygame 1.8.0

    rp   r   )leftrightr   r   r   collide_rect  s   r   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	collide_rect_ratioaO  A callable class that checks for collisions using scaled rects

    The class checks for collisions between two sprites using a scaled version
    of the sprites' rects. Is created with a ratio; the instance is then
    intended to be passed as a collided callback function to the *collide
    functions.

    New in pygame 1.8.1

    c                 C   
   || _ dS )zcreate a new collide_rect_ratio callable

        Ratio is expected to be a floating point value used to scale
        the underlying sprite rect before checking for collisions.

        Nratior   r   r   r   r   r     r,   zcollide_rect_ratio.__init__c                 C   2   dj | jjt| d@ ddd | j D dS )/
        Turn the class into a string.
        <{klass} @{id:x} {attrs}>  c                 s   "    | ]\}}| d |V  qdS =Nr   rq   kvr   r   r   rt          z.collide_rect_ratio.__repr__.<locals>.<genexpr>klassidattrsformatr/   r0   r   joinrE   r   r(   r   r   r   r2     
   
zcollide_rect_ratio.__repr__c                 C   sl   | j }|j}|j}|j}||| | || | }|j}|j}|j}||| | || | }||S )ae  detect collision between two sprites using scaled rects

        pygame.sprite.collide_rect_ratio(ratio)(left, right): return bool

        Tests for collision between two sprites. Uses the pygame.Rect
        colliderect function to calculate the collision after scaling the rects
        by the stored ratio. Sprites must have "rect" attributes.

        )r   rp   widthheightZinflater   )r   r   r   r   leftrectr   r  	rightrectr   r   r   __call__  s   
zcollide_rect_ratio.__call__Nr0   r>   r?   r@   r   r2   r  r   r   r   r   r     s
    	r   c           	      C   s   | j j|j j }| j j|j j }|d |d  }z| j}W n ty:   | j }d|jd |jd  d  }|| _Y nw z|j}W n ty]   |j }d|jd |jd  d  }||_Y nw ||| d kS )a  detect collision between two sprites using circles

    pygame.sprite.collide_circle(left, right): return bool

    Tests for collision between two sprites by testing whether two circles
    centered on the sprites overlap. If the sprites have a "radius" attribute,
    then that radius is used to create the circle; otherwise, a circle is
    created that is big enough to completely enclose the sprite's rect as
    given by the "rect" attribute. This function is intended to be passed as
    a collided callback function to the *collide functions. Sprites must have a
    "rect" and an optional "radius" attribute.

    New in pygame 1.8.0

    rO         ?)rp   centerxcenteryradiusr:   r   r  )	r   r   	xdistance	ydistancedistancesquared
leftradiusr  rightradiusr  r   r   r   collide_circle	  s$   



r  c                   @   r   )	collide_circle_ratioay  detect collision between two sprites using scaled circles

    This callable class checks for collisions between two sprites using a
    scaled version of a sprite's radius. It is created with a ratio as the
    argument to the constructor. The instance is then intended to be passed as
    a collided callback function to the *collide functions.

    New in pygame 1.8.1

    c                 C   r   )a9  creates a new collide_circle_ratio callable instance

        The given ratio is expected to be a floating point value used to scale
        the underlying sprite radius before checking for collisions.

        When the ratio is ratio=1.0, then it behaves exactly like the
        collide_circle method.

        Nr   r   r   r   r   r   @  s   

zcollide_circle_ratio.__init__c                 C   r   )r   r   r   r   c                 s   r   r   r   r   r   r   r   rt   T  r   z0collide_circle_ratio.__repr__.<locals>.<genexpr>r   r   r(   r   r   r   r2   L  r   zcollide_circle_ratio.__repr__c                 C   s   | j }|jj|jj }|jj|jj }|d |d  }z|j}W n ty=   |j}d|jd |jd  d  }||_Y nw ||9 }z|j}	W n tyd   |j}
d|
jd |
jd  d  }	|	|_Y nw |	|9 }	|||	 d kS )a  detect collision between two sprites using scaled circles

        pygame.sprite.collide_circle_radio(ratio)(left, right): return bool

        Tests for collision between two sprites by testing whether two circles
        centered on the sprites overlap after scaling the circle's radius by
        the stored ratio. If the sprites have a "radius" attribute, that is
        used to create the circle; otherwise, a circle is created that is big
        enough to completely enclose the sprite's rect as given by the "rect"
        attribute. Intended to be passed as a collided callback function to the
        *collide functions. Sprites must have a "rect" and an optional "radius"
        attribute.

        rO   r  )r   rp   r  r  r	  r:   r   r  )r   r   r   r   r
  r  r  r  r  r  r  r   r   r   r  W  s*   



zcollide_circle_ratio.__call__Nr  r   r   r   r   r  4  s
    r  c                 C   s   |j d | j d  }|j d | j d  }z| j}W n ty'   t| j}Y nw z|j}W n ty;   t|j}Y nw ||||fS )a  collision detection between two sprites, using masks.

    pygame.sprite.collide_mask(SpriteLeft, SpriteRight): bool

    Tests for collision between two sprites by testing if their bitmasks
    overlap. If the sprites have a "mask" attribute, that is used as the mask;
    otherwise, a mask is created from the sprite image. Intended to be passed
    as a collided callback function to the *collide functions. Sprites must
    have a "rect" and an optional "mask" attribute.

    New in pygame 1.8.0

    r   rI   )rp   maskr:   r   ro   overlap)r   r   ZxoffsetZyoffsetZleftmaskZ	rightmaskr   r   r   collide_mask  s   

r  c                    s   j j|r3g }|j}| D ]!} dur# |r"|  || q|j r0|  || q|S  durA fdd|D S fdd|D S )a-  find Sprites in a Group that intersect another Sprite

    pygame.sprite.spritecollide(sprite, group, dokill, collided=None):
        return Sprite_list

    Return a list containing all Sprites in a Group that intersect with another
    Sprite. Intersection is determined by comparing the Sprite.rect attribute
    of each Sprite.

    The dokill argument is a bool. If set to True, all Sprites that collide
    will be removed from the Group.

    The collided argument is a callback function used to calculate if two
    sprites are colliding. it should take two sprites as values, and return a
    bool value indicating if they are colliding. If collided is not passed, all
    sprites must have a "rect" value, which is a rectangle of the sprite area,
    which will be used to calculate the collision.

    Nc                    s   g | ]	} |r|qS r   r   rq   group_sprite)collidedr^   r   r   r     s
    
z!spritecollide.<locals>.<listcomp>c                    s   g | ]	} |j r|qS r   )rp   r  )default_sprite_collide_funcr   r   r     s    )rp   r   r_   r[   r%   )r^   r   Zdokillr  crashedr_   r  r   )r  r  r^   r   spritecollide  s,   


r  c           	      C   sh   i }t }|r |  D ]}|||||}|r|||< |  q
|S | D ]}|||||}|r1|||< q"|S )aw  detect collision between a group and another group

    pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb):
        return dict

    Given two groups, this will find the intersections between all sprites in
    each group. It returns a dictionary of all sprites in the first group that
    collide. The value for each item in the dictionary is a list of the sprites
    in the second group it collides with. The two dokill arguments control if
    the sprites from either group will be automatically removed from all
    groups. Collided is a callback function used to calculate if two sprites
    are colliding. it should take two sprites as values, and return a bool
    value indicating if they are colliding. If collided is not passed, all
    sprites must have a "rect" value, which is a rectangle of the sprite area
    that will be used to calculate the collision.

    )r  r[   r%   )	ZgroupaZgroupbZdokillaZdokillbr  r  Zsprite_collide_funcZgroup_a_sprite	collisionr   r   r   groupcollide  s    r  c                 C   sP   | j j}|dur|D ]}|| |r|  S q
dS |D ]}||j r%|  S qdS )a
  finds any sprites in a group that collide with the given sprite

    pygame.sprite.spritecollideany(sprite, group): return sprite

    Given a sprite and a group of sprites, this will return any single
    sprite that collides with the given sprite. If there are no
    collisions, then this returns None.

    If you don't need all the features of the spritecollide function, this
    function will be a bit quicker.

    Collided is a callback function used to calculate if two sprites are
    colliding. It should take two sprites as values and return a bool value
    indicating if they are colliding. If collided is not passed, then all
    sprites must have a "rect" value, which is a rectangle of the sprite area,
    which will be used to calculate the collision.


    Nr   )r^   r   r  r  r  r   r   r   spritecollideany  s   

r  r
   )$r@   typingr   r   weakrefr   warningsr   r   Zpygame.rectr   Zpygame.timer   Zpygame.maskr   r	   rC   rH   rW   rX   r   ZRenderPlainZRenderClearr   r   r   r   r   r   r   r  r  r  r  r  r  r   r   r   r   <module>   sH   B _    O  TG:+M

4$