Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python Project: Ant Escaping Path

Python Project: Ant Escaping Path

Python simulation project
Python, Graphics and simulation

In this project we assume that we have a square pad and we put an ant in the center of this pad, we let the ant to walk, once it reach any eadgs of the square we move it with our hand to the middle again, say we doing this for x times and each time we colored the ant foots with gray level (light to dark) color. Our project is to write a code to simulate this task (ant movement). 

Enhancement: Here are some enhancement ideas:
1. We can use the Pi or Golden Ratio for the variables.
2. Also we can set a memory facto so with each time we move the Ant to the center the memory will increase so it may use the same path.
3. For the memory, we can set a position (x,y) as a food, and each time it reach the food it’s memory will increase.

The Code

# Create in 27/8/2019 .

import turtle
import random

screen = turtle.Screen()


# colors varibles
# r=240, g=240, b=240 is a light gray color


cf = 3 # color increasing factor
ff = 0 # ant forward moving factor

def rand_walk(x,the_t):


# The color will be darker each time



def ant_walk ():

x = 1

while x < 7 : # Moving the Ant to the center 7 times


if random.randrange(1,100) %2:

t_ang = random.randrange(10,45)



t_ang = random.randrange(10,45)

t1.left (t_ang)

# if the ant reach the square boards then we move it to the center.

if (t1.xcor() > 650 ) or (t1.xcor() 650 ) or (t1.ycor() < -650 ):



x = x + 1

# Calling the Function

Here is an GIF file

To Download my Python code (.py) files Click-Here

Follow me on Twitter..

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: