Skip to content

Instantly share code, notes, and snippets.

@psivakrishnareddy
Created March 19, 2023 09:45
Show Gist options
  • Select an option

  • Save psivakrishnareddy/56a71ce044c5e66c656fb688efd7cb25 to your computer and use it in GitHub Desktop.

Select an option

Save psivakrishnareddy/56a71ce044c5e66c656fb688efd7cb25 to your computer and use it in GitHub Desktop.
This is a simple code exploring turtle package in python
from turtle import Screen, Turtle, colormode
import random
def random_color():
'''returns a Random rgb color tuple'''
return (random.randint(1,255),random.randint(1,255),random.randint(1,255))
turtle = Turtle()
colors = ('cornflower blue', 'lime green', 'red', 'dark magenta','light blue','gold','medium blue','dark orange')
directions = (0,90,180,270)
# turtle.shape('turtle')
turtle.color('red')
#draws a square
# for _ in range(4):
# turtle.forward(100) #Moves east by default
# turtle.right(90)
#Draws dashed line
# def draw_dashed_line():
# for _ in range(15):
# turtle.forward(10)
# turtle.penup()
# turtle.forward(10)
# turtle.pendown()
# turtle.right(90)
# for _ in range(4):
# draw_dashed_line()
# Shapes with angles
def draw_shape(num_sides):
angle = 360 / num_sides
for _ in range(num_sides):
turtle.forward(100)
turtle.right(angle)
# turtle.pensize(3)
# for i in range(3,12):
# ci = colors[i % len(colors)]
# turtle.color(ci)
# draw_shape(i)
# Random Walk
# colormode(255)
# turtle.pensize(10)
# turtle.speed(0)
# for _ in range(200):
# # turtle.color(random.choice(colors))
# rgb = random_color()
# turtle.color(rgb)
# turtle.forward(30)
# turtle.setheading(random.choice(directions))
#SPIRAL
# def draw_spiral(size_of_graph):
# colormode(255)
# turtle.speed('fastest')
# for _ in range(int(360 / size_of_graph)):
# turtle.color(random_color())
# turtle.begin_fill()
# # turtle.circle(100)
# draw_shape(5)
# # turtle.end_fill()
# turtle.setheading(turtle.heading() + size_of_graph)
# draw_spiral(5)
#Dot art
colormode(255)
turtle.speed(100)
turtle.penup()
dot_matrix = 15
number_of_dots = dot_matrix ** 2
turtle.setheading(225)
turtle.forward(500)
turtle.setheading(0)
for dot_count in range(1,number_of_dots+1):
turtle.dot(20,random_color())
turtle.forward(50)
if dot_count%dot_matrix == 0:
turtle.setheading(90)
turtle.forward(50)
turtle.setheading(180)
turtle.forward(750)
turtle.setheading(0)
turtle.hideturtle()
turtle.getscreen()
screen = Screen()
# screen.getcanvas().postscript(file="dot_art.eps")
screen.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment