Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW.07b Turtle Friend In Need Your friend, turtle, wants to run off and marry his

ID: 3722336 • Letter: H

Question

HW.07b Turtle Friend In Need

Your friend, turtle, wants to run off and marry his princess girlfriend but an all-powerful otaku opposes his plan. He hires two brothers, a pair of plumbers, to thwart turtle's plan and kidnap the princess.

Your turtle friend asks you to create a program, turtle_friend.py, to devise a means to evade the plumbers and escape with his princess to live happily ever after.

Task

Create a function that uses Turtle Graphics to simulate your turtle friend’s escape. The function should define a parameter, num_enemies, to represent the number of enemies your friend could face. Enemies will be displayed as red or green bouncing balls.

Create num_enemies red or green bouncing balls, each located at a random location but no closer than a ball's width from any window borders. For each ball, select a random direction (dx and dy) for each ball to move in the combined range of (-5, 5), excluding 0.

Note: Do NOT have all balls use the same dx and dy values; rather, each ball should have its own dx and dy. You will need to use parallel lists (where a ball's dx and dy are located at the same index in both lists) or a list of sublists to manage the ball's dx and dy movements.

Create a turtle (your turtle friend) in the lower right-hand corner of the window. The turtle should be the same size as the balls. Use the arrow keys to control your turtle friend’s movement. When running your function, the user will use the arrow keys to try to move your turtle friend from the starting corner of the window to the OPPOSITE corner without letting the turtle be hit by a ball.

You will need to detect collisions between each ball and the window boundaries in order to know whether to reverse the ball's heading. You do NOT need to detect collisions with the boundaries of the window for your turtle friend. Also, the balls do NOT need to collide with each other; that is, they may pass through each other and/or overlap.

If your friend collides with a bouncing ball, he is captured (ie. game over). If your friend eludes the bouncing balls, then he escapes (ie. your friend wins). For each ending condition, print a message of your choosing (eg "Your friend made it!" or "Your friend is now turtle soup!") to indicate the result.

Notes:

Set the window to 500 pixels x 500 pixels.

Both your turtle friend and the enemies are defined as turtle objects; use a turtle shape for your friend and circle shape for the enemies.

Set the size of your friend and the enemies to turtlesize 3; this value can vary slightly but your friend and the enemies should be the SAME size.

To test for a collision between your turtle friend and an enemy, you'll want to determine when the distance between them is small enough to know they are right beside each other. You can use a turtle object's .distance() method to determine distance between to turtle objects. The following statement returns the distance between turtle1 and turtle2:

               turtle1.distance(turtle2)

Additional Assignment Criteria

Place all functions in the same Python file, turtle_friend.py.

Include a header with the filename, brief descriptions of each function, your name and a date.

Add line comments before each function and at other appropriate sections of your code.

Keep your functions are reasonable size by breaking their contents into additional functions that are called from your function (keeps your code modular).

Explanation / Answer

## python program for turtle.py

import turtle

from random import randint

size = 20

circles = 20

turtle.speed(100)

turtle.colormode(255)

def move(length, angle):

turtle.right(angle)

turtle.forward(length)

def hex():

turtle.pendown()

turtle.color( randint(0,255),randint(0,255),randint(0,255) )

turtle.begin_fill()

for i in range(6):

move(size,-60)

turtle.end_fill()

turtle.penup()

# start

turtle.penup()

for circle in range (circles):

if circle == 0:

hex()

move(size,-60)

move(size,-60)

move(size,-60)

move(0,180)

for i in range (6):

move(0,60)

for j in range (circle+1):

hex()

move(size,-60)

move(size,60)

move(-size,0)

move(-size,60)

move(size,-120)

move(0,60)

turtle.exitonclick()

def init_keys(screen, sprite, step):

def add_move(dx, dy, ks):

def f():

x, y = sprite.pos()

sprite.goto(x+dx, y+dy)

screen.onkey(f, ks)

#Directions

add_move(-step, 0, "Left")

add_move(step, 0, "Right")

add_move(0, step, "Up")

add_move(0, -step, "Down")

def create_ball():

ball = turtle.Turtle()

ball.penup()

ball.shape("circle")

ball.shapesize(3)

ball.color("blue")

return ball

def init_screen():

screen = turtle.Screen()

screen.setup(500, 500)

screen.title("shooting ball")

init_keys(screen, create_ball(), 10)

return screen

def main():

screen = init_screen()

screen.listen()

screen.mainloop()

main()