
    hs              
          S /r SSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
Jr  SSKJr   SSKrSSKJrJrJr   SSKrSS	KJr  SrSrSrSrSrSrSrS\-  rS\-  rSr Sr!Sr"S\-  r#Sr$S\-  r%S\-  r&Sr'\S-  r(\)" \(\%\#5      \RT                  " S5      -  \!-  \-  r+ " S S\5      r, " S S \RZ                  \5      r.\/S!:X  a  \R`                  " / S"Q5      r1S# r2\." S$S%9r3S&q4[h        (       d  \3Rk                  5         S'r6Sr7S&q8 \2" 5         \3Rs                  \15      u  r:r;r<r=r>\6\;-  r6\7S(-  S:X  d  \<(       d  \=(       a2  \?" S)\@" \1 V s/ s H  o S* PM     sn 5      -   5        \?" S+\7 S,\6S* 35        \7S--  r7\<(       d  \=(       d  [p        (       d  [h        (       a  OM  [h        (       d  M  \3R                  5         gg! \ a  r\
" S5      \eSrCff = f! \ a  r\
" S
5      \eSrCff = fs  sn f ).u   Andrea PIERRÉ    N)spaces)Car)DependencyNotInstalledInvalidAction)EzPickle)contactListener
fixtureDefpolygonShapezqBox2D is not installed, you can install it by run `pip install swig` followed by `pip install "gymnasium[box2d]"`)gfxdrawz=pygame is not installed, run `pip install "gymnasium[box2d]"``   iX  i  i  i   g      @i  i  2   g@T   gףp=
?(         g      4@   c                   ,    \ rS rSrS rS rS rS rSrg)FrictionDetector;   c                 H    [         R                  " U 5        Xl        X l        g N)r   __init__envlap_complete_percent)selfr   r   s      Y/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/box2d/car_racing.pyr   FrictionDetector.__init__<   s      &$8!    c                 (    U R                  US5        g )NT_contactr   contacts     r   BeginContactFrictionDetector.BeginContactA   s    gt$r   c                 (    U R                  US5        g NFr    r"   s     r   
EndContactFrictionDetector.EndContactD   s    gu%r   c                    S nS nUR                   R                  R                  nUR                  R                  R                  nU(       a  SUR                  ;   a  UnUnU(       a  SUR                  ;   a  UnUnU(       d  g U R
                  R                  UR                  S S & U(       a  SUR                  ;  a  g U(       a  UR                  R                  U5        UR                  (       d  SUl
        U R
                  =R                  S[        U R
                  R                  5      -  -  sl        U R
                  =R                  S-  sl        UR                  S:X  aW  U R
                  R                  [        U R
                  R                  5      -  U R                   :  a  SU R
                  l        g g g g UR                  R%                  U5        g )Nroad_frictiontilesTg     @@   r   )fixtureAbodyuserDatafixtureB__dict__r   
road_colorcolorr,   addroad_visitedrewardlentracktile_visited_countidxr   new_lapremove)r   r#   begintileobju1u2s          r   r!   FrictionDetector._contactG   sb   ""++""++/R[[0DC/R[[0DC ++

1gS\\1IIMM$$$$(!6C,?#??++q0+ HHM33c$((..6II//0 (,DHH$0 " % IIT"r   )r   r   N)	__name__
__module____qualname____firstlineno__r   r$   r(   r!   __static_attributes__ r   r   r   r   ;   s    9
%&!#r   r   c                     ^  \ rS rSrSr/ SQ\S.r     SS\S-  S\S\	S	\S
\4
S jjr
S rS rS rS rSSS.S\S-  S\S-  4U 4S jjjrS\R&                  \-  4S jrS rS\4S jrS rS r S S jrS rS rSrU =r$ )!	CarRacingk   a  
## Description
The easiest control task to learn from pixels - a top-down
racing environment. The generated track is random every episode.

Some indicators are shown at the bottom of the window along with the
state RGB buffer. From left to right: true speed, four ABS sensors,
steering wheel position, and gyroscope.
To play yourself (it's rather fast for humans), type:
```shell
python gymnasium/envs/box2d/car_racing.py
```
Remember: it's a powerful rear-wheel drive car - don't press the accelerator
and turn at the same time.

## Action Space
If continuous there are 3 actions :
- 0: steering, -1 is full left, +1 is full right
- 1: gas
- 2: braking

If discrete there are 5 actions:
- 0: do nothing
- 1: steer right
- 2: steer left
- 3: gas
- 4: brake

## Observation Space

A top-down 96x96 RGB image of the car and race track.

## Rewards
The reward is -0.1 every frame and +1000/N for every track tile visited, where N is the total number of tiles
 visited in the track. For example, if you have finished in 732 frames, your reward is 1000 - 0.1*732 = 926.8 points.

## Starting State
The car starts at rest in the center of the road.

## Episode Termination
The episode finishes when all the tiles are visited. The car can also go outside the playfield -
 that is, far off the track, in which case it will receive -100 reward and die.

## Arguments

```python
>>> import gymnasium as gym
>>> env = gym.make("CarRacing-v3", render_mode="rgb_array", lap_complete_percent=0.95, domain_randomize=False, continuous=False)
>>> env
<TimeLimit<OrderEnforcing<PassiveEnvChecker<CarRacing<CarRacing-v3>>>>>

```

* `lap_complete_percent=0.95` dictates the percentage of tiles that must be visited by
 the agent before a lap is considered complete.

* `domain_randomize=False` enables the domain randomized variant of the environment.
 In this scenario, the background and track colours are different on every reset.

* `continuous=True` specifies if the agent has continuous (true) or discrete (false) actions.
 See action space section for a description of each.

## Reset Arguments

Passing the option `options["randomize"] = True` will change the current colour of the environment on demand.
Correspondingly, passing the option `options["randomize"] = False` will not change the current colour of the environment.
`domain_randomize` must be `True` on init for this argument to work.

```python
>>> import gymnasium as gym
>>> env = gym.make("CarRacing-v3", domain_randomize=True)

# normal reset, this changes the colour scheme by default
>>> obs, _ = env.reset()

# reset with colour scheme change
>>> randomize_obs, _ = env.reset(options={"randomize": True})

# reset with no colour scheme change
>>> non_random_obs, _ = env.reset(options={"randomize": False})

```

## Version History
- v2: Change truncation to termination when finishing the lap (1.0.0)
- v1: Change track completion logic and add domain randomization (0.24.0)
- v0: Original version

## References
- Chris Campbell (2014), http://www.iforce2d.net/b2dtut/top-down-car.

## Credits
Created by Oleg Klimov
)human	rgb_arraystate_pixels)render_modes
render_fpsNrender_modeverboser   domain_randomize
continuousc                    [         R                  " U UUUUU5        XPl        X@l        X0l        U R                  5         [        X R                  5      U l        [        R                  " SU R                  S9U l
        S U l        S U l        S U l        SU l        S U l        S U l        S U l        S U l        SU l        SU l        X l        SU l        [/        [1        / SQS9S9U l        U R                  (       a  [4        R6                  " [8        R:                  " / S	Q5      R=                  [8        R>                  5      [8        R:                  " / S
Q5      R=                  [8        R>                  5      5      U l         O[4        RB                  " S5      U l         [4        R6                  " SS[D        [F        S4[8        RH                  S9U l%        Xl&        g )Nr   r   )r   T        F)rW   )r-   r   )r-   )r   rY   )vertices)shape)rY   r   r   )r-   r-   r-      r         )lowhighr[   dtype)'r   r   rU   rT   r   _init_colorsr   contactListener_keeprefBox2Db2Worldworldscreensurfclockisopeninvisible_state_windowinvisible_video_windowroadcarr7   prev_rewardrS   r<   r	   r
   fd_tiler   Boxnparrayastypefloat32action_spaceDiscreteSTATE_HSTATE_Wuint8observation_spacerR   )r   rR   rS   r   rT   rU   s         r   r   CarRacing.__init__   sj    	 	
 % 0$8!'7>W>W'X$]]64;W;WX
-1	
&*#&*#	#!(JK
 ?? &

$++BJJ7&--bjj9!D
 !' 2D "(GWa#8"
 'r   c                     U R                   (       d  g U R                    H  nU R                  R                  U5        M      / U l         U R                  c   eU R                  R	                  5         g r   )rm   rf   DestroyBodyrn   destroy)r   ts     r   _destroyCarRacing._destroy  sS    yyAJJ""1% 	xx###r   c                    U R                   (       a  U R                  R                  SSSS9U l        U R                  R                  SSSS9U l        [
        R                  " U R                  5      U l        U R                  R                  S5      nU R                  U==   S-  ss'   g [
        R                  " / SQ5      U l        [
        R                  " / SQ5      U l        [
        R                  " / SQ5      U l        g )	Nr      r^   size   )f   r   r   )r      r   )r      r   )
rT   	np_randomuniformr3   bg_colorrr   copygrass_colorintegersrs   )r   r;   s     r   rb   CarRacing._init_colors  s      "nn44Q!4DDO NN221c2BDM!wwt}}5D..))!,CS!R'! !hh7DOHH_5DM!xx8Dr   c                 r   U R                   (       d   S5       eU(       a  U R                  R                  SSSS9U l        U R                  R                  SSSS9U l        [
        R                  " U R                  5      U l        U R                  R                  S5      nU R                  U==   S-  ss'   g g )Nz3domain_randomize must be True to use this function.r   r   r^   r   r   )	rT   r   r   r3   r   rr   r   r   r   )r   	randomizer;   s      r   _reinit_colorsCarRacing._reinit_colors%  s    !!	A@	A! "nn44Q!4DDO NN221c2BDM!wwt}}5D..))!,CS!R'! r   c           	         Sn/ n[        U5       GH  nU R                  R                  SS[        R                  -  S-  U-  5      nS[        R                  -  U-  U-  U-   nU R                  R                  [
        S-  [
        5      nUS:X  a  SnS[
        -  nX1S-
  :X  a@  S[        R                  -  U-  U-  nS[        R                  -  S-  U-  U l        S[
        -  nUR                  XV[        R                  " U5      -  U[        R                  " U5      -  45        GM     / U l
        S[
        -  SSpnSn
Sn/ nSnS	n [        R                  " X5      nU(       a  US:  a  US-  nS	nUS:  a  S
nUS[        R                  -  -  n S
n X*[        U5      -     u  nnnUU::  a  S	nOU
S-  n
U
[        U5      -  S:X  a  OM7  U(       d  OUS[        R                  -  -  nM[  [        R                  " U	5      n[        R                  " U	5      nU* nUnUU-
  nUU-
  nUU-  UU-  -   nX-
  S[        R                  -  :  a1  U	S[        R                  -  -  n	X-
  S[        R                  -  :  a  M1  X-
  S[        R                  -  :  a1  U	S[        R                  -  -  n	X-
  S[        R                  -  :  a  M1  U	nU[        -  nUS:  a  U	[        [        [!        SU-  5      5      -  n	US:  a  U	[        [        [!        SU-  5      5      -  n	UU["        -  -  nUU["        -  -  nUR                  UUS-  U	S-  -   Xx45        US:  a  OUS-  nUS:X  a  OGM  Su  nn[        U5      n US-  nUS:X  a  g	UU   S   U R                  :  =(       a    UUS-
     S   U R                  :*  nU(       a	  US:X  a  UnOU(       a	  US:X  a  UnOMb  U R$                  (       a  ['        SUUUU-
  4-  5        US:w  d   eUS:w  d   eUUUS-
   nUS   S   n[        R                  " U5      n [        R                  " U5      n![(        R*                  " [(        R,                  " U US   S   US   S   -
  -  5      [(        R,                  " U!US   S   US   S   -
  -  5      -   5      n"U"["        :  a  g	S	/[        U5      -  n#[        [        U5      5       H  nS
n$Sn%[        [.        5       HV  n&UUU&-
  S-
     S   n'UUU&-
  S-
     S   n(U$[!        U'U(-
  5      [        S-  :  -  n$U%[(        R0                  " U'U(-
  5      -  n%MX     U$[!        U%5      [.        :H  -  n$U$U#U'   M     [        [        U5      5       H,  n[        [.        5       H  n&U#UU&-
  ==   U#U   -  ss'   M     M.     [        [        U5      5       GHz  nUU   u  n)n'n*n+UUS-
     u  n,n(n-n.U*[2        [        R                  " U'5      -  -
  U+[2        [        R                  " U'5      -  -
  4n/U*[2        [        R                  " U'5      -  -   U+[2        [        R                  " U'5      -  -   4n0U-[2        [        R                  " U(5      -  -
  U.[2        [        R                  " U(5      -  -
  4n1U-[2        [        R                  " U(5      -  -   U.[2        [        R                  " U(5      -  -   4n2U/U0U2U1/n3U3U R4                  R6                  l        U R:                  R=                  U R4                  S9n4U4U4l        SUS-  -  S-  nU R@                  U-   U4l!        S	U4l"        SU4l#        UU4l$        S
U4RJ                  S   l&        U RN                  R                  U/U0U2U1/U4RB                  45        U R                  R                  U45        U#U   (       d  GM  [(        R0                  " U(U'-
  5      n5U*U5[2        -  [        R                  " U'5      -  -   U+U5[2        -  [        R                  " U'5      -  -   4n6U*U5[2        [P        -   -  [        R                  " U'5      -  -   U+U5[2        [P        -   -  [        R                  " U'5      -  -   4n7U-U5[2        -  [        R                  " U(5      -  -   U.U5[2        -  [        R                  " U(5      -  -   4n8U-U5[2        [P        -   -  [        R                  " U(5      -  -   U.U5[2        [P        -   -  [        R                  " U(5      -  -   4n9U RN                  R                  U6U7U9U8/US-  S:X  a  SOS45        GM}     Xl)        g
)N   r   r   r-   r^   g      ?g      i	  FTg      g333333?gMbP?g333333ӿg      ?r   )rY   rY   rY   z*Track generation: %i..%i -> %i-tiles track皙?)fixtures{Gz?r]         ?r]   r]   r]   r]   r   r   )*ranger   r   mathpi	TRACK_RADstart_alphaappendcossinrm   atan2r8   SCALEminTRACK_TURN_RATEabsTRACK_DETAIL_STEPrS   printrr   sqrtsquareBORDER_MIN_COUNTsignTRACK_WIDTHrp   r[   rZ   rf   CreateStaticBodyr0   r3   r4   r6   r+   r;   r   sensor	road_polyBORDERr9   ):r   CHECKPOINTScheckpointscnoisealpharadxybetadest_ilapsr9   	no_freezevisited_other_sidefailed
dest_alphadest_xdest_yr1xr1yp1xp1ydest_dxdest_dyproj	prev_betai1i2ipass_through_start
first_betafirst_perp_xfirst_perp_ywell_glued_togetherbordergoodonesidenegbeta1beta2alpha1x1y1alpha2x2y2road1_lroad1_rroad2_lroad2_rrZ   r   sideb1_lb1_rb2_lb2_rs:                                                             r   _create_trackCarRacing._create_track4  s    {#ANN**1a$''kAo.KLEK!Ok1E9E..((Q	BCAvIo!O#DGGa+5#$tww;$#7+#E IoTXXe_'<cDHHUO>STU $ 	 9_ad	"JJq$E!eai	%*"qy%)"TWW$1<c+FV=V1W.J
*!&aKFK 00A5  TWW$((4.C((4.C$CCqjGqjG=3=0D,tww.DGG# ,tww.,/DGG# ,/IEMDczOS->??d{OS->??(((A(((ALL%S4#:!=qDEaxNIA~i n BJFAAvad...V5Q<?dFVFV3V  "bBh#b  <<>"b"r'ARRSRxxRxxb26"1Xa[
xx
+xx
+ ggIIleAhqkE"IaL&@ABiiaeBil(BCDE
 !22 3u:%s5z"ADG-.a#gk*1-a#gk*1-EEM*_s-BBB27755=11	 /
 CL$444DF1I # s5z"A-.q3w6!9, / #
 s5z"A$)!H!FE2r$)!a%L!FE2r[488E?22[488E?22G
 [488E?22[488E?22G
 [488E?22[488E?22G
 [488E?22[488E?22G  '7;H*2DLL'

++T\\+BAAJA$Aoo)AG"AN!AOAE#'AJJqM NN!!GWgw#G"QRIIQayywwuu}-+dhhuo==+dhhuo==
 v!56%HHv!56%HH
 +dhhuo==+dhhuo==
 v!56%HHv!56%HH %%tT40+,q5A:;c #n 
r   )seedoptionsr   r   c                  > [         TU ]  US9  U R                  5         [        X R                  5      U R
                  l        U R
                  R                  U R
                  l        SU l        SU l	        SU l
        SU l        SU l        / U l        U R                  (       a3  Sn[        U[         5      (       a  SU;   a  US   nU R#                  U5         U R%                  5       nU(       a  OU R&                  (       a  [)        S5        M6  [+        U R
                  /U R,                  S   SS	 Q76 U l        U R0                  S
:X  a  U R3                  5         U R5                  S 5      S   0 4$ )N)r   rX   r   FTr   zOretry to generate track (normal if there are not manyinstances of this message)r-   r   rM   )superresetr   r   r   rf   contactListener_bug_workaroundr   r7   ro   r:   r   r<   r   rT   
isinstancedictr   r   rS   r   r   r9   rn   rR   renderstep)r   r   r   r   success	__class__s        r   r   CarRacing.reset  s:    	4 4D++5


1 &*ZZ%N%N

""#  I'4((') ' 4I	*((*G||1  tzz7DJJqM!A$67w&KKMyyq!2%%r   actionc                    U R                   c   eUGb4  U R                  (       a{  UR                  [        R                  5      nU R                   R                  US   * 5        U R                   R                  US   5        U R                   R                  US   5        OU R                  R                  U5      (       d  [        SU SU R                   S35      eU R                   R                  SUS:H  -  SUS:H  -  -   5        U R                   R                  S	US
:H  -  5        U R                   R                  SUS:H  -  5        U R                   R                  S[        -  5        U R                  R                  S[        -  SS5        U =R                  S[        -  -  sl        U R!                  S5      U l        SnSnSn0 nUb  U =R$                  S-  sl        SU R                   l        U R$                  U R(                  -
  nU R$                  U l        U R*                  [-        U R.                  5      :X  d  U R0                  (       a  SnSUS'   U R                   R2                  R4                  u  pg[7        U5      [8        :  d  [7        U5      [8        :  a	  SnSUS'   SnU R:                  S:X  a  U R=                  5         U R"                  X#XE4$ )Nr   r-   r   zyou passed the invalid action `z"`. The supported action_space is ``g333333g333333?r   r^   皙?r   r      <   rO   F皙?rX   Tlap_finishedirM   )rn   rU   rt   rr   float64steergasbrakerv   containsr   r   FPSrf   Stepr   _renderstater7   
fuel_spentro   r:   r8   r9   r<   hullpositionr   	PLAYFIELDrR   r   )r   r   step_reward
terminated	truncatedinfor   r   s           r   r   CarRacing.step  sF   xx###rzz2q	z*VAY'vay)((11&99'9& B::>:K:K9LAO  tv{3cVq[6IIJSFaK01sfk23cCi 

c	662#)\\.1

	KK3K #&DHH++(8(88K#{{D&&#djj/9T\\!
'+^$88==))DA1v	!SVi%7!
',^$"w&KKMzz;ICCr   c                     U R                   cG  U R                  c   e[        R                  R	                  SU R                  R
                   S35        g U R                  U R                   5      $ )NzYou are calling render method without specifying any render mode. You can specify the render_mode at initialization, e.g. gym.make("z", render_mode="rgb_array"))rR   specgymloggerwarnidr
  r   s    r   r   CarRacing.renderM  sc    #99(((JJOO""&)),,/JL
 << 0 011r   modec                    XR                   S   ;   d   e[        R                  R                  5         U R                  cg  US:X  aa  [        R                  " 5         [        R
                  R                  5         [        R
                  R                  [        [        45      U l        U R                  c#  [        R                  R                  5       U l	        SU R                  ;  a  g [        R                  " [        [        45      U l        U R                  c   eU R                  R                   R"                  * nS[$        -  ['        SU R(                  -
  S5      -  [*        [$        -  [-        U R(                  S5      -  -   nU R                  R                   R.                  S   * U-  nU R                  R                   R.                  S   * U-  n[        R0                  R3                  XE45      R5                  U5      n[        S-  US   -   [        S-  US   -   4nU R7                  X6U5        U R                  R9                  U R                  UUUUS	;  5        [        R:                  R=                  U R                  S
S5      U l        U R?                  [        [        5        [        R                  RA                  [        R                  RC                  5       S5      nURE                  SU RF                  -  SSS5      nURI                  5       n	S[        [        S-  S-  -
  4U	l%        U R                  RM                  X5        US:X  a  [        RN                  RQ                  5         U R                  RS                  U R                   S   5        U R                  c   eU R                  RU                  S5        U R                  RM                  U R                  S5        [        R
                  R=                  5         g US:X  a&  U RW                  U R                  [X        [Z        45      $ US:X  a&  U RW                  U R                  [\        [^        45      $ U R`                  $ )NrP   rM   r   r  r-   r   r   r   )state_pixels_listrO   FT*   z%04ir   r   r   r   r   g      @      D@rQ   rW   rN   rO   )1metadatapygamefontinitrg   displayset_modeWINDOW_WWINDOW_Hri   timeClockr2   Surfacerh   rn   r  angler   maxr   ZOOMr   r  r   Vector2
rotate_rad_render_roaddraw	transformflip_render_indicatorsFontget_default_fontr   r7   get_rectcenterbliteventpumptickfill_create_image_arrayVIDEO_WVIDEO_Hry   rx   rj   )
r   r  r.  zoomscroll_xscroll_ytransr%  text	text_rects
             r   r
  CarRacing._renderY  s=   }}^4444;;47?KKMNN! ..118X2FGDK::**,DJdmm#NNHh#78	xx###$$$U{STVVQ//$,TVVQ2OOXX]]++A./$6XX]]++A./$6##X$89DDUKAa((Q,q*AB$u-II==	
 $$))$))UDA	 	(3{{ < < >C{{6DKK/	RMMO	8c>D+@ @A			t'7?LLJJOODMM,78;;***KKQKKTYY/NN![ ++DII7IJJ^#++DII7IJJ;;r   c           
         [         nXD4XD* 4U* U* 4U* U4/nU R                  U R                  XPR                  XUSS9  / n[	        SSS5       H  n[	        SSS5       H  nUR                  [        U-  [        -   [        U-  S-   4[        U-  S-   [        U-  S-   4[        U-  S-   [        U-  [        -   4[        U-  [        -   [        U-  [        -   4/5        M     M     U H+  n	U R                  U R                  XR                  XU5        M-     U R                   HW  u  pU	 Vs/ s H  oS   US   4PM     n	nU
 Vs/ s H  n[        U5      PM     n
nU R                  U R                  XXU5        MY     g s  snf s  snf )NF)clipir   r   r   r-   )
r  _draw_colored_polygonrh   r   r   r   	GRASS_DIMr   r   int)r   rD  translationr.  boundsfieldgrassr   r   polyr4   pr   s                r   r3  CarRacing._render_road  s   WWvgWf	
 	""IIummTE 	# 	

 sB"A3A&"Q2IMA4EF"Q*IMA,=>"Q*IMI,EF"Q2IMI4MN	 ' # D&&		4!1!14e   >>KD*./$QqT1Q4L$D/%*+USVUE+&&tyy$tRWX	 */+s   +E>Fc           	      B  ^ ^^	^
 US-  m
TS-  m	SnUT4UTST	-  -
  4STST	-  -
  4ST4/n[         R                  R                  T R                  X4S9  UU	U
4S jnUU	U
4S jnT R                  c   e[
        R                  " [
        R                  " T R                  R                  R                  S   5      [
        R                  " T R                  R                  R                  S   5      -   5      nU 4S	 jnU" Xu" SS
U-  5      S5        U" T R                  R                  S   R                  U" SST R                  R                  S   R                  -  5      S5        U" T R                  R                  S   R                  U" SST R                  R                  S   R                  -  5      S5        U" T R                  R                  S   R                  U" SST R                  R                  S   R                  -  5      S5        U" T R                  R                  S   R                  U" SST R                  R                  S   R                  -  5      S5        U" T R                  R                  S   R                  R                  U" SST R                  R                  S   R                  R                  -  5      S5        U" T R                  R                  R                  U" SST R                  R                  R                  -  5      S5        g )Nr"  r!  r\   r   )r4   pointsc                 z   > U T-  TTTU-  -   -
  4U S-   T-  TTTU-  -   -
  4U S-   T-  TT-
  4U S-   T-  TT-
  4/$ )Nr-   r   rI   placevalHhss     r   vertical_ind2CarRacing._render_indicators.<locals>.vertical_ind  sk    AQW-.!)q!q1s7{"34!)q!a%(!)q!a%(	 r   c                 |   > U S-   T-  TST-  -
  4X-   T-  TST-  -
  4X-   T-  TST-  -
  4U S-   T-  TST-  -
  4/$ )Nr   r   r   rI   rZ  s     r   	horiz_ind/CarRacing._render_indicators.<locals>.horiz_ind  sk    !)q!a!e),+"AAI.+"AAI.!)q!a!e),	 r   r-   c                 v   > [        U 5      S:  a)  [        R                  R                  TR                  XS9  g g )Ng-C6?)rX  r4   )r   r$  r4  polygonrh   )valuerX  r4   r   s      r   render_if_min3CarRacing._render_indicators.<locals>.render_if_min  s/    5zD ##DIIf#J !r   g{Gz?r      r   )r   r   r]   r   r   	   )3   r   r]   r^   
   r   g      $)r   r]   r      gr   )r$  r4  rf  rh   rn   rr   r   r   r  linearVelocitywheelsomegajointr.  angularVelocity)r   Wr]  r4   rf  r`  rc  
true_speedrh  r^  r_  s   ` `      @@r   r7  CarRacing._render_indicators  s   HHq6Aq1q5y>Aq1q5y>Aq6BDIIUC		 xx###WWIIdhhmm22156ii44Q789

	K 	j,q$2C"DoVHHOOA$$D488??1#5#;#;;<	

 	HHOOA$$D488??1#5#;#;;<	

 	HHOOA$$D488??1#5#;#;;<	

 	HHOOA$$TDHHOOA$6$<$<<=	
 	HHOOA$$**b%$((//!"4":":"@"@@A	

 	HHMM))b$!>!>>?	
r   c                    U Vs/ s H1  n[         R                  R                  U5      R                  U5      PM3     nnU Vs/ s H  oS   U-  US   -   US   U-  US   -   4PM      nnU(       a  [	        S U 5       5      (       aC  [
        R                  " U R                  X#5        [
        R                  " U R                  X#5        g g s  snf s  snf )Nr   r-   c              3      #    U  H\  n[         * US    s=:*  =(       a    [        [         -   :*  Os  =(       a(    [         * US   s=:*  =(       a    [        [         -   :*  Os  v   M^     g7f)r   r-   N)MAX_SHAPE_DIMr)  r*  ).0coords     r   	<genexpr>2CarRacing._draw_colored_polygon.<locals>.<genexpr>  sX      
  ^uQxCC8m+CC I58GGx-/GGIs   A$A&)	r$  r   r1  r2  anyr   	aapolygonrh   filled_polygon)	r   surfacerT  r4   rD  rP  r.  rL  r   s	            r   rM  CarRacing._draw_colored_polygon  s     CGG$Q##A&11%8$GRV
RVQqTD[;q>)1Q4$;Q+GHRV 	 
 s 
 
 
 

 dii5""499d:
 H
s   8C%Cc                     [         R                  R                  X5      n[        R                  " [        R
                  " [         R                  R                  U5      5      SS9$ )N)r-   r   r   )axes)r$  r5  smoothscalerr   	transposers   	surfarraypixels3d)r   rg   r   scaled_screens       r   rA  CarRacing._create_image_array  sH    ((44VB||HHV%%..}=>Y
 	
r   c                     U R                   b;  [        R                  R                  5         SU l        [        R                  " 5         g g r'   )rg   r$  r'  quitrj   r  s    r   closeCarRacing.close  s1    ;;"NN!DKKKM #r   )rv   r   rn   ri   rc   rU   rT   rp   r   rk   rl   rj   r   r<   r{   ro   rR   r7   rm   r3   r   rg   r   r  rh   r   r:   r9   rS   rf   )NFgffffff?FT)T)rD   rE   rF   rG   __doc__r  r#  strboolfloatr   r   rb   r   r   rO  r   r   rr   ndarrayr   r   r
  r3  r7  rM  rA  r  rH   __classcell__)r   s   @r   rK   rK   k   s    ]@

 H #'&*!&6'4Z6' 6' $	6'
 6' 6'p9 (|B  #	(& Dj(& 	(& (&T/D2::+ /Db
2:C :x$YLB
J DH;(
 r   rK   __main__)rX   rX   rX   c                      [         R                  R                  5        GH  n U R                  [         R                  :X  a  U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a  SqU R
                  [         R                  :X  a  SqU R                  [         R                  :X  a  U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R
                  [         R                  :X  a	  S[        S'   U R                  [         R                   :X  d  GM  SqGM     g )Ng      r   r   r-   r   r   T)r$  r=  gettypeKEYDOWNkeyK_LEFTaK_RIGHTK_UPK_DOWNK_RETURNrestartK_ESCAPEr  KEYUPQUIT)r=  s    r   register_inputr  #  s@   \\%%'EzzV^^+99-AaD99.AaD99+AaD99-AaD99/"G99/DzzV\\)99-AaD99.AaD99+AaD99-AaDzzV[[(5 (r   rM   )rR   FrX      z
action z+0.2fzstep z total_reward r-   )B__credits__r   numpyrr   	gymnasiumr  r   !gymnasium.envs.box2d.car_dynamicsr   gymnasium.errorr   r   gymnasium.utilsr   rd   Box2D.b2r   r	   r
   ImportErrorer$  r   ry   rx   rB  rC  r)  r*  r   r   r  r  r0  ZOOM_FOLLOWr   r   r   r   r   rN  r/  r   ry  r   EnvrK   rD   rs   r  r  r   r  r   total_rewardstepsr  r   r_  rr  r  r  r   r  r  )r   s   0r   <module>r     sJ        1 A $BB  


%K	5L	
 J 5j	
U 		; 12TYYq\ADH5P 
-# -#`r
 r
j z
!A< 
(CDd		03-Aq*iALs{a:kCq(Aq!e9q(A$BBCeWN<2FGHQJEY''TT  d IIKg _  
 {  
 Gf )Bs4   F< 
G G*<G	GGG'	G""G'