-
Notifications
You must be signed in to change notification settings - Fork 0
/
forest.py
269 lines (236 loc) · 6.3 KB
/
forest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
__author__ = 'Shweta Yakkali'
__author__ = 'Aishwarya Desai'
import turtle
import random
# global constants for window dimensions
WINDOW_WIDTH = 1600
WINDOW_HEIGHT = 1600
def init():
"""
Initialize for drawing. (-800, -800) is in the lower left and
(800, 800) is in the upper right.
:pre: pos (0,0), heading (north), up
:post: pos (0,0), heading (north), up
:return: None
"""
turtle.setworldcoordinates(-WINDOW_WIDTH / 2, -WINDOW_WIDTH / 2,
WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
turtle.up()
turtle.setx(-800)
turtle.sety(-800)
turtle.title('Forest')
turtle.speed(1)
def drawTrunk(length):
"""
Draw the Trunk.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (north), up
:return: None
"""
turtle.up()
turtle.left(90)
turtle.down()
turtle.forward(length)
def drawPine(length):
"""
Draw the Pine Tree.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (south), up
:return: None
"""
turtle.right(90)
turtle.forward(30)
turtle.left(120)
turtle.forward(60)
turtle.left(120)
turtle.forward(60)
turtle.left(120)
turtle.forward(30)
turtle.up()
turtle.right(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(100)
def drawMaple(length):
"""
Draw the Maple Tree.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (south), up
:return: None
"""
turtle.down()
turtle.right(90)
turtle.circle(25.9)
turtle.up()
turtle.right(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(100)
def drawThird(length):
"""
Draw a 3rd tree of our choice.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (south), up
:return: None
"""
turtle.right(45)
turtle.forward(36.7)
turtle.left(90)
turtle.forward(36.7)
turtle.left(90)
turtle.forward(36.7)
turtle.left(90)
turtle.forward(36.7)
turtle.up()
turtle.right(45)
turtle.forward(length)
turtle.left(90)
turtle.forward(100)
def star():
"""
Draws a star which starts at a position of 10 pixels higher than the tallest tree+ 52, where
52 is the height of the equilateral triangle in pine or diameter of circle in maple or diagonal of rhombus in 3rd tree.
The main function.
:pre: pos (0,0), heading (east), up
:post: pos (0,0), heading (south-east), up
:return: None
"""
global maxht
turtle.setx(-600)
turtle.sety(-(728-10-maxht)) #to print the star 10pixels higher than the tallest tree
turtle.down()
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.left(180)
turtle.forward(5)
turtle.right(90)
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.left(180)
turtle.forward(5)
turtle.right(45)
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.left(180)
turtle.forward(5)
turtle.left(90)
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.left(180)
turtle.forward(5)
def House(height,side):
"""
Draw the House.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (south), up
:return: None
"""
turtle.down()
turtle.left(90)
turtle.forward(height)
turtle.right(45)
turtle.forward(side)
turtle.right(90)
turtle.forward(side)
turtle.right(45)
turtle.forward(height)
turtle.left(90)
turtle.up()
turtle.forward(100)
def drawSun():
"""
Draw the Sun.
:pre: (relative) pos (0,0), heading (eat), up anticlockwise
:post: (relative) pos (0,0), heading (west), up
:return: None
"""
turtle.up()
turtle.sety(400)
turtle.setx(400)
turtle.down()
turtle.circle(20)
def drawRanTree(woods):
"""
Draw the Trees which are random.
:pre: (relative) pos (0,0), heading (north), up
:post: (relative) pos (0,0), heading (south), up
:return: None
"""
l=0
length=0
global wood
global maxht
l=random.randint(1,3)
if l==1:
length=random.randint(50,200)
if(maxht<length):
maxht=length
x=length
wood=wood+length
print("l=1",wood)
drawTrunk(length)
drawPine(length)
elif l==2:
length=random.randint(50,150)
if(maxht<length):
maxht=length
y=length
wood=wood+length
print("l=2",wood)
drawTrunk(length)
drawMaple(length)
elif l==3:
length=random.randint(50,100)
if(maxht<length):
maxht=length
z=length
wood=wood+length
print("l=3",wood)
drawTrunk(length)
drawThird(length)
return wood
def main():
"""
The main function.
:pre: pos (0,0), heading (east), up
:post: pos (0,0), heading (south-east), up
:return: None
"""
init()
global wood
global maxht
global totalwood
wood=0
maxht=0
totalwood=0
n=0
#scene=int(input("Do you want a day scene or night scene enter 1 for night and zero for day?"))
n=int(input("How many trees do you want in the forest ? "))
house=int(input("do you want a house? yes=1/no=0 : "))
a=random.randint(1,(n-1))
for i in range(n):
if (a==i and house==1):
House(100,71)
woodoftrunk=drawRanTree(wood)
#print("max height=",maxht)
star()
woodofhouse=100+71+71+100
totalwood=woodoftrunk+woodofhouse
print("total wood=",totalwood)
input("night is done press enter for day")
turtle.reset()
print("we have",totalwood,"units of lumber for building")
height=totalwood/(2+1.414)
print("we will build a house with walls",height,"tall")
side=height/1.414
House(height,side)
drawSun()
input("day is done,house is built,press enter to quit!")
input('Hit enter ')
turtle.bye()
if __name__ == '__main__':
main()