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/Wanderer.py at master · omdgit/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 }}
omdgit
ThinkPython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ThinkPython
/
code
/
Wanderer.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
91 lines (63 loc) · 2.48 KB
master
Breadcrumbs
ThinkPython
/
code
/
Wanderer.py
Copy path
Top
File metadata and controls
Code
Blame
91 lines (63 loc) · 2.48 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
"""This module is part of Swampy, a suite of programs available from
allendowney.com/swampy.
Copyright 2010 Allen B. Downey
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
"""
# import everything from the random module
from
random
import
*
# import everything from TurtleWorld
try
:
# see if Swampy is installed as a package
from
swampy
.
TurtleWorld
import
*
except
ImportError
:
# otherwise see if the modules are on the PYTHONPATH
from
TurtleWorld
import
*
# a Wanderer is a kind of turtle
class
Wanderer
(
Turtle
):
def
__init__
(
self
,
speed
=
1
,
clumsiness
=
60
):
"""Initializes a Wanderer.
This is the function that creates new turtles.
the first parameter is the new turtle itself,
which is provided automatically when we invoke
Wanderer()
"""
# first, make a Turtle
Turtle
.
__init__
(
self
)
# then add the other attributes that make it a Wanderer
self
.
delay
=
0
# speed is the distance the Wanderer moves per step
self
.
speed
=
speed
# clumsiness determines the ability of the Wanderer to
# track a straight line
self
.
clumsiness
=
clumsiness
# Wanderers start out facing in a random direction
self
.
rt
(
randint
(
0
,
360
))
def
distance
(
self
):
"""Computes the distance from the Turtle to the origin."""
# change the following line to compute distance correctly
return
self
.
get_x
()
+
self
.
get_y
()
def
step
(
self
):
"""Invoked whenever the turtle is supposed to move."""
# you are not allowed to modify this method
# here is how to invoke a method on a Wanderer
dist
=
self
.
distance
()
# call the function that keeps the Wanderers in bounds
self
.
keep_in_bounds
(
dist
)
# choose a random direction and turn
dir
=
randint
(
0
,
self
.
clumsiness
)
-
randint
(
0
,
self
.
clumsiness
)
self
.
rt
(
dir
)
# move forward according to the speed attribute
self
.
fd
(
self
.
speed
)
def
keep_in_bounds
(
self
,
dist
):
"""Turns the Turtle in order to keep it in bounds."""
# you should modify this method
pass
# create a new TurtleWorld
world
=
TurtleWorld
()
# add the Run, Stop, Step and Clear buttons
world
.
setup_run
()
# make three Wanderers with different speed and clumsiness attributes
for
i
in
range
(
1
,
4
):
Wanderer
(
i
,
i
*
45
)
# tell world to start processing events (button presses, etc)
wait_for_user
()
You can’t perform that action at this time.