Lab 3, Part 3: Bees (more turtle functions)
You'll get more practice with writing python functions that draw turtle graphics
in this part of lab.
Open lab03/bees.py
to get started...
Task 1: Write a generic bee function
In the file bees.py
, you are given two functions called bigBee()
and littleBee()
. These functions produce, as you might expect, a big bee (yellow with navy stripe) and a small bee (green with red stripe).
Note that both of these functions are zero-parameter functions, so colors and sizes are hard-coded. The diagram below shows the dimensions of each bee (they share the same proportions).
Your job is to capture the pattern in these two bee functions, and write one
generic bee
function that, given the colors and size, will draw a bee with the same
proportions as those above.
This google doc worksheet displays the bigBee
and littleBee
functions side-by-side, which you may find useful.
Test your bee
function
You can test your bee
function by using it to reproduce the original bigBee
and littleBee
.
You can use the invocations below in your test
function.
# this invocation is intended to replicate the original bigBee
bee(______, ________, _______)
teleport(200, 200) # so the bees don't overlap
# this invocation is intended to replicate the original littleBee
bee(______, ________, _______)
When you run your code, you should see this:
Note: you might notice that the vertical stripe of the bee runs beyond the edges of the
bee's body. This is because when the pensize
is set to be very large, the pen "leaks" over
the edge of the body when it is positioned at the body's edge. For now, don't worry about it.
A swarm of bees!
Our goal here is to generate a swarm of randomly-sized bees at random locations and with
random colors (so much randomness!). Eventually, your swarm
function will draw something like this:
We'll need to work our way up to a swarm. Let's start small, with a single random bee first.
Task 2: Random bee
You will write a function that will use your bee
function to generate
a bee of random size at a random location. Your oneRandomBee
function is a zero-parameter
function that does not return a value.
Recall that random.randint(a, b)
returns a random integer between a
and b
, inclusive.
You can use this to generate a random size for your bee. A reasonable range is between 5 and 100, but you and your partner can choose a smaller or larger range if desired.
Similarly, you can also use random.randint
to generate a random x
and y
location for the bee.
The x
and y
coordinates can range between -400 and +400.
With your partner, sketch out or talk about your plan.
You can test your oneRandomBee
function by calling it from
your test
function:
oneRandomBee()
Each invocation will produce a random bee, here are 3 examples (yours will be different because of the randomness). It's important to test a bunch of times when using random numbers.
Task 3: Swarm
Once you are convinced that your oneRandomBee
function works properly,
then write the swarm
function which will draw a swarm of 10 bees.
Here are some swarm examples. Two of the examples use random colors, which
you have not yet incorporated, but can do so (you can use the provided
randomTurtleColor
function). It just takes one line of code to make that change.
Note: You and your partner might be wondering if there is a more elegant way to repeat a line of code 10 times. Yes, there is a way (loops) and we will get to loops in a couple of weeks.
You can even add a flower in your drawing, so there is something for the bees to land on.
Task 4a: Make your bee
function fruitful
Now, let's revisit your original bee
function and make it
fruitful. Let's have the bee
function return the total area of the
bee (head area + body area). Add a few lines of code to your bee
function
to calculate and return the area of the bee.
Hints:
- area of a circle is:
pi
*r
2 wherer
is the radius - in Python you can
from math import pi
and then use
pi
as needed. - some test cases:
In []: bee(100, 'pink','yellow') Out[]: 36442.4747816416 In []: bee(25, 'black','orange') Out[]: 2277.6546738526
Task 4b: Fruitful oneRandomBee
Now that bee
is fruitful, make oneRandomBee
fruitful as well
oneRandomBee
should return the total area of the bee drawn.
Here are some sample tests (recall that oneRandomBee
is random, so your tests won't
match ours).
In []: oneRandomBee()
Out[]: 25105.2208770729
In []: oneRandomBee()
Out[]: 32889.33349043154
Task 4c: Fruitful swarm
Continuing along the same lines, now that oneRandomBee
is fruitful, make swarm
fruitful as well.
swarm
should return the total area of all the 10 bees drawn in the swarm.
Here are some sample tests (recall that there is randomness, so your tests won't match ours).
In []: swarm()
Out[]: 199267.45210601628
In []: swarm()
Out[]: 107767.68642427053
In []: swarm()
Out[]: 139144.65721126396