Question
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()
Directions
Modify the program in the following ways:
- Write appropriate comments for this program and function. Include:
- A header comment for the program
- A header for the function describing the parameters and return value.
- Comment the blocks of code. The text has descriptions of all this, so reread that if necessary.
- Create a second turtle and start it at a different location than the current one. The location should be 50 units away, either horizontally or vertically. Have both turtles move randomly, but independently, so you will have to flip a coin for each turtle.
- Modify the while loop so that the program stops when either turtle leaves the screen. This is pretty easy, once you see it.
- After the loop, print a message indicating which turtle won. You decide whether it was the one still in the screen or the one that left. Use the turtle that left the screen to write the message (see the write() function in the turtle module).
- Write a Boolean function that has two turtles as parameters, and returns True if the turtles are at exactly the same location, and False otherwise.
- Call your Boolean function from Step 5 to also stop the program when the two turtles end up at the same location. In this case, print there was no winner. This is very much like step 3.
- Add another feature to your program. Some ideas are a) a fancier ending message, b) a third turtle, c) more randomness, like letting a random turtle go first each time or having a turtle randomly skip a turn, or ???
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started