racer2d 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- racer2d-0.1.0/LICENSE +21 -0
- racer2d-0.1.0/PKG-INFO +235 -0
- racer2d-0.1.0/README.md +191 -0
- racer2d-0.1.0/pyproject.toml +43 -0
- racer2d-0.1.0/racer2d/__init__.py +21 -0
- racer2d-0.1.0/racer2d/__main__.py +23 -0
- racer2d-0.1.0/racer2d/assets/car.png +0 -0
- racer2d-0.1.0/racer2d/car.py +105 -0
- racer2d-0.1.0/racer2d/env.py +151 -0
- racer2d-0.1.0/racer2d/logger.py +73 -0
- racer2d-0.1.0/racer2d/maps/map1_oval/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map1_oval/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map1_oval/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map1_oval/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map2_rectangle/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map2_rectangle/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map2_rectangle/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map2_rectangle/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map3_figure8/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map3_figure8/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map3_figure8/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map3_figure8/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map4_wavy/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map4_wavy/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map4_wavy/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map4_wavy/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map5_lshape/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map5_lshape/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map5_lshape/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map5_lshape/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map6_rally/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map6_rally/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map6_rally/config.json +62 -0
- racer2d-0.1.0/racer2d/maps/map6_rally/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map7_obstacle_ring/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map7_obstacle_ring/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map7_obstacle_ring/config.json +42 -0
- racer2d-0.1.0/racer2d/maps/map7_obstacle_ring/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps/map8_obstacle_rally/background.png +0 -0
- racer2d-0.1.0/racer2d/maps/map8_obstacle_rally/border_mask.png +0 -0
- racer2d-0.1.0/racer2d/maps/map8_obstacle_rally/config.json +62 -0
- racer2d-0.1.0/racer2d/maps/map8_obstacle_rally/thumbnail.png +0 -0
- racer2d-0.1.0/racer2d/maps_registry.py +24 -0
- racer2d-0.1.0/racer2d/menu.py +78 -0
- racer2d-0.1.0/racer2d/play.py +121 -0
- racer2d-0.1.0/racer2d/random_policy.py +48 -0
- racer2d-0.1.0/racer2d/sim.py +58 -0
- racer2d-0.1.0/racer2d/tools/generate_maps.py +324 -0
- racer2d-0.1.0/racer2d/track.py +60 -0
- racer2d-0.1.0/racer2d.egg-info/PKG-INFO +235 -0
- racer2d-0.1.0/racer2d.egg-info/SOURCES.txt +54 -0
- racer2d-0.1.0/racer2d.egg-info/dependency_links.txt +1 -0
- racer2d-0.1.0/racer2d.egg-info/entry_points.txt +4 -0
- racer2d-0.1.0/racer2d.egg-info/requires.txt +7 -0
- racer2d-0.1.0/racer2d.egg-info/top_level.txt +1 -0
- racer2d-0.1.0/setup.cfg +4 -0
racer2d-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ErnestBeckham
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
racer2d-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: racer2d
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A human-playable 2D top-down racing game, a behavior-cloning data logger, and a Gymnasium environment wrapping the same simulation.
|
|
5
|
+
Author: ErnestBeckham
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 ErnestBeckham
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/MorningStarTM/AI-Laplace-2D
|
|
29
|
+
Project-URL: Repository, https://github.com/MorningStarTM/AI-Laplace-2D
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
34
|
+
Classifier: Topic :: Games/Entertainment :: Simulation
|
|
35
|
+
Requires-Python: >=3.8
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
License-File: LICENSE
|
|
38
|
+
Requires-Dist: pygame>=2.5
|
|
39
|
+
Requires-Dist: numpy
|
|
40
|
+
Requires-Dist: Pillow
|
|
41
|
+
Requires-Dist: gymnasium>=0.29
|
|
42
|
+
Provides-Extra: train
|
|
43
|
+
Requires-Dist: torch; extra == "train"
|
|
44
|
+
|
|
45
|
+
# racer2d
|
|
46
|
+
|
|
47
|
+
A human-playable 2D top-down racing game, a behavior-cloning data logger, and
|
|
48
|
+
a [Gymnasium](https://gymnasium.farama.org/) environment wrapping the same
|
|
49
|
+
simulation - so you can drive the same tracks by hand to collect data, or
|
|
50
|
+
drive them with a model.
|
|
51
|
+
|
|
52
|
+
## Requirements
|
|
53
|
+
|
|
54
|
+
- Python 3.8+
|
|
55
|
+
- A display is only needed to actually *see* the game/env (`racer2d-play`, or
|
|
56
|
+
`render_mode="human"`). Training/testing the gym env works fully headless
|
|
57
|
+
(see [Testing headlessly / on a server](#testing-headlessly--on-a-server)).
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
**From source (development install):**
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
git clone <this-repo-url>
|
|
65
|
+
cd AI-Laplace-2D
|
|
66
|
+
pip install -e .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`-e` (editable) means edits to the `racer2d/` source take effect immediately
|
|
70
|
+
without reinstalling - use this if you're working on this repo.
|
|
71
|
+
|
|
72
|
+
**From a built wheel/sdist** (e.g. once published to PyPI):
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install racer2d
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Both install the `racer2d` package plus three console scripts: `racer2d-play`,
|
|
79
|
+
`racer2d-random-agent`, `racer2d-generate-maps`.
|
|
80
|
+
|
|
81
|
+
### Verify the install
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
python -c "import racer2d; print(racer2d.__version__); print(racer2d.available_maps())"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Expected output: a version string, then a list of 8 map ids
|
|
88
|
+
(`['map1_oval', 'map2_rectangle', ...]`). If this fails, the package either
|
|
89
|
+
isn't installed or isn't on `PYTHONPATH` for the Python you're running.
|
|
90
|
+
|
|
91
|
+
## Play
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
racer2d-play
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Pick a map, drive with WASD, press `R` to start/stop recording a dataset
|
|
98
|
+
(screenshots + continuous steer/throttle actions + pose/lap state) to
|
|
99
|
+
`./data/`, Esc to quit.
|
|
100
|
+
|
|
101
|
+
## Gym environment
|
|
102
|
+
|
|
103
|
+
### Quick test: random-action rollout
|
|
104
|
+
|
|
105
|
+
No model needed - this is the fastest way to confirm the env actually works
|
|
106
|
+
end to end (reset/step/render, map assets loading, collision, gym
|
|
107
|
+
registration) right after install:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
racer2d-random-agent --map map1_oval --steps 300
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Expected output (exact numbers will vary, it's random):
|
|
114
|
+
```
|
|
115
|
+
episode 0: steps=300 collisions=... laps_completed=0 final_pos=(x, y)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Other useful flags:
|
|
119
|
+
```bash
|
|
120
|
+
racer2d-random-agent --map map7_obstacle_ring --episodes 3 --steps 1000 --seed 0
|
|
121
|
+
racer2d-random-agent --render # opens a visible window instead of running headless
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### In a plain .py file
|
|
125
|
+
|
|
126
|
+
Ready-to-run file: [`examples/random_agent.py`](examples/random_agent.py).
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
python examples/random_agent.py
|
|
130
|
+
python examples/random_agent.py --map map7_obstacle_ring --steps 1000
|
|
131
|
+
python examples/random_agent.py --render # opens a visible window
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### In a Jupyter notebook
|
|
135
|
+
|
|
136
|
+
Ready-to-run notebook: [`examples/random_agent_notebook.ipynb`](examples/random_agent_notebook.ipynb)
|
|
137
|
+
(`pip install matplotlib` if you don't already have it, then open the file
|
|
138
|
+
and run all cells - it loads the env, drives it with random actions, and
|
|
139
|
+
displays the first/last observation frame inline with `plt.imshow`). Minimal
|
|
140
|
+
version of the same cells:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
import racer2d # importing this registers "Racer2D-v0" with gymnasium
|
|
144
|
+
import gymnasium as gym
|
|
145
|
+
import matplotlib.pyplot as plt
|
|
146
|
+
|
|
147
|
+
env = gym.make("Racer2D-v0", map_id="map1_oval", max_steps=300)
|
|
148
|
+
obs, info = env.reset(seed=0)
|
|
149
|
+
plt.imshow(obs) # (800, 1280, 3) uint8 RGB frame
|
|
150
|
+
|
|
151
|
+
for step in range(300):
|
|
152
|
+
action = env.action_space.sample() # <-- swap for your model's output
|
|
153
|
+
obs, reward, terminated, truncated, info = env.step(action)
|
|
154
|
+
if terminated or truncated:
|
|
155
|
+
break
|
|
156
|
+
|
|
157
|
+
plt.imshow(obs)
|
|
158
|
+
print(info)
|
|
159
|
+
env.close()
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Using the env in your own code
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
import gymnasium as gym
|
|
166
|
+
import racer2d # importing this registers "Racer2D-v0" with gymnasium
|
|
167
|
+
|
|
168
|
+
env = gym.make("Racer2D-v0", map_id="map1_oval")
|
|
169
|
+
obs, info = env.reset(seed=0)
|
|
170
|
+
print(obs.shape, obs.dtype) # (800, 1280, 3) uint8 - the camera-following RGB frame
|
|
171
|
+
print(env.action_space) # Box(-1.0, 1.0, (2,), float32) -> [steer, throttle]
|
|
172
|
+
print(env.observation_space)
|
|
173
|
+
|
|
174
|
+
for _ in range(500):
|
|
175
|
+
action = env.action_space.sample() # replace with your model's output
|
|
176
|
+
obs, reward, terminated, truncated, info = env.step(action)
|
|
177
|
+
if terminated or truncated:
|
|
178
|
+
obs, info = env.reset()
|
|
179
|
+
env.close()
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Or construct it directly without going through `gym.make`:
|
|
183
|
+
`from racer2d import RacerEnv; env = RacerEnv(map_id="map1_oval", max_steps=1000)`.
|
|
184
|
+
|
|
185
|
+
`info` (returned every `reset()`/`step()`) carries: `map_id`, `pos`, `angle`,
|
|
186
|
+
`speed`, `checkpoint_idx`, `laps_completed`, `off_track` (whether this tick's
|
|
187
|
+
action ended in a wall/obstacle collision). `reward` is a `0.0` stub today -
|
|
188
|
+
the primary use case is behavior cloning from logged human play
|
|
189
|
+
(`racer2d-play` + `R` to record), not RL - but `info` has everything a real
|
|
190
|
+
reward function would need, so one can be layered on later (e.g. a thin
|
|
191
|
+
wrapper around `RacerEnv`) without changing the env's observation/action
|
|
192
|
+
shape.
|
|
193
|
+
|
|
194
|
+
### Testing headlessly / on a server
|
|
195
|
+
|
|
196
|
+
The env (and `racer2d-random-agent` without `--render`) runs with no visible
|
|
197
|
+
window by default - fine for CI, a remote training box, or any machine
|
|
198
|
+
without a display. If you hit SDL/display errors, force it explicitly before
|
|
199
|
+
anything else imports pygame:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# Linux/macOS
|
|
203
|
+
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy racer2d-random-agent --map map1_oval
|
|
204
|
+
|
|
205
|
+
# Windows (PowerShell)
|
|
206
|
+
$env:SDL_VIDEODRIVER="dummy"; $env:SDL_AUDIODRIVER="dummy"; racer2d-random-agent --map map1_oval
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`render_mode=None` (the default) and `render_mode="rgb_array"` never open a
|
|
210
|
+
window regardless; only `render_mode="human"` does.
|
|
211
|
+
|
|
212
|
+
### Available maps
|
|
213
|
+
|
|
214
|
+
Pass any of these as `map_id=` (or `--map`):
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
import racer2d
|
|
218
|
+
print(racer2d.available_maps())
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
8 generated maps under `racer2d/maps/` (oval, rectangle, figure-8, serpentine,
|
|
222
|
+
L-shape, a large scrolling rally stage, and two with static road obstacles).
|
|
223
|
+
Regenerate/tweak them with `racer2d-generate-maps`
|
|
224
|
+
(see `racer2d/tools/generate_maps.py`).
|
|
225
|
+
|
|
226
|
+
## Troubleshooting
|
|
227
|
+
|
|
228
|
+
- **`ModuleNotFoundError: No module named 'racer2d'`** - the package isn't
|
|
229
|
+
installed for the Python interpreter you're running; re-run `pip install -e .`
|
|
230
|
+
(or `pip install racer2d`) with that same interpreter (`python -m pip install ...`
|
|
231
|
+
if `pip`/`python` resolve to different environments).
|
|
232
|
+
- **`No maps found in racer2d/maps/`** - map assets weren't bundled/found;
|
|
233
|
+
run `racer2d-generate-maps` once to (re)generate them.
|
|
234
|
+
- **Window/display errors on a server or in CI** - see
|
|
235
|
+
[Testing headlessly / on a server](#testing-headlessly--on-a-server) above.
|
racer2d-0.1.0/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# racer2d
|
|
2
|
+
|
|
3
|
+
A human-playable 2D top-down racing game, a behavior-cloning data logger, and
|
|
4
|
+
a [Gymnasium](https://gymnasium.farama.org/) environment wrapping the same
|
|
5
|
+
simulation - so you can drive the same tracks by hand to collect data, or
|
|
6
|
+
drive them with a model.
|
|
7
|
+
|
|
8
|
+
## Requirements
|
|
9
|
+
|
|
10
|
+
- Python 3.8+
|
|
11
|
+
- A display is only needed to actually *see* the game/env (`racer2d-play`, or
|
|
12
|
+
`render_mode="human"`). Training/testing the gym env works fully headless
|
|
13
|
+
(see [Testing headlessly / on a server](#testing-headlessly--on-a-server)).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
**From source (development install):**
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
git clone <this-repo-url>
|
|
21
|
+
cd AI-Laplace-2D
|
|
22
|
+
pip install -e .
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`-e` (editable) means edits to the `racer2d/` source take effect immediately
|
|
26
|
+
without reinstalling - use this if you're working on this repo.
|
|
27
|
+
|
|
28
|
+
**From a built wheel/sdist** (e.g. once published to PyPI):
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install racer2d
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Both install the `racer2d` package plus three console scripts: `racer2d-play`,
|
|
35
|
+
`racer2d-random-agent`, `racer2d-generate-maps`.
|
|
36
|
+
|
|
37
|
+
### Verify the install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
python -c "import racer2d; print(racer2d.__version__); print(racer2d.available_maps())"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Expected output: a version string, then a list of 8 map ids
|
|
44
|
+
(`['map1_oval', 'map2_rectangle', ...]`). If this fails, the package either
|
|
45
|
+
isn't installed or isn't on `PYTHONPATH` for the Python you're running.
|
|
46
|
+
|
|
47
|
+
## Play
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
racer2d-play
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Pick a map, drive with WASD, press `R` to start/stop recording a dataset
|
|
54
|
+
(screenshots + continuous steer/throttle actions + pose/lap state) to
|
|
55
|
+
`./data/`, Esc to quit.
|
|
56
|
+
|
|
57
|
+
## Gym environment
|
|
58
|
+
|
|
59
|
+
### Quick test: random-action rollout
|
|
60
|
+
|
|
61
|
+
No model needed - this is the fastest way to confirm the env actually works
|
|
62
|
+
end to end (reset/step/render, map assets loading, collision, gym
|
|
63
|
+
registration) right after install:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
racer2d-random-agent --map map1_oval --steps 300
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Expected output (exact numbers will vary, it's random):
|
|
70
|
+
```
|
|
71
|
+
episode 0: steps=300 collisions=... laps_completed=0 final_pos=(x, y)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Other useful flags:
|
|
75
|
+
```bash
|
|
76
|
+
racer2d-random-agent --map map7_obstacle_ring --episodes 3 --steps 1000 --seed 0
|
|
77
|
+
racer2d-random-agent --render # opens a visible window instead of running headless
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### In a plain .py file
|
|
81
|
+
|
|
82
|
+
Ready-to-run file: [`examples/random_agent.py`](examples/random_agent.py).
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
python examples/random_agent.py
|
|
86
|
+
python examples/random_agent.py --map map7_obstacle_ring --steps 1000
|
|
87
|
+
python examples/random_agent.py --render # opens a visible window
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### In a Jupyter notebook
|
|
91
|
+
|
|
92
|
+
Ready-to-run notebook: [`examples/random_agent_notebook.ipynb`](examples/random_agent_notebook.ipynb)
|
|
93
|
+
(`pip install matplotlib` if you don't already have it, then open the file
|
|
94
|
+
and run all cells - it loads the env, drives it with random actions, and
|
|
95
|
+
displays the first/last observation frame inline with `plt.imshow`). Minimal
|
|
96
|
+
version of the same cells:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import racer2d # importing this registers "Racer2D-v0" with gymnasium
|
|
100
|
+
import gymnasium as gym
|
|
101
|
+
import matplotlib.pyplot as plt
|
|
102
|
+
|
|
103
|
+
env = gym.make("Racer2D-v0", map_id="map1_oval", max_steps=300)
|
|
104
|
+
obs, info = env.reset(seed=0)
|
|
105
|
+
plt.imshow(obs) # (800, 1280, 3) uint8 RGB frame
|
|
106
|
+
|
|
107
|
+
for step in range(300):
|
|
108
|
+
action = env.action_space.sample() # <-- swap for your model's output
|
|
109
|
+
obs, reward, terminated, truncated, info = env.step(action)
|
|
110
|
+
if terminated or truncated:
|
|
111
|
+
break
|
|
112
|
+
|
|
113
|
+
plt.imshow(obs)
|
|
114
|
+
print(info)
|
|
115
|
+
env.close()
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Using the env in your own code
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
import gymnasium as gym
|
|
122
|
+
import racer2d # importing this registers "Racer2D-v0" with gymnasium
|
|
123
|
+
|
|
124
|
+
env = gym.make("Racer2D-v0", map_id="map1_oval")
|
|
125
|
+
obs, info = env.reset(seed=0)
|
|
126
|
+
print(obs.shape, obs.dtype) # (800, 1280, 3) uint8 - the camera-following RGB frame
|
|
127
|
+
print(env.action_space) # Box(-1.0, 1.0, (2,), float32) -> [steer, throttle]
|
|
128
|
+
print(env.observation_space)
|
|
129
|
+
|
|
130
|
+
for _ in range(500):
|
|
131
|
+
action = env.action_space.sample() # replace with your model's output
|
|
132
|
+
obs, reward, terminated, truncated, info = env.step(action)
|
|
133
|
+
if terminated or truncated:
|
|
134
|
+
obs, info = env.reset()
|
|
135
|
+
env.close()
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Or construct it directly without going through `gym.make`:
|
|
139
|
+
`from racer2d import RacerEnv; env = RacerEnv(map_id="map1_oval", max_steps=1000)`.
|
|
140
|
+
|
|
141
|
+
`info` (returned every `reset()`/`step()`) carries: `map_id`, `pos`, `angle`,
|
|
142
|
+
`speed`, `checkpoint_idx`, `laps_completed`, `off_track` (whether this tick's
|
|
143
|
+
action ended in a wall/obstacle collision). `reward` is a `0.0` stub today -
|
|
144
|
+
the primary use case is behavior cloning from logged human play
|
|
145
|
+
(`racer2d-play` + `R` to record), not RL - but `info` has everything a real
|
|
146
|
+
reward function would need, so one can be layered on later (e.g. a thin
|
|
147
|
+
wrapper around `RacerEnv`) without changing the env's observation/action
|
|
148
|
+
shape.
|
|
149
|
+
|
|
150
|
+
### Testing headlessly / on a server
|
|
151
|
+
|
|
152
|
+
The env (and `racer2d-random-agent` without `--render`) runs with no visible
|
|
153
|
+
window by default - fine for CI, a remote training box, or any machine
|
|
154
|
+
without a display. If you hit SDL/display errors, force it explicitly before
|
|
155
|
+
anything else imports pygame:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# Linux/macOS
|
|
159
|
+
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy racer2d-random-agent --map map1_oval
|
|
160
|
+
|
|
161
|
+
# Windows (PowerShell)
|
|
162
|
+
$env:SDL_VIDEODRIVER="dummy"; $env:SDL_AUDIODRIVER="dummy"; racer2d-random-agent --map map1_oval
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`render_mode=None` (the default) and `render_mode="rgb_array"` never open a
|
|
166
|
+
window regardless; only `render_mode="human"` does.
|
|
167
|
+
|
|
168
|
+
### Available maps
|
|
169
|
+
|
|
170
|
+
Pass any of these as `map_id=` (or `--map`):
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
import racer2d
|
|
174
|
+
print(racer2d.available_maps())
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
8 generated maps under `racer2d/maps/` (oval, rectangle, figure-8, serpentine,
|
|
178
|
+
L-shape, a large scrolling rally stage, and two with static road obstacles).
|
|
179
|
+
Regenerate/tweak them with `racer2d-generate-maps`
|
|
180
|
+
(see `racer2d/tools/generate_maps.py`).
|
|
181
|
+
|
|
182
|
+
## Troubleshooting
|
|
183
|
+
|
|
184
|
+
- **`ModuleNotFoundError: No module named 'racer2d'`** - the package isn't
|
|
185
|
+
installed for the Python interpreter you're running; re-run `pip install -e .`
|
|
186
|
+
(or `pip install racer2d`) with that same interpreter (`python -m pip install ...`
|
|
187
|
+
if `pip`/`python` resolve to different environments).
|
|
188
|
+
- **`No maps found in racer2d/maps/`** - map assets weren't bundled/found;
|
|
189
|
+
run `racer2d-generate-maps` once to (re)generate them.
|
|
190
|
+
- **Window/display errors on a server or in CI** - see
|
|
191
|
+
[Testing headlessly / on a server](#testing-headlessly--on-a-server) above.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "racer2d"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A human-playable 2D top-down racing game, a behavior-cloning data logger, and a Gymnasium environment wrapping the same simulation."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
authors = [{ name = "ErnestBeckham" }]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
18
|
+
"Topic :: Games/Entertainment :: Simulation",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"pygame>=2.5",
|
|
22
|
+
"numpy",
|
|
23
|
+
"Pillow",
|
|
24
|
+
"gymnasium>=0.29",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/MorningStarTM/AI-Laplace-2D"
|
|
29
|
+
Repository = "https://github.com/MorningStarTM/AI-Laplace-2D"
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
train = ["torch"]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
racer2d-play = "racer2d.__main__:main"
|
|
36
|
+
racer2d-random-agent = "racer2d.random_policy:main"
|
|
37
|
+
racer2d-generate-maps = "racer2d.tools.generate_maps:main"
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
include = ["racer2d*"]
|
|
41
|
+
|
|
42
|
+
[tool.setuptools.package-data]
|
|
43
|
+
racer2d = ["maps/**/*", "assets/*"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""racer2d: a human-playable 2D top-down racing game, a behavior-cloning
|
|
2
|
+
data logger, and a Gymnasium environment wrapping the same simulation.
|
|
3
|
+
"""
|
|
4
|
+
__version__ = "0.1.0"
|
|
5
|
+
|
|
6
|
+
from .env import RacerEnv, available_maps
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import gymnasium
|
|
10
|
+
|
|
11
|
+
gymnasium.register(
|
|
12
|
+
id="Racer2D-v0",
|
|
13
|
+
entry_point="racer2d.env:RacerEnv",
|
|
14
|
+
)
|
|
15
|
+
except Exception:
|
|
16
|
+
# Registration is a convenience (lets gymnasium.make("Racer2D-v0", ...)
|
|
17
|
+
# work); importing racer2d directly to use RacerEnv() still works fine
|
|
18
|
+
# without it, so a registration hiccup shouldn't break the import.
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
__all__ = ["RacerEnv", "available_maps", "__version__"]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Entry point: python -m racer2d (or the `racer2d-play` console script)"""
|
|
2
|
+
import pygame
|
|
3
|
+
|
|
4
|
+
from .menu import run_menu
|
|
5
|
+
from .play import run_race, WIDTH, HEIGHT
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main():
|
|
9
|
+
pygame.init()
|
|
10
|
+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
11
|
+
pygame.display.set_caption("Racer2D")
|
|
12
|
+
|
|
13
|
+
while True:
|
|
14
|
+
map_id = run_menu(screen)
|
|
15
|
+
if map_id is None:
|
|
16
|
+
break
|
|
17
|
+
run_race(screen, map_id)
|
|
18
|
+
|
|
19
|
+
pygame.quit()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
main()
|
|
Binary file
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Top-down car with a kinematic-bicycle-model physics core.
|
|
2
|
+
|
|
3
|
+
This is the standard model real arcade racers (and self-driving research
|
|
4
|
+
stacks) use for a top-down car: the heading change each tick is derived from
|
|
5
|
+
an actual front-wheel steering angle and the car's wheelbase, scaled by
|
|
6
|
+
current speed - not an arbitrary fixed "rotation speed". That single change
|
|
7
|
+
is what gets rid of the old model's glitches: it makes braking/reversing and
|
|
8
|
+
steering fall out of real geometry instead of ad hoc per-case hacks, so
|
|
9
|
+
there's no path left for the car to ever move opposite to its own velocity
|
|
10
|
+
sign (the old "lurches backward when you press forward" bug).
|
|
11
|
+
"""
|
|
12
|
+
import math
|
|
13
|
+
|
|
14
|
+
import pygame
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Car:
|
|
18
|
+
"""(x, y) is always the car's CENTER, matching track centerline points."""
|
|
19
|
+
|
|
20
|
+
MAX_SPEED = 5.2
|
|
21
|
+
MAX_REVERSE_SPEED = 3.5
|
|
22
|
+
|
|
23
|
+
ENGINE_ACCEL = 0.40 # speeding up under throttle
|
|
24
|
+
BRAKE_DECEL = 0.65 # slowing down when throttle opposes current motion
|
|
25
|
+
REVERSE_ACCEL = 0.28 # speeding up in reverse once stopped
|
|
26
|
+
COAST_DECEL = 0.10 # rolling/engine resistance with no throttle at all
|
|
27
|
+
|
|
28
|
+
WHEELBASE = 18.0 # px between front/rear axle - sets turn tightness
|
|
29
|
+
MAX_STEER_DEG = 18.0 # max front-wheel angle
|
|
30
|
+
MIN_SPEED_TO_STEER = 0.02
|
|
31
|
+
|
|
32
|
+
def __init__(self, img_path, start_pos, start_angle=0.0):
|
|
33
|
+
self.img = pygame.image.load(img_path).convert_alpha()
|
|
34
|
+
self.start_pos = tuple(start_pos)
|
|
35
|
+
self.start_angle = start_angle
|
|
36
|
+
self.reset()
|
|
37
|
+
|
|
38
|
+
def reset(self):
|
|
39
|
+
self.x, self.y = self.start_pos
|
|
40
|
+
self.angle = self.start_angle
|
|
41
|
+
self.speed = 0.0
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def vel(self):
|
|
45
|
+
"""Backwards-compatible alias some callers/logging use."""
|
|
46
|
+
return self.speed
|
|
47
|
+
|
|
48
|
+
def step(self, throttle: float, steering: float):
|
|
49
|
+
"""Advance physics by one tick. throttle/steering are in [-1, 1]."""
|
|
50
|
+
throttle = max(-1.0, min(1.0, throttle))
|
|
51
|
+
steering = max(-1.0, min(1.0, steering))
|
|
52
|
+
|
|
53
|
+
if throttle > 1e-3:
|
|
54
|
+
if self.speed < 0:
|
|
55
|
+
self.speed += self.BRAKE_DECEL * throttle # braking out of reverse
|
|
56
|
+
else:
|
|
57
|
+
self.speed += self.ENGINE_ACCEL * throttle # accelerating forward
|
|
58
|
+
elif throttle < -1e-3:
|
|
59
|
+
if self.speed > 0:
|
|
60
|
+
self.speed += self.BRAKE_DECEL * throttle # braking out of forward motion
|
|
61
|
+
else:
|
|
62
|
+
self.speed += self.REVERSE_ACCEL * throttle # accelerating backward
|
|
63
|
+
else:
|
|
64
|
+
if self.speed > 0:
|
|
65
|
+
self.speed = max(0.0, self.speed - self.COAST_DECEL)
|
|
66
|
+
elif self.speed < 0:
|
|
67
|
+
self.speed = min(0.0, self.speed + self.COAST_DECEL)
|
|
68
|
+
|
|
69
|
+
self.speed = max(-self.MAX_REVERSE_SPEED, min(self.MAX_SPEED, self.speed))
|
|
70
|
+
|
|
71
|
+
# Kinematic bicycle model: yaw rate = (speed / wheelbase) * tan(steer angle).
|
|
72
|
+
# Turning scales with speed (no spinning in place) and naturally reverses
|
|
73
|
+
# direction when speed is negative, matching how a car actually steers
|
|
74
|
+
# in reverse - no manual sign-flipping needed.
|
|
75
|
+
if abs(self.speed) > self.MIN_SPEED_TO_STEER:
|
|
76
|
+
steer_rad = math.radians(steering * self.MAX_STEER_DEG)
|
|
77
|
+
yaw_rate_deg = math.degrees((self.speed / self.WHEELBASE) * math.tan(steer_rad))
|
|
78
|
+
self.angle -= yaw_rate_deg
|
|
79
|
+
|
|
80
|
+
radians = math.radians(self.angle)
|
|
81
|
+
self.x -= math.sin(radians) * self.speed
|
|
82
|
+
self.y -= math.cos(radians) * self.speed
|
|
83
|
+
|
|
84
|
+
def bounce(self):
|
|
85
|
+
"""Call after a step() that ended up overlapping a wall: the caller
|
|
86
|
+
is expected to have already restored (x, y) to the last safe spot
|
|
87
|
+
(see play.py) so the car can never end up embedded in a wall - only
|
|
88
|
+
damping the velocity here, with no position change, means a slow
|
|
89
|
+
crawl into a wall can't "stick": the car is always left exactly on
|
|
90
|
+
the last valid tile with a speed that pushes it away next tick."""
|
|
91
|
+
self.speed = -self.speed * 0.5
|
|
92
|
+
|
|
93
|
+
def get_mask_and_topleft(self):
|
|
94
|
+
rotated = pygame.transform.rotate(self.img, self.angle)
|
|
95
|
+
rect = rotated.get_rect(center=(self.x, self.y))
|
|
96
|
+
mask = pygame.mask.from_surface(rotated)
|
|
97
|
+
return mask, rect.topleft
|
|
98
|
+
|
|
99
|
+
def draw(self, surface, camera_x=0.0, camera_y=0.0):
|
|
100
|
+
rotated = pygame.transform.rotate(self.img, self.angle)
|
|
101
|
+
rect = rotated.get_rect(center=(self.x - camera_x, self.y - camera_y))
|
|
102
|
+
surface.blit(rotated, rect.topleft)
|
|
103
|
+
|
|
104
|
+
def get_position(self):
|
|
105
|
+
return self.x, self.y
|