Archive

Posts Tagged ‘graphics’

Python Project: Ant Escaping Path

August 29, 2019 Leave a comment


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()
screen.setworldcoordinates(-700,-700,700,700)
screen.tracer(5)

t1=turtle.Turtle()
t1.speed(0)
t1.penup()

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

r=240
g=240
b=240
t1.pencolor(r,g,b)

cf = 3 # color increasing factor
ff = 0 # ant forward moving factor
#screen.bgcolor(“black”)

def rand_walk(x,the_t):

the_t.pendown()

# The color will be darker each time

the_t.pencolor(r-(x*cf),g-(x*cf),b-(x*cf))

the_t.forward(20+(ff*x))

def ant_walk ():

x = 1

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

rand_walk(x,t1)

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

t_ang = random.randrange(10,45)

t1.right(t_ang)

else:

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 ):

t1.penup()

t1.goto(0,0)

x = x + 1

# Calling the Function
ant_walk()


Here is an GIF file



Follow me on Twitter..





Advertisements