o
    tVg0                     @   s&  d Z ddlZddlZddlmZ ddlZej se	ddZ
dZdZdZeddd	d
Zdaejejed Zdd Zdd ZG dd dejjZG dd dejjZG dd dejjZG dd dejjZG dd dejjZG dd dejjZdddZ e!dkre   e"  dS dS )a7   pygame.examples.aliens

Shows a mini game where you have to defend against aliens.

What does it show you about pygame?

* pg.sprite, the difference between Sprite and Group.
* dirty rectangle optimization for processing for speed.
* music with pg.mixer.music, including fadeout
* sound effects with pg.Sound
* event processing, keyboard handling, QUIT handling.
* a main loop frame limited with a game clock from pg.time.Clock
* fullscreen switching.


Controls
--------

* Left and right arrows to move.
* Space bar to shoot
* f key to toggle between fullscreen.

    N)Listz%Sorry, extended image module required      <      i  i  c              	   C   sP   t jtd| } ztj| }W |
 S  tjy'   td|  dt	  w )z$loads an image, prepares it for playdatazCould not load image "z" )ospathjoinmain_dirpgimageloaderror
SystemExit	get_errorconvert)filesurface r   J/home/ubuntu/.local/lib/python3.10/site-packages/pygame/examples/aliens.py
load_image1   s   r   c                 C   sR   t jsdS tjtd| } z	t j| }|W S  t jy(   td|   Y dS w )z-because pygame can be compiled without mixer.Nr   zWarning, unable to load, )	r   mixerr   r	   r
   r   Soundr   print)r   soundr   r   r   
load_sound;   s   r   c                   @   sH   e Zd ZU dZdZdZdZg Zee	j
 ed< dd Zdd	 Zd
d ZdS )Playerz1Representing the player as a moon buggy type car.
      imagesc                 G   sN   t jjj| g|R   | jd | _| jjtjd| _	d| _
| j	j| _d| _d S )Nr   	midbottom)r   spriteSprite__init__r!   r   get_rect
SCREENRECTr#   rect	reloadingtoporigtopfacingselfgroupsr   r   r   r'   X   s   

zPlayer.__init__c                 C   sv   |r|| _ | j|| j d | jt| _|dk r!| jd | _n
|dkr+| jd | _| j| jj	| j
 d  | j_d S )Nr      r   )r.   r*   move_ipspeedclampr)   r!   r   r-   leftbouncer,   )r0   	directionr   r   r   move`   s    zPlayer.movec                 C   s    | j | j | jj }|| jjfS )N)r.   
gun_offsetr*   centerxr,   )r0   posr   r   r   gunposk   s   zPlayer.gunposN)__name__
__module____qualname____doc__r4   r7   r:   r!   r   r   Surface__annotations__r'   r9   r=   r   r   r   r   r   P   s   
 r   c                   @   <   e Zd ZU dZdZdZg Zeej	 e
d< dd Zdd Zd	S )
Alienz7An alien space ship. That slowly moves down the screen.   r   r!   c                 G   sb   t jjj| g|R   | jd | _| j | _t	dt
j | _d| _| jdk r/tj| j_d S d S )Nr   )r$   r2   )r   r%   r&   r'   r!   r   r(   r*   randomchoicerE   r4   r.   framer)   rightr/   r   r   r   r'   w   s   
zAlien.__init__c                 O   sl   | j | jd t| j s"| j | _| j jd | j _| j t| _ | jd | _| j	| j| j
 d  | _d S )Nr   r2      )r*   r3   r.   r)   containsbottomr,   r5   rI   r!   	animcycler   r0   argskwargsr   r   r   update   s   
zAlien.updateN)r>   r?   r@   rA   r4   rN   r!   r   r   rB   rC   r'   rR   r   r   r   r   rE   p   s   
 	rE   c                   @   rD   )
	Explosionz5An explosion. Hopefully the Alien and not the player!r   rK   r!   c                 G   sB   t jjj| g|R   | jd | _| jj|jjd| _| j	| _
d S )Nr   )center)r   r%   r&   r'   r!   r   r(   r*   rT   defaultlifelife)r0   actorr1   r   r   r   r'      s   zExplosion.__init__c                 O   s>   | j d | _ | j| j | j d  | _| j dkr|   dS dS )zcalled every time around the game loop.

        Show the explosion surface for 'defaultlife'.
        Every game tick(update), we decrease the 'life'.

        Also we animate the explosion.
        r2   r   r   N)rV   r!   rN   r   killrO   r   r   r   rR      s
   
zExplosion.updateN)r>   r?   r@   rA   rU   rN   r!   r   r   rB   rC   r'   rR   r   r   r   r   rS      s   
 rS   c                   @   8   e Zd ZU dZdZg Zeej e	d< dd Z
dd ZdS )	Shotz!a bullet the Player sprite fires.r    r!   c                 G   s6   t jjj| g|R   | jd | _| jj|d| _d S )Nr   r"   )r   r%   r&   r'   r!   r   r(   r*   )r0   r<   r1   r   r   r   r'      s   zShot.__init__c                 O   s,   | j d| j | j jdkr|   dS dS )z^called every time around the game loop.

        Every tick we move the shot upwards.
        r   N)r*   r3   r4   r,   rX   rO   r   r   r   rR      s   zShot.updateNr>   r?   r@   rA   r4   r!   r   r   rB   rC   r'   rR   r   r   r   r   rZ      s   
 rZ   c                   @   rY   )	BombzA bomb the aliens drop.	   r!   c                 G   sH   t jjj| g|R   | jd | _| jj|jddj	d| _|| _
d S )Nr      r"   )r   r%   r&   r'   r!   r   r(   r*   r9   r#   explosion_group)r0   alienr_   r1   r   r   r   r'      s   
zBomb.__init__c                 O   s8   | j d| j | j jdkrt| | j |   dS dS )zcalled every time around the game loop.

        Every frame we move the sprite 'rect' down.
        When it reaches the bottom we:

        - make an explosion.
        - remove the Bomb.
        r   i  N)r*   r3   r4   rM   rS   r_   rX   rO   r   r   r   rR      s
   	zBomb.updateNr[   r   r   r   r   r\      s   
 r\   c                   @   s    e Zd ZdZdd Zdd ZdS )Scorezto keep track of the score.c                 G   s^   t jjj| g|R   t jd d| _| jd d| _d| _| 	  | j
 dd| _d S )N   r2   whiter$   r   i  )r   r%   r&   r'   fontFont
set_italiccolor	lastscorerR   r   r(   r9   r*   r/   r   r   r   r'      s   zScore.__init__c                 O   s6   t | jkrt | _dt  }| j|d| j| _dS dS )z9We only update the score in update() when it has changed.zScore: r   N)SCORErh   rd   renderrg   r   )r0   rP   rQ   msgr   r   r   rR      s
   

zScore.updateN)r>   r?   r@   rA   r'   rR   r   r   r   r   ra      s    	ra   c                 C   s   t  d dkrt jdddd t   t jr$t j s$td d t _d}d} t jt	j
| d}t jt	j
| |}td}|t j|d	dgt_td
}|t j|d	d	gt_dd dD t_tdgt_tdgt_t jtjd d}t j| t jd t jd td}t t	j
}tdt	j| D ]
}|||df q||d t j  td}	td}
t jrt j!"t#dd}t jj$%| t jj$&d t j'( }t j'( }t j'( }t j') }t j'* }t+}t j,- }t|}t||| t j.r|/t0| |1 r}t j23 D ]i}|j4t j5kr d S |j4t j6kr.|j7t j8kr. d S |j4t j6kry|j7t j9kry|sZtd |: }t jt	j
| t j;B |}||d ntd |: }t jt	j
| |}||d t j  | }qt j7< }|=|| |>  |t j? |t j@  }|A| |t jB }|jCs|rtD|tEk rt|F || t jr|
d ur|
&  ||_C|r|d	 }ntGtHH tI st||| t+}|rtGtHH tJ st|j'||| t j'K||d	D ]"}t jr|	d ur|	&  t|| t|| tLd	 aL|M  qt j'N||d	d	O D ]}t jr6|	d ur6|	&  t|| tLd	 aLq't j'K||d	D ]}t jrX|	d urX|	&  t|| t|| |M  qI|P|}t j>| |Qd |1 st jrt jj$Rd t j,Sd d S )Nr   r   iD      i   zWarning, no soundFzplayer1.gifr2   zexplosion1.gifc                 S   s   g | ]}t |qS r   )r   ).0imr   r   r   
<listcomp>   s    zmain.<locals>.<listcomp>)z
alien1.gifz
alien2.gifz
alien3.gifzbomb.gifzshot.gif)rl   rl   zPygame Alienszbackground.gif)r   r   zboom.wavzcar_door.wavr   zhouse_lo.wavr$   zChanging to FULLSCREENzChanging to windowed mode(   i  )Tr   get_sdl_versionr   pre_initinitget_initr   displaymode_okr)   sizeset_moder   	transformflipr   r!   rS   rE   r\   rZ   scaleset_iconset_captionmouseset_visiblerB   rangewidth	get_widthblitr   r   r	   r
   r   musicr   playr%   GroupRenderUpdatesGroupSingleALIEN_RELOADtimeClockrd   addra   aliveeventgettypeQUITKEYDOWNkeyK_ESCAPEK_fcopy
FULLSCREENget_pressedclearrR   K_RIGHTK_LEFTr9   K_SPACEr+   len	MAX_SHOTSr=   intrG   
ALIEN_ODDS	BOMB_ODDSspritecollideri   rX   groupcollidekeysdrawtickfadeoutwait)winstyle
fullscreen	bestdepthscreenimgiconbgdtile
backgroundx
boom_soundshoot_soundr   aliensshotsbombsall	lastalienalienreloadclockplayerr   screen_backupkeystater8   firingr`   bombdirtyr   r   r   main   s   




















Vr   __main__)r   )#rA   r   rG   typingr   pygamer   r   get_extendedr   r   r   r   r   Rectr)   ri   r	   splitabspath__file__r   r   r   r%   r&   r   rE   rS   rZ   r\   ra   r   r>   quitr   r   r   r   <module>   s8   

 
 !