Skip to content

Instantly share code, notes, and snippets.

@andersonfrailey
Created April 13, 2020 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfrailey/e440027694677fd055ab503d3eda05ea to your computer and use it in GitHub Desktop.
Save andersonfrailey/e440027694677fd055ab503d3eda05ea to your computer and use it in GitHub Desktop.
This script uses turtle to make that nifty little "S" thing we all used to draw in middle school a bunch of times
import turtle
import random
def draw_s(t, size=100, start=(0, 0), rel_gap=2):
"""
Draw the 90's S using turtle.
Parameters
----------
t: turtle object
size: size (in pixles) of the lines used to start the s
start: starting position
/ \
/ \
/ \
/ \
/ \
| | |
| | |
| |____|
\ \
\ \
\ \
\ \
\ \
|––––| |
| | |
| | |
\ /
\ /
\ /
\ /
\ /
"""
gap = size / rel_gap
# create an offset variable for moving the turtle
y_offset = turtle.Vec2D(0, size / 2)
# start the turtle facing upward
t.setheading(90)
# go to starting position
t.penup()
t.goto(start)
t.pendown()
# draw the left set lines
t.fd(size)
l_b_t = t.pos()
# gap between two sets of lines
t.penup()
t.fd(gap)
t.pendown()
l_t_b = t.pos() # make note of location
t.fd(size)
l_t_t = t.pos()
# draw middle set of lines and note where they are
t.penup()
t.right(90)
t.fd(size / 2)
t.pendown()
m_t_t = t.pos()
t.right(90)
t.fd(size)
m_t_b = t.pos()
t.penup()
t.fd(gap)
m_b_t = t.pos()
t.pendown()
t.fd(size)
# make note of lower middle position
m_b_b = t.pos()
# draw right set of lines
t.left(90)
t.penup()
t.fd(size / 2)
r_b_b = t.pos()
t.left(90)
t.pendown()
t.fd(size)
r_b_t = t.pos()
t.penup()
t.fd(gap)
t.pendown()
r_t_b = t.pos()
t.fd(size)
r_t_t = t.pos()
# draw the top right leg
t.left(45)
t.goto(m_t_t + y_offset)
# draw top left leg
t.left(90)
t.goto(l_t_t)
# draw bottom left leg
t.penup()
t.setheading(270)
t.goto(start)
t.pendown()
t.left(45)
t.goto(m_b_b + y_offset * -1)
t.left(90)
t.goto(r_b_b)
# draw the middle legs
t.penup()
t.left(90)
t.goto(l_t_b)
t.right(180)
t.pendown()
t.goto(m_b_t)
t.setheading(90)
t.penup()
t.goto(m_t_b)
t.pendown()
t.right(135)
t.goto(r_b_t)
# draw final connecting parts
t.setheading(90)
t.penup()
midpoint = (m_t_b + r_b_t) * .5
t.goto(r_t_b)
t.left(90)
t.pendown()
# t.goto(m_t_b)
t.goto(midpoint)
t.penup()
t.left(45)
midpoint2 = (m_b_t + l_t_b) * 0.5
t.goto(l_b_t)
t.setheading(360)
t.pendown()
# t.goto(m_b_t)
t.goto(midpoint2)
# loop and make a bunch of them!!
num_s = 100
t = turtle.Turtle()
t.screen.colormode(255)
t.speed(10)
x_range = 200
y_range = 200
for i in range(num_s):
size = random.randint(10, 50)
x = random.randint(-x_range, x_range)
y = random.randint(-y_range, y_range)
r = random.randint(1, 255)
g = random.randint(1, 255)
b = random.randint(1, 255)
t.pencolor((r, g, b))
t.width(random.randint(1, 10))
# change background color
r = random.randint(1, 255)
g = random.randint(1, 255)
b = random.randint(1, 255)
turtle.bgcolor((r, g, b))
draw_s(t, size, start=(x, y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment