Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ThinkPython/code/Highway.py at master · themycode/ThinkPython · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
themycode
ThinkPython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ThinkPython
/
code
/
Highway.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
199 lines (150 loc) · 5.24 KB
master
Breadcrumbs
ThinkPython
/
code
/
Highway.py
Copy path
Top
File metadata and controls
Code
Blame
199 lines (150 loc) · 5.24 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
""" Code example from Complexity and Computation, a book about
exploring complexity science with Python. Available free from
http://greenteapress.com/complexity
Copyright 2011 Allen B. Downey.
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
"""
import
math
import
random
from
swampy
.
TurtleWorld
import
TurtleWorld
,
Turtle
import
color_list
class
Highway
(
TurtleWorld
):
"""A circular Highway with one lane that spirals down the canvas.
Attributes:
rows: the number of rows that spiral down the canvas.
delay: time between steps in seconds
colors: list of RGB strings
"""
def
__init__
(
self
):
TurtleWorld
.
__init__
(
self
)
self
.
rows
=
2.0
self
.
delay
=
0.01
self
.
colors
=
color_list
.
make_color_dict
().
values
()
def
random_color
(
self
):
"""Returns a random RGB string in #RRGGBB format."""
return
random
.
choice
(
self
.
colors
)
def
get_length
(
self
):
"""Returns the total length of this highway."""
return
self
.
ca_width
*
self
.
rows
def
project
(
self
,
turtle
):
"""Project the turtle onto the highway.
Reads the Turtle's position position and returns its
x,y coordinates.
"""
p
=
1.0
*
turtle
.
position
%
self
.
get_length
()
x
=
p
%
self
.
ca_width
y
=
p
/
self
.
ca_width
*
self
.
ca_height
/
self
.
rows
return
x
,
y
def
lane_heading
(
self
):
"""Returns the heading that aligns the turtle with the lane."""
x
=
self
.
ca_width
y
=
1.0
*
self
.
ca_height
/
self
.
rows
angle
=
math
.
atan2
(
y
,
x
)
heading
=
angle
*
180
/
math
.
pi
return
heading
def
step
(
self
):
"""Performs one time step."""
TurtleWorld
.
step
(
self
)
# compute average turtle speed
total
=
0.0
for
turtle
in
self
.
animals
:
total
+=
turtle
.
speed
print
total
/
len
(
self
.
animals
)
def
make_drivers
(
self
,
n
,
constructor
):
"""Make drivers at random positions.
Args:
n: number of Drivers
constructor: function that returns a Driver
Returns:
a list of Drivers.
"""
t
=
[]
for
i
in
range
(
n
):
turtle
=
constructor
(
self
)
t
.
append
((
turtle
.
position
,
turtle
))
# link up the drivers so each has an attribute (next) that
# refers to the driver in front
t
.
sort
()
turtles
=
[
t
[
1
]
for
t
in
t
]
for
i
in
range
(
n
-
1
):
turtles
[
i
].
next
=
turtles
[
i
+
1
]
turtles
[
-
1
].
next
=
turtles
[
0
]
return
turtles
class
Driver
(
Turtle
):
"""A Turtle with a random position, speed and color."""
def
__init__
(
self
,
*
args
,
**
kwds
):
Turtle
.
__init__
(
self
,
*
args
,
**
kwds
)
self
.
delay
=
0
self
.
color
=
self
.
world
.
random_color
()
self
.
position
=
random
.
randrange
(
0
,
self
.
world
.
get_length
())
self
.
speed_limit
=
4
self
.
speed
=
random
.
randrange
(
0
,
self
.
speed_limit
)
self
.
safe_distance
=
30
self
.
heading
=
self
.
world
.
lane_heading
()
self
.
next
=
None
self
.
project
()
self
.
redraw
()
def
project
(
self
):
"""Sets x and y according to position."""
self
.
x
,
self
.
y
=
self
.
world
.
project
(
self
)
def
accelerate
(
self
,
change
):
"""Speeds up the Turtle by the given amount, within bounds."""
self
.
speed
+=
change
if
self
.
speed
<
0
:
self
.
speed
=
0
if
self
.
speed
>
self
.
speed_limit
:
self
.
speed
=
self
.
speed_limit
def
brake
(
self
,
change
):
"""Slows down the Turtle by the given amount, within bounds."""
self
.
accelerate
(
-
change
)
def
step
(
self
):
"""Checks the distance to the next driver, adjusts speed, and moves.
This function enforces the rules for all drivers.
Driver decision-making is in choose_acceleration().
"""
dist
=
self
.
find_distance
()
# get acceleration
change
=
self
.
choose_acceleration
(
dist
)
self
.
accelerate
(
change
)
# if the resulting speed would cause a collision, jam on the brakes
if
self
.
speed
>
dist
:
self
.
speed
=
0
# move
self
.
position
+=
self
.
speed
self
.
project
()
self
.
redraw
()
def
find_distance
(
self
):
"""Finds the distance between this Turtle and the next."""
dist
=
self
.
next
.
position
-
self
.
position
# deal with wrap-around
if
dist
<
0
:
dist
+=
self
.
world
.
get_length
()
return
dist
def
choose_acceleration
(
self
,
dist
):
"""Adjusts the speed of the Driver."""
if
dist
<
self
.
safe_distance
:
return
-
1
else
:
return
0.3
def
make_highway
(
n
,
driver
=
Driver
):
"""Make the highway and drivers, then run the simulation.
Args:
n: number of Drivers
driver: constructor
"""
# create the highway
world
=
Highway
()
world
.
canvas
.
clear_transforms
()
world
.
setup_run
()
# create (n) drivers
world
.
make_drivers
(
n
,
driver
)
# start the simulation, then wait for user events
print
len
(
world
.
animals
)
world
.
run
()
world
.
mainloop
()
def
main
(
script
,
n
=
20
):
n
=
int
(
n
)
make_highway
(
n
)
if
__name__
==
'__main__'
:
import
sys
main
(
*
sys
.
argv
)
You can’t perform that action at this time.