Throbbing Heart Animation with Python and Turtle (Source Code)

https://pythonturtle.academy/wp-content/uploads/2021/11/heart.mp4

Animate a throbbing heart. Check out this simple heart drawing tutorial if you need help.

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Heart Animation - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

screen.tracer(0,0)
turtle.color('red')

def draw_heart(alpha,d):
  r = d/math.tan(math.radians(180-alpha/2))
  turtle.up()
  turtle.goto(0,-d*math.cos(math.radians(45)))
  turtle.seth(90-(alpha-180))
  turtle.down()
  turtle.begin_fill()
  turtle.fd(d)
  turtle.circle(r,alpha)
  turtle.left(180)
  turtle.circle(r,alpha)
  turtle.fd(d)
  turtle.end_fill()

a = 220
sign = -1
def animate():
  global a,sign
  turtle.clear()
  draw_heart(a,1000)
  screen.update()
  a += sign
  if a < 215:
    sign = -sign
  elif a > 220:
    sign = -sign
  screen.ontimer(animate,50)

animate()

Stock Price Random Walk with Positive Mean

In this project, you simulated stock price random walk with mean equal to 0%. What if we increase the mean of Gaussian distribution very slightly to 0.5% and also increase the standard deviation to 5%. You may see something similar to a trending stock prices of successful companies.

Random Walk of Trending Stock Price
Random Walk of Trending Stock Price
Random Walk of Trending Stock Price

Stock Price Random Walk (Source Code)

In a previous project, we simulated a random walk on a 2-D plane. For this project, simulate stock price change with random walk. Start with an initial stock price. At next day, the price change by random percentage with Gaussian (Normal) distribution (random.gauss() function) with mean 0% and standard deviation of 2%. Try to simulate multiple times and see if they look similar to some of stock price charts you have seen before.

Stock Price Random Walk with mean 0% and std 2%
Stock Price Random Walk with mean 0% and std 2%
Stock Price Random Walk with mean 0% and std 2%

Source Code:

import turtle
import random

screen = turtle.Screen()
screen.title('Stock Price Random Walk - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(0,0,1000,1000)
turtle.speed(0)
turtle.hideturtle()

price=500
turtle.up()
turtle.goto(0,price)
turtle.down()
for i in range(1,1000):
    price *= (1+random.gauss(0,0.02))
    turtle.goto(i,price)

5 Degree Square Spiral in a Square (Source Code)

As you can see in the following picture, the squares are tilted by 5 degrees inside a bigger square generating spiral effect.

5 Degree Square Spiral

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('5 Degree Square Spiral in a Square - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def draw_square(x,y,direction,length):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(direction)
    turtle.back(length/2)
    turtle.left(90)
    turtle.back(length/2)
    turtle.seth(direction)
    turtle.down()
    for _ in range(4):
        turtle.fd(length)
        turtle.left(90)

def square_spiral(x,y,direction,length):
    if length < 5: return
    
    draw_square(x,y,direction,length)
    square_spiral(x,y,direction+alpha,length/(math.sin(math.radians(alpha)) + math.cos(math.radians(alpha))))


alpha=5
square_spiral(0,0,0,1600)

Circle of Squares (Source Code)

Draw the following circle made up with squares. You may need use trigonometry to solve this.

Circle of Squares

Source Code:

import turtle
import math
screen = turtle.Screen()
screen.title('Squares Circle - PythonTurtle.Academy')
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def draw_square(x,y,length,direction,color):
    turtle.up()
    turtle.goto(x-length/(2**0.5)*math.cos(math.radians(direction+45)),y-length/(2**0.5)*math.sin(math.radians(direction+45)))
    turtle.seth(direction)
    turtle.color(color)
    turtle.down()
    for _ in range(4):
        turtle.fd(length)
        turtle.left(90)

def draw_square_circle(radius,direction,n):
    length = 2*radius*math.sin(math.radians(180/n))
    length = length/(2**0.5)
    for _ in range(n):
        draw_square(radius*math.cos(math.radians(180/n))*math.cos(math.radians(direction)),\
                    radius*math.cos(math.radians(180/n))*math.sin(math.radians(direction)),\
                    length, direction+45,'blue')
        direction += 360/n
        
draw_square_circle(600,0,20)

Bulge Illusion with Python Turtle (Source Code Included)

This is another Akiyoshi illusion. It feels like the center is bulging.

Bulge Illusion

Source Code:

import turtle

screen = turtle.Screen()
screen.setup(800,800)
screen.setworldcoordinates(-8,-8,8,8)
screen.tracer(0,0)
screen.title('Bulge Illusion - PythonTurtle.Academy')
turtle.hideturtle()
turtle.speed(0)

color1 = 'light sky blue'
color2 = 'firebrick'
screen.bgcolor(color2)

def draw_square(x,y,size,c):
    turtle.up()
    turtle.goto(x-size/2,y-size/2)
    turtle.seth(0)
    turtle.color(c)
    turtle.begin_fill()
    for _ in range(4):
        turtle.fd(size)
        turtle.left(90)
    turtle.end_fill()
    
def draw_board():
    for x in range(-7,8,2):
        for y in range(-7,8,2):
            draw_square(x,y,1,color1)
    for x in range(-6,8,2):
        for y in range(-6,8,2):
            draw_square(x,y,1,color1)
            

def draw_diag(x,y):
    c = color2 if (x+y)%2 == 0 else color1
    if x*y > 0:
        draw_square(x-0.3,y+0.3,0.3,c)
        draw_square(x+0.3,y-0.3,0.3,c)
    elif x*y < 0:
        draw_square(x+0.3,y+0.3,0.3,c)
        draw_square(x-0.3,y-0.3,0.3,c)

def draw_straight(x,y):
    c = color2 if (x+y)%2 == 0 else color1
    if y>0:
        draw_square(x-0.3,y-0.3,0.3,c)
        draw_square(x+0.3,y-0.3,0.3,c)
    elif y<0:
        draw_square(x-0.3,y+0.3,0.3,c)
        draw_square(x+0.3,y+0.3,0.3,c)
    elif x>0:
        draw_square(x-0.3,y-0.3,0.3,c)
        draw_square(x-0.3,y+0.3,0.3,c)
    elif x<0:
        draw_square(x+0.3,y-0.3,0.3,c)
        draw_square(x+0.3,y+0.3,0.3,c)
        
def draw_bulge():
    for x in range(-6,7):
        for y in range(-6,7):
            if abs(x)+abs(y)<=7:
                draw_diag(x,y)
                if x==0 or y==0: draw_straight(x,y)
    x,y = -5,-3
    for i in range(3):
        draw_diag(x,y)
        draw_diag(-x,-y)
        draw_diag(x,-y)
        draw_diag(-x,y)
        x += 1
        y -= 1
draw_board()
draw_bulge()
screen.update()