-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtleTriangles.py
More file actions
61 lines (52 loc) · 1.59 KB
/
TurtleTriangles.py
File metadata and controls
61 lines (52 loc) · 1.59 KB
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
60
61
#!/usr/bin/python3
#
# TurtleTriangles.py
#
# Make triangle art with the Python Turtles module.
# Inspired by Runestone Academy's "Foundations of Python Programming"
# (https://runestone.academy/ns/books/published/fopp/index.html)
#
# 2022-May-29 - suuze_linux
#
# Import required modules
import math
import random
import turtle
# Define variables, lists and functions
colorlist = ["blue", "red", "yellow", "hotpink", "grey", "purple", "green", "turquoise"]
sizelist = [50, 100, 150]
def draw_triangle(dorcassize, dorcascolor, dorcasx, dorcasy, dorcasheading):
dorcas = turtle.Turtle() # create a turtle named dorcas
dorcas.color(dorcascolor)
dorcas.shape("turtle")
dorcas.pensize(3)
dorcas.penup()
dorcas.goto(dorcasx,dorcasy)
dorcas.setheading(dorcasheading)
dorcas.pendown()
dorcas.forward(dorcassize)
dorcas.left(90)
dorcas.forward(dorcassize)
dorcas.left(135)
hypotenuse = math.sqrt((dorcassize ** 2) * 2)
dorcas.forward(hypotenuse)
# Setup new screen and turtle
window = turtle.Screen()
window.bgcolor("orange")
# Main program
innumber = input("Enter number of triangles to draw, or 'q' to quit: ")
if innumber == "q":
print("Click on the triangles' window to exit!")
# break
else:
try:
number = int(innumber)
for _ in range(1, number):
# Call function draw_triangle()
draw_triangle(random.choice(sizelist), \
random.choice(colorlist), random.randint(-175, 175), \
random.randint(-175, 175), random.randrange(0, 360, 10))
except ValueError as ve:
print("Please enter an integer.")
print("Please click on triangles' window to exit.")
window.exitonclick()