Restricted Polygon Chaos Game 2 with Python Turtle

In a previous project, you drew a Sierpinski Triangle with Chaos Game. You also did restricted chaos projects for square and pentagon. Now, generalize the project that allows chaos game for polygon of any number of vertices.

In this project, the restriction is: next vertex has to be immediate neighbors of the previous vertex or the same vertex as the previous vertex. This restriction should draw following figures from pentagon to decagon.

Pentagon Fractal with Chaos Game
Hexagon Fractal with Chaos Game
Heptagon Fractal with Chaos Game
Nonagon Fractal with Chaos Game
Decagon Fractal with Chaos Game

Restricted Square Chaos Game 2

In a previous project, you drew a Sierpinski Triangle with Chaos Game. Instead of a triangle, use four points of a square as the target points and play the chaos game. There is restriction: you can’t choose a vertex that is 2 places away. That is: you can only move to the same vertex as the last move or the immediate neighbor of the last move. It should draw something like this after 100,000 iterations:

Restricted Square Chaos Game

Restricted Square Chaos Game 1

In a previous project, you drew a Sierpinski Triangle with Chaos Game. Instead of a triangle, use four points of a square as the target points and play the chaos game. There is restriction: you can’t choose the same direction as the last move. It should draw something like this after 100,000 iterations:

Restricted Square Chaos Game

Chaos Game – Sierpinski Triangle with Python Turtle (with Solution)

Draw three points to form a triangle. Draw the fourth point in a random position. In each of the following step, this point will move halfway towards one of the three randomly chosen points of the original triangle. Repeat the above step 1000, 10,000, and 100,000 times. You will see a Sierpinski Triangle! This process is an example of Chaos Game.

Triangle Chaos Game after 1,000 Steps
Triangle Chaos Game after 10,000 Steps
Triangle Chaos Game after 100,000 Steps

Solution:

import turtle
import random

screen = turtle.Screen()
screen.title('Triangle Chaos Game with Python Turtle')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.up()

A = (0,350)
B = (-300,-200)
C = (300,-200)
V = (A,B,C) # list of three vertices
for v in V:
    turtle.goto(v)
    turtle.dot('red')

n = 10000 # number of points to draw
p = (random.uniform(-200,200),random.uniform(-200,200)) # random starting point
t = turtle.Turtle()
t.up()
t.hideturtle()
for i in range(n):
    t.goto(p)
    t.dot(2,'blue')
    r = random.randrange(len(V)) # pick a random vertex
    p = ((V[r][0]+p[0])/2,(V[r][1]+p[1])/2) # go to mid point between the random vertex and point   
    if i % 1000 == 0: # update for every 1000 moves, this part is for performance reason only
        t = turtle.Turtle() # use new turutle
        t.up()
        t.hideturtle()
        screen.update()

Asteroids Game with Python Turtle

Develop the Asteroids Game with Python Turtle. Because this project is fairly large, you may want to use Object Oriented Programming by defining several classes and put them in separate files. You may also need to know how to detect collisions. These projects will help you develop this game:

You can also add music and sound effects to this game with PyGame’s sound library.

Closest Point On a Line Segment to Another Point

Write a program that finds the closest point of a line segment to another point. Randomly pick a point and color it in blue. Also, randomly generate several lines. Find the closest points on these lines to the blue point and mark these closest points in red as show in the picture below:

Closest Point On a Line Segment to the Blue Point

What’s next?
Intersecting Lines with a Circle

Detecting a Point in a Polygon with Python Turtle

Generate a random polygon with many sides and randomly drop point on the screen. If the point falls inside the polygon color it in red, otherwise color it in blue.

You can use Ray Casting Algorithm to determine if a point is inside a polygon. You may also want to know how to detect if two lines intersect with these two projects: Random Intersecting Lines and Two Randomly Moving Intersecting Lines.

Firing, Accelerating, and Rotating Spaceship

Continuing from previous Accelerating and Rotating Spaceship project, make the spaceship fire the bullets. Each bullet have limited range and there should be some time gap between the firings so that spaceship won’t destroy asteroids too easily in the future game. You may need to use list structure in Python to store the bullets.