Do you know how to do turtle methods and random methods? I've got a homework assignment but don't have time to do it by Sunday night. I put the assignment there. Hope to work with you!
CPSC COMPUTER SCIENCE 1FALL 2020 Assignment 3 – Fugitive Turtles Submit before: Midnight, Sunday, November 1 READING ALL INSTRUCTIONS BELOW CAREFULLY WILL SAVE YOU A LOT OF TIME AND FRUSTRATION Learning outcome: After competing this assignment, you will be able to: · Use a while loop for repeating one or more program statements multiple times · Create graphics including animation using the Turtle graphics module in Python · Show the ability to approach problem solving using computers in a more methodical way by applying the computational thinking strategies of problem decomposition, pattern recognition and algorithmic thinking. A Python turtle graphics refresher The turtle has three main attributes: · a position (initially at x=0, y=0, the origin of a 2D coordinate system at the center of a window 400 pixels wide and 300 pixels high) · an orientation or heading (initially due East or 0 degrees; see diagram below) · a pen having attributes such as color, width, and up versus down (initially down). The turtle moves with commands that are relative to its position, such as "move forward 10 spaces" and "turn left 90 degrees". The pen carried by the turtle can also be controlled, by lowering it on to the canvas (the “pendown” function) to draw a line or taking it off the canvas (the “penup” function) before a change of position to avoid drawing a line, setting pen color, or setting pen width. From these building blocks one can build more complex shapes like squares, triangles, circles and other composite figures. Example 1: Python program for drawing a square using Turtle graphics functions import turtle jo = turtle.Turtle()# Create turtle jo jo.color("red")# Set Jo’s color jo.forward(100) jo.right(90) jo.forward(100) jo.right(90) jo.forward(100) jo.right(90) jo.forward(100) jo.right(90) Example 2: Program draws a diamond, fills it with color and writes a label below it: import turtle al = turtle.Turtle()#Create turtle al al.begin_fill() #To fill all shapes with color #Set pen size, pen color and fill color al.pen(pensize = 5, pencolor="black",fillcolor="yellow") # Draw the diamond al.right(20) al.forward(100) al.left(40) al.forward(100) al.left(140) al.forward(100) al.left(40) al.forward(100) al.end_fill() # Stop filling shapes with color #Position al and write label al.setheading(270) al.penup() al.forward(65) al.left(90) al.forward(95) al.pendown() al.write("Diamond", align = "center", font=("Arial", 12, "normal")) al.hideturtle() You should run the programs above in IDLE and make sure you understand how they work. As you’ll notice, the programs consist of a series of function calls. A complete documentation of turtle functions can be found at: http://docs.python.org/3.1/library/turtle.html Problem statement: Design the solution to the following problem and implement it in a Python program. Simulate two turtles that escape from a holding pen accidentally left open and attempts to reach the fence surrounding the enclosure they are kept in. The cage is at the center of the enclosure. The turtles don’t see very well, and their walk to freedom follows quite random paths. Each turtle moves a certain random distance, stops, changes direction randomly and then repeats this move-stop-change direction cycle until it reaches the fence. Your program will name the turtle that manages to escape through the fence first and give the total distance covered by it. Assignment Notes The random module in Python implements a random number generator. The code shown below generates a random number in the range a to b inclusive and stores it in variable randomNumber: import random randomNumber = random.randint(a, b) Make the following assumptions: · Every time a turtle walks, it manages to cover a random distance in the range, 10 to 20 (Hint: Use random.randint(10,20)) · The turtle turns randomly to the right or left only by one of these following amounts: 0, 15, 30 and 45 degrees (also selected randomly). · The turtles walk in a straight line to get out of the cage Given below is a high-level algorithm for the solution: Create a graphics window of a suitable size and background color Create the two turtles, t1 and t2 Use a hidden turtle to draw the holding pen and the surrounding fence Store x and y coordinates of N, S, E and W walls Initialize total distance covered by t1 and t2 to 0 While both t1 and t2 are inside the walls Compute random direction and random distance for t1 Move t1 forward in the random direction through the random distance Update total distance covered by t1 Compute random direction and random distance for t2 Move t2 forward in the random direction through random distance Update total distance covered by t2 Determine who got through fence first Print name and total distance travelled by this turtle Copy this algorithm into your newly created source code file and refine it further. Use steps of the algorithm as comments where appropriate. Think of these steps as a subtask (problem decomposition!) and try to solve it one task at a time. To find out if a turtle has reached and possibly crossed the fence, make use of the fact that the turtle is inside as long as its x coordinate value is less than that of the East fence and greater than that of West fence, and at the same time its y coordinate value is less than that of the North fence and larger than that of South fence (think of a Boolean expression as the condition to test). Sample test execution For details of all turtle module functions, visit: https://docs.python.org/3.1/library/turtle.html What to submit Your Python program in a .py file. Name your file:
Evaluation Rubric: · Your Python program · Code complete and free from any syntax errors (30 points) · Graphics window showing the holding pen, fence and turtles (20 points) · Simulates random movement of both turtles using a loop(20 point) · Displays name and distance travelled by winning turtle correctly(25 points) · Documentation and presentation (header and internal comments, meaningful variable names) (5 points) You must test your program multiple times before submitting. Any program with syntax error will get no more than 30 points depending on the amount and quality of coding done.