Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active September 30, 2020 00:35
Show Gist options
  • Save shinysu/c4fb35b7f7bd9b95e8cb1b53368cd2a5 to your computer and use it in GitHub Desktop.
Save shinysu/c4fb35b7f7bd9b95e8cb1b53368cd2a5 to your computer and use it in GitHub Desktop.
session1 - python turtle
'''
draw a line using turtle
'''
import turtle
t = turtle.Turtle()
t.forward(100)
'''
draw a square
'''
import turtle
t = turtle.Turtle()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
'''
draw a square using loop
The statements within the for loop executes 4 times for i values, 0, 1, 2,3
key concept - using loops for repeated statements
'''
import turtle
t = turtle.Turtle()
for i in range(4):
t.forward(100)
t.left(90)
'''
draw a square - replace all magic numbers with variables
key concept - using variables for storing values
'''
import turtle
t = turtle.Turtle()
sides = 4
length = 100
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
'''
draw a polygon - using a function
key concept - using functions for block of code that performs an action
syntax: def function_name():
'''
import turtle
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
t = turtle.Turtle()
draw_shape(t, 4, 100)
'''
draw a pattern
'''
import turtle
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
t=turtle.Turtle()
t.color("red")
for i in range(20):
draw_shape(t, 4, 100)
t.left(18)
'''
fill the polygon with colors
'''
import turtle
t = turtle.Turtle()
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
t.color("green")
t.begin_fill()
draw_shape(t, 4, 100)
t.end_fill()
'''
random shape - draw random shapes with random length
use the randint() function to get a random integer in a range
'''
import turtle
from random import randint
t = turtle.Turtle()
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
sides = randint(3, 8)
length = randint(50, 300)
t.color("green")
t.begin_fill()
draw_shape(t, sides, length)
t.end_fill()
'''
move the turtle to a location and draw the shape
t.goto(x, y) is used move the turtle to the location(x,y) in screen
'''
import turtle
from random import randint
t = turtle.Turtle()
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
sides = randint(3, 8)
length = randint(50, 300)
t.penup()
t.goto(100, 100)
t.pendown()
t.color("green")
t.begin_fill()
draw_shape(t, sides, length)
t.end_fill()
'''
write a function to move the turtle
'''
import turtle
from random import randint
t = turtle.Turtle()
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
def move(t, x, y):
t.penup()
t.goto(x, y)
t.pendown()
sides = randint(3, 8)
length = randint(50, 300)
move(t, 100, 100)
t.color("green")
t.begin_fill()
draw_shape(t, sides, length)
t.end_fill()
'''
move the draw_shape() and move() functions to a module names utils.py and import it here
'''
import turtle
from random import randint
from utils import draw_shape, move
t = turtle.Turtle()
sides = randint(3,8)
length = randint(50,300)
move(t, 100, 100)
t.color("red")
draw_shape(t, sides, 100)
move(t, -100,-100)
draw_shape(t, sides, 100)
'''
get random colour for the shape
use list to store a list of colors to chose from
'''
import turtle
from random import randint
from utils import draw_shape, move
t = turtle.Turtle()
def get_random_color():
colours = ["violet", "indigo", "blue", "green", "yellow", "orange", "red"]
shape_color = colours[randint(0, len(colours) - 1)]
return shape_color
sides = randint(3, 8)
length = randint(50, 300)
move(t, 100, 100)
colour = get_random_color()
t.color(colour)
t.begin_fill()
draw_shape(t, sides, length)
t.end_fill()
'''
draw a house
'''
import turtle
from random import randint
from utils import draw_shape, move
t = turtle.Turtle()
def get_random_color():
colours = ["violet", "indigo", "blue", "green", "yellow", "orange", "red"]
shape_color = colours[randint(0, len(colours) - 1)]
return shape_color
colour = get_random_color()
t.color(colour)
t.begin_fill()
draw_shape(t, 4, 100)
t.end_fill()
'''triangle roof of the house'''
move(t, 0, 100)
colour = get_random_color()
t.color(colour)
t.begin_fill()
draw_shape(t, 3, 100)
t.end_fill()
'''door of the house'''
move(t, 40, 0)
colour = get_random_color()
t.color(colour)
t.begin_fill()
draw_shape(t, 4, 20)
t.end_fill()
'''
utils module with two functions draw_shape and move
'''
def draw_shape(t, sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
def move(t, x, y):
t.penup()
t.goto(x, y)
t.pendown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment