Random Walk with Python Turtle (with Source Code)

In this project you are going to simulate random walk. Create five or more turtles and put them into a Python List. In each iteration, each turtle in the list choose a random direction and move forward a constant number of steps.

Source Code:

import turtle
import random
import math

turtle.setup(1000,1000)
turtle.title("Random Walk - PythonTurtle.Academy")
a = turtle.Turtle()
b = turtle.Turtle()
c = turtle.Turtle()
d = turtle.Turtle()
e = turtle.Turtle()

a.color('red')
b.color('green')
c.color('blue')
d.color('orange')
e.color('black')

tlist = []
tlist.append(a)
tlist.append(b)
tlist.append(c)
tlist.append(d)
tlist.append(e)

turtle.speed(0)
turtle.tracer(0)
turtle.hideturtle()
sum = 0
count = 0
for j in range(100):  
    for i in range(10000):
        for t in tlist:
            t.seth(random.randrange(0,360,90))
            t.fd(10)
        turtle.update()
    for t in tlist:
        sum += math.sqrt(t.xcor()*t.xcor() + t.ycor()*t.ycor())/10*2*math.sqrt(t.xcor()*t.xcor() + t.ycor()*t.ycor())/10*2/100
        count += 1
    for t in tlist:
        t.clear()
        t.up()
        t.goto(0,0)
        t.down()
    print(sum/count)

20 Random Walkers with List:

import turtle
import random

screen = turtle.Screen()
screen.setup(1000,1000)
screen.tracer(0,0)
walkers = list()
n = 20
for i in range(n):
    walkers.append(turtle.Turtle())
for i in range(n):
    walkers[i].color((random.uniform(0,1), random.uniform(0,1), random.uniform(0,1)))
                  
def random_walk():
    for i in range(n):
        angle = random.randint(0,3)*90
        walkers[i].seth(angle)
        walkers[i].fd(10)
    screen.update()
    screen.ontimer(random_walk,1000//20)

random_walk()

Massive Chasing Game (with Source Code)

In this Python Turtle project, you are going create a fun and beautiful animation. First, you are going to create a list of 100 turtles and put them into random positions. Set the color of these turtles gradually from hue value 0 to hue value 1. In each iteration, each turtle will move toward a small step toward the next turtle in the list. The last turtle in the list will move toward the first turtle. Repeat the iteration forever. These turtles will converge into a beautiful rainbow colored shape!

Video of this animation:

Source Code:

import turtle
import random
import colorsys

screen = turtle.Screen()
screen.tracer(0,0)
screen.setup(1000,1000)
turtle.hideturtle()

n=100
chasers = []
for i in range(n):
    chasers.append(turtle.Turtle())

h = 0
for i in range(n):
    c = colorsys.hsv_to_rgb(h,1,0.8)
    h += 1/n
    chasers[i].color(c)
    chasers[i].up()
    chasers[i].goto(random.uniform(-500,500), random.uniform(-500,500))

def chase():
    for i in range(n-1):
        angle = chasers[i].towards(chasers[i+1])
        chasers[i].seth(angle)
    angle = chasers[n-1].towards(chasers[0])
    chasers[n-1].seth(angle)
    for i in range(n):
        chasers[i].fd(2)
    screen.update()
    screen.ontimer(chase,1000//20)
    
chase()

Capture The Flags Game

In this Python Turtle project, you are going to develop a simple game. In this game, you create 11 flags in random positions. The player will control an emoji by keyboard to collect as many flags as possible before the timer expires.

To be able to finish this project, you need to know the basics of animation, timer and keyboard events in Turtle. You will also need to know list in Python and how to display unicode characters.

Check out animation of this project here: