Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() /

import random

import turtle

def isInScreen(win,turt):

leftBound = -win.window_width() / 2

rightBound = win.window_width() / 2

topBound = win.window_height() / 2

bottomBound = -win.window_height() / 2

turtleX = turt.xcor()

turtleY = turt.ycor()

stillIn = True

if turtleX > rightBound or turtleX < leftBound:

stillIn = False

if turtleY > topBound or turtleY < bottomBound:

stillIn = False

return stillIn

def main():

wn = turtle.Screen()

# Define your turtles here

june = turtle.Turtle()

june.shape('turtle')

while isInScreen(wn,june):

coin = random.randrange(0, 2)

if coin == 0:

june.left(90)

else:

june.right(90)

june.forward(50)

wn.exitonclick()

main()

Modify the program in the following ways:

  1. Write appropriate comments for this program and function. Include:
    1. A header comment for the program
    2. A header for the function describing the parameters and return value.
    3. Comment the blocks of code. The text has descriptions of all this, so reread that if necessary.
  2. Create a new turtle, a black hole turtle, that moves to some random location on the screen and draws a black dot of diameter 100. Use the .dot function instead of .circle, since you want the turtle at the center of the figure. [As of Fall 1, 2018, there is a bug in the active code window. If you are using the textbook to write this program, use may have to use dot(50) to get the correct size black hole. If you are using a stand-alone python compiler, use dot(100).] This should be done in the main function before creating the randomly walking turtle.
  3. Write a boolean function that has two turtles as parameters, and returns true if the turtles are within 50 units of each other and false otherwise. There are several ways to determine the distance between two turtles. You could use math.sqrt or math.hypot to compute the distance. If you read the turtle documentation, you may find another way as well. Talk to the instructor if you are having trouble with this. Be sure to comment this function.
  4. Modify the while loop so that the program stops when the turtle leaves the screen or when it touches the black hole. Use your Boolean function from Step 3.
  5. Modify the function isInScreen so that the function only returns false if the turtle wanders off the top or bottom of the screen. The next step is going to deal with the turtle moving off the screen horizontally.
  6. Modify the body of the while loop so that if the turtle wanders off the screen either to the right or the left, it reappears on the opposite side of the screen.
  7. Add another feature to your program. Either more randomness, or one more randomly walking turtle, or multiple black holes, or ???

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions