Problem 2 Lists of Turtles

Total Page:16

File Type:pdf, Size:1020Kb

Problem 2 Lists of Turtles

Problem 2 Lists of Turtles

Problem Create a bale of turtles and enable them to move only within a display area. As the turtles move, use two disc-shaped turtles: one to track the average location of the bale and one to track one extreme of their distribution (uppermost, lowermost, leftmost, or rightmost).

Specifications 1. Create a display area with coordinates in the range of a rectangle bounded by opposite diagonal corners at (-300, -400) and (300, 400).

2. From user input, determine a number n of turtles to create.

3. Use a list comprehension to create a list of n turtles

4. With and for the bale of turtles:

a. Begin each turtle moving randomly within the display area.

b. The turtles may be colored and color other than red or green.

c. There must be a function to calculate a turtle’s next location.

d. All turtles must remain in the display area.

5. Create a red turtle and make its position match the average location of the other n turtles.

a. There must be a function to calculate and return the average location of the n turtles.

b. The red turtle must leave a pen trail

6. Create a green turtle and make its position track exactly one of the current leftmost, rightmost, uppermost or bottommost turtle in the display.

a. There must be a function to calculate and return the proper coordinate of the current, extreme-most turtle.

b. The green turtle must leave a pen trail.

c. Use the green turtle to draw a line the complete width or height of the display area at each extreme value of turtle location found for the bale. Use a horizontal line if tracking upper- or lower-most. Use a vertical line, if tracking left- or rightmost

7. Demonstrate your program. The following image shows the state after at least 100 steps using each of the 25 turtles and tracking the average location (using the red pen trail) and the position leftmost turtle in the bale (using the green pen trail and vertical bar).

Recommended publications