-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX6.41.py
52 lines (42 loc) · 1.55 KB
/
EX6.41.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
# 6.41 (Turtle: draw points, rectangles, and circles) Use the functions defined in Listing
# 6.14 to write a program that displays a rectangle centered at with width
# and height 100 and a circle centered at (50, 0) with radius 50. Fill 10 random
# points inside the rectangle and 10 inside the circle, as shown in Figure 6.11c.
# Draw a point at the specified location (x, y)
import random
import turtle
def drawPoint(x, y):
turtle.penup() # Pull the pen up
turtle.goto(x, y)
turtle.pendown() # Pull the pen down
turtle.begin_fill() # Begin to fill color in a shape
turtle.circle(3)
turtle.end_fill() # Fill the shape
# Draw a circle at centered at (x, y) with the specified radius
def drawCircle(x, y, radius):
turtle.penup() # Pull the pen up
turtle.goto(x, y - radius)
turtle.pendown() # Pull the pen down
turtle.circle(radius)
# Draw a rectangle at (x, y) with the specified width and height
def drawRectangle(x, y, width, height):
turtle.penup() # Pull the pen up
turtle.goto(x + width / 2, y + height / 2)
turtle.pendown() # Pull the pen down
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.forward(width)
def main():
drawRectangle(-75, 0, 100, 100)
drawCircle(50, 0, 50)
turtle.speed(5)
for i in range(10):
drawPoint(random.randint(-115,-30), random.randint(-50,40))
drawPoint(random.randint(20, 90), random.randint(-40, 25))
turtle.done()
main()