Chaos Game via Python Turtle

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 1

  • @zhengli2589
    @zhengli2589  2 ปีที่แล้ว

    If you have done very well with the game of Guess the Number, then let’s try a new and magic game. Assume you have an invisible triangle, and you can draw its three nodes first. I show you the base code as follows:
    ########################################
    import random
    import turtle
    turtle.TurtleScreen._RUNNING = True
    myScreen = turtle.Screen()
    myScreen.setup(width = 1.0, height = 1.0)
    # Three fixed nodes of the triangle. You can of course change the locations of the nodes.
    R=300
    x1=0
    y1=R
    x2=-R
    y2=-R
    x3=R
    y3=-R
    myPen=turtle.Turtle()
    # Draw the three fixed nodes
    myPen.penup()
    myPen.goto(x1,y1)
    myPen.dot()
    myPen.goto(x2,y2)
    myPen.dot()
    myPen.goto(x3,y3)
    myPen.dot()
    # Starting point can be any location inside the triangle
    myScreen.exitonclick()
    ########################################
    Then, you can program to draw more dots by following the steps below:
    (1) Start from a random location inside the triangle. Don’t draw anything yet.
    (2) Randomly select one of the three nodes of the triangle.
    (3) Find the middle point between your starting point and the randomly selected triangle node.
    (4) Draw a dot at this middle point, and treat this middle point as your new starting point.
    (5) Repeat from Step (2) to Step (4).
    Please take this opportunity to program with natural language thinking. When you translate your natural language solution into Python program, please pay more attention to practicing the decision making and looping logics.