Created
August 26, 2020 15:14
-
-
Save shinysu/6cc41a636fa267c823280f0736fad378 to your computer and use it in GitHub Desktop.
Example programs for day2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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(x, y): | |
t.penup() | |
t.goto(x, y) | |
t.pendown() | |
sides = randint(3, 8) | |
length = randint(50, 300) | |
move(100, 100) | |
t.color("green") | |
t.begin_fill() | |
draw_shape(t, sides, length) | |
t.end_fill() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
get random colour for the shape | |
use list to store a list of colors to chose from | |
''' | |
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(x, y): | |
t.penup() | |
t.goto(x, y) | |
t.pendown() | |
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(100, 100) | |
colour = get_random_color() | |
t.color(colour) | |
t.begin_fill() | |
draw_shape(t, sides, length) | |
t.end_fill() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
draw a house | |
''' | |
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(x, y): | |
t.penup() | |
t.goto(x, y) | |
t.pendown() | |
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(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(40, 0) | |
colour = get_random_color() | |
t.color(colour) | |
t.begin_fill() | |
draw_shape(t, 4, 20) | |
t.end_fill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment