Type to search…

Bio

The turtle module provides a visual and intuitive way to simulate biological structures like atoms, molecules and proteins.

Molecular Geometry

The Water Molecule (H2O)

Draw a classic Mickey Mouse-shaped water molecule.

  • Create a turtle to represent the drawing pen.
  • Draw a large red circle to represent the Oxygen atom.
  • Lift the pen, move to the center of the Oxygen atom, and set the turtle’s heading.
  • Draw a thick gray line (the chemical bond) outward, then draw a smaller white (or light gray) circle for the first Hydrogen atom.
  • Return to the center of the Oxygen atom. Turn the turtle exactly 104.5 degrees (the bond angle of water).
  • Draw the second bond and the second Hydrogen atom.

Possible solution, draw Water Molecule (H2O):

python
# Simple Mickey Mouse Water Molecule (H2O)
import turtle

t = turtle.Turtle()
t.speed(3)

# -------------------------
# Function to draw a filled circle (atom)
# -------------------------
def draw_atom(radius, fill_color):
    t.color("black", fill_color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

# -------------------------
# Function to draw a bond + hydrogen
# -------------------------
def draw_hydrogen(angle):
    t.setheading(angle)
    t.pendown()
    t.pensize(5)
    t.color("gray")
    t.forward(100)

    draw_atom(25, "white")

    t.penup()
    t.goto(0, 0)

# -------------------------
# Draw Oxygen
# -------------------------
t.penup()
t.goto(0, -60)
t.pendown()
draw_atom(60, "red")

t.penup()
t.goto(0, 0)

# -------------------------
# Draw Hydrogens
# -------------------------
draw_hydrogen(0)
draw_hydrogen(104.5)

turtle.done()

Biological Dynamics

Brownian Motion (Random Walk)

Simulate the erratic, random movement of a microscopic particle suspended in a fluid.

  • Import the random module alongside turtle.
  • Create a small, colored dot (the turtle) representing a protein or molecule.
  • Write a while loop that repeats 500 times.
  • Inside the loop, have the turtle turn a random angle (between 0 and 360 degrees) and move forward a random small distance (e.g., between 1 and 10 pixels).
  • Leave the turtle’s pen down so it draws a trail of its path.
  • Add an “invisible boundary” (like a cell membrane). If the turtle’s x or y coordinates go beyond a certain limit, force it to bounce back into the center.

Cellular Viral Infection Dynamics

Simulate a simple viral infection spreading through a cluster of cells.

  • Create a list of 20 turtles. Scatter them randomly across the screen and stamp them as green circles. These are healthy cells.
  • Create one red turtle (the virus/infected cell) and place it in the center.
  • Create an animation loop where all turtles move randomly (using a mini random walk).
  • The Dynamic Rule: Check the distance between the red turtle and the green turtles. If a green turtle gets within a specific distance (collision radius) of a red turtle, change the green turtle’s color to red.
  • Watch as the “infection” dynamically spreads through the moving population!

You will need to use turtle.distance() to calculate the proximity between the agents on the screen.

Possible solution

python
import turtle
import random

wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Simulació de propagació viral")
wn.setup(width=600, height=600)

# --- Create green cells ---
cells = []
for _ in range(20):
    cell = turtle.Turtle()
    cell.shape("circle")
    cell.color("green")
    cell.penup()
    cell.goto(random.randint(-250, 250), random.randint(-250, 250))
    cells.append(cell)

# --- Create red cell (the virus) on center ---
virus = turtle.Turtle()
virus.shape("circle")
virus.color("red")
virus.penup()
virus.goto(0, 0)

# --- Custom params ---
collision_radius = 20
move_distance = 10

# --- Main program ---
while True:
    for cell in cells:
        # Move green cells randomly
        dx = random.randint(-move_distance, move_distance)
        dy = random.randint(-move_distance, move_distance)
        cell.goto(cell.xcor() + dx, cell.ycor() + dy)
        
        # Check infection
        if cell.color()[0] == "green" and cell.distance(virus) < collision_radius:
            cell.color("red")

Protein Structures

Primary Structure (The Polypeptide Chain)

Draw a “beads on a string” model of a polypeptide chain using different colors to represent different types of amino acids (e.g., hydrophobic vs. hydrophilic).

  • Create a list of colors in Python (e.g., ["red", "blue", "green", "blue", "yellow", "red"]). Each color represents a specific amino acid.
  • Write a for loop that iterates through your list of colors.
  • For each color, change the turtle’s fill color, draw a filled circle (the amino acid bead), and then move the turtle forward.
  • Keep the pen down as you move forward to draw a black line connecting the beads—this represents the peptide bond.

First solution:

python
import turtle
screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Primary Structure - Beads on a String")
t = turtle.Turtle()
t.speed(2)
t.pensize(2)

# --------------------------
# Sample AA (AminoAcids)
# hydrophobic = blue, hydrophilic = red, special = green
# --------------------------
amino_acids_colors = ["red", "blue", "green", "yellow", "blue", "red", "green"]

# --------------------------
# Draw polypeptide chain.
# --------------------------
t.penup()
t.goto(-250, 0)  # començar a l'esquerra
t.pendown()

for color in amino_acids_colors:
    t.fillcolor(color)
    t.begin_fill()
    t.circle(20)  # bead size
    t.end_fill()
    t.forward(50)  # go to next bead

turtle.done()

Improved version, based on Real AA sequence. B chain of Human Hemoglobin. 17 AA.

python
import turtle

# --------------------------
# Classe per a un aminoàcid
# --------------------------
class AminoAcid:
    def __init__(self, name, color):
        self.name = name
        self.color = color
        self.position = (0, 0)

# --------------------------
# Real AA sequence. B chain: Human Hemoglobin. 17 beads.
# --------------------------
# Format: (name, color)
amino_acids = [
    AminoAcid("Phe", "red"),
    AminoAcid("Val", "orange"),
    AminoAcid("Asn", "yellow"),
    AminoAcid("Gln", "green"),
    AminoAcid("His", "cyan"),
    AminoAcid("Leu", "blue"),
    AminoAcid("Cys", "purple"),
    AminoAcid("Gly", "magenta"),
    AminoAcid("Ser", "lime"),
    AminoAcid("His", "cyan"),  
    AminoAcid("Leu", "blue"),
    AminoAcid("Val", "orange"),
    AminoAcid("Glu", "salmon"),
    AminoAcid("Ala", "brown"),
    AminoAcid("Leu", "blue"),
    AminoAcid("Tyr", "plum"),
    AminoAcid("Leu", "blue")
]

screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Protein Visualization — Insulina Humana (cadena B)")
t = turtle.Turtle()
t.speed(2)
t.pensize(2)

# --------------------------
# Draw AA chain
# --------------------------
t.penup()
t.goto(-300, 0)
t.pendown()

for aa in amino_acids:
    # Draw bead
    t.fillcolor(aa.color)
    t.begin_fill()
    t.circle(15)
    t.end_fill()
    
    # Save text position
    aa.position = t.position()
    
    # Draw lines.
    # t.penup()
    t.forward(50)
    # t.pendown()

# --------------------------
# Write aminoacid name.
# --------------------------
for aa in amino_acids:
    t.penup()
    x, y = aa.position
    t.goto(x, y + 30)
    t.write(aa.name, align="center", font=("Arial", 12, "normal"))

t.hideturtle()
screen.mainloop()

You're reading a preview.

Sign in with Google to read the full page. A Google account includes 5 free pages in total; students and teachers read their course pages without limit.

Sign in