-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyramid.py
59 lines (52 loc) · 1.44 KB
/
pyramid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Import turtle graphics library
import turtle
#Import Python Math Libary
from math import *
from random import *
# Function to draw a square about the current position
def drawSquareFromCenter(turtle, x):
turtle.penup()
turtle.forward(-x / 2)
turtle.right(90)
turtle.forward(x / 2)
turtle.left(90)
turtle.pendown()
turtle.forward(x)
turtle.left(90)
turtle.forward(x)
turtle.left(90)
turtle.forward(x)
turtle.left(90)
turtle.forward(x)
turtle.left(90)
turtle.penup()
turtle.forward(x / 2)
turtle.left(90)
turtle.forward(x / 2)
turtle.right(90)
#TODO: Function to draw n squares in a row and go back to origin
def repeat(turtle, size, count):
for i in range(count):
random_color(turtle)
drawSquareFromCenter(turtle, size)
turtle.fd(size)
distance = count*size
turtle.bk(distance)
def random_color(turtle):
r, g, b = randint(0, 255), randint(0, 255), randint(0, 255)
turtle.pencolor((r,g,b))
def main():
bottom_row = int(input("Enter a number: "))
screen = turtle.Screen()
screen.colormode(255)
box_size = 30
bob = turtle.Turtle()
bob.speed(8)
for i in range(bottom_row):
current_row = bottom_row - i
repeat(bob,box_size,current_row)
bob.lt(90)
bob.fd(box_size)
bob.rt(90)
bob.fd(box_size/2)
main()