Vertically Centered Triangle And now, modify the above program to center the triangle vertically as well as horizon- tally, updating the dot location too. Your output should like as follows, Here is my answer: # Import the turtle module import turtle # Create a turtle object t = turtle.Turtle() # Set the speed and the pen size t.speed(0) t.pensize(3) # Define a function to draw a triangle with a dot in the center def draw_triangle(): # Draw a triangle for i in range(3): t.forward(100) t.left(120) # Move to the center t.penup() t.forward(50) t.left(60) t.forward(28.87) # This is the height of an equilateral triangle divided by 2 t.right(90) # Draw a dot t.dot(10) # Move back to the corner t.left(90) t.forward(28.87) t.right(60) t.forward(50) t.left(120) t.pendown() # Move to the center of the screen t.penup() t.goto(0, 0) t.pendown() # Draw the triangle draw_triangle() # Hide the turtle t.hideturtle() # Exit on click turtle.exitonclick() My question here: Your First Animation Now that you’ve had a chance to experiment with animations, its your turn to write one. Create an animation that is based upon your code the question “Vertically Centered Triangle”. This animation should feature the same triangle. However, the dot should bounce up and down (so update the y coordinate) inside of the triangle. When it reaches the edges of the triangle, its velocity should be reversed so that it always remains inside of the shape.
Vertically Centered Triangle
And now, modify the above
tally, updating the dot location too. Your output should like as follows,
Here is my answer:
# Import the turtle module
import turtle
# Create a turtle object
t = turtle.Turtle()
# Set the speed and the pen size
t.speed(0)
t.pensize(3)
# Define a function to draw a triangle with a dot in the center
def draw_triangle():
# Draw a triangle
for i in range(3):
t.forward(100)
t.left(120)
# Move to the center
t.penup()
t.forward(50)
t.left(60)
t.forward(28.87) # This is the height of an equilateral triangle divided by 2
t.right(90)
# Draw a dot
t.dot(10)
# Move back to the corner
t.left(90)
t.forward(28.87)
t.right(60)
t.forward(50)
t.left(120)
t.pendown()
# Move to the center of the screen
t.penup()
t.goto(0, 0)
t.pendown()
# Draw the triangle
draw_triangle()
# Hide the turtle
t.hideturtle()
# Exit on click
turtle.exitonclick()
My question here:
Your First Animation
Now that you’ve had a chance to experiment with animations, its your turn to write
one. Create an animation that is based upon your code the question “Vertically Centered
Triangle”. This animation should feature the same triangle. However, the dot should
bounce up and down (so update the y coordinate) inside of the triangle. When it reaches
the edges of the triangle, its velocity should be reversed so that it always remains inside
of the shape.

Step by step
Solved in 4 steps with 1 images









