-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
266 lines (159 loc) · 5.1 KB
/
main.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
#!/usr/bin/env python
# library turtle is used for graphics
import turtle
# while loop to secure right inputs. Inputs have to be integers, if they're not, user is asked to input different value
while True:
# two verctor velocities get declared and are assigned values by the user
vx0 = input("Enter vx0: ")
vy0 = input("Enter vy0: ")
try:
vx0 = int(vx0)
break
except ValueError:
pass
try:
vy0 = int(vy0)
break
except ValueError:
pass
vx0 = int(vx0)
vy0 = int(vy0)
# parameters of the main screen
sc = turtle.Screen()
sc.bgcolor("black")
sc.setup(width= 1280, height= 720)
sc.tracer(0)
# text-writing pens that write x and y coordinates
txty = turtle.Turtle()
txty.speed(0)
txty.color("white")
txty.penup()
txty.hideturtle()
txtx = turtle.Turtle()
txtx.speed(0)
txtx.color("white")
txtx.penup()
txtx.hideturtle()
# text-writing pen that notes coordinates on the left
txt = turtle.Turtle()
txt.speed(0)
txt.color("white")
txt.penup()
txt.hideturtle()
# objects representing the cannon ball
sq = turtle.Turtle()
sq.speed(0)
sq.shape("square")
sq.color("white")
# red squeare that represents hight on y axes
sqy = turtle.Turtle()
sqy.speed(0)
sqy.shape("square")
sqy.color("red")
sqy.shapesize(0.3, 0.3)
sqy.penup()
# red square that represents length on x axes
sqx = turtle.Turtle()
sqx.speed(0)
sqx.shape("square")
sqx.color("red")
sqx.shapesize(0.3, 0.3)
sqx.penup()
# pens that draw horizontal and verticals parts of the grid
peny = turtle.Turtle()
peny.color("white")
peny.hideturtle()
penx = turtle.Turtle()
penx.color("white")
penx.hideturtle()
# for loop used to draw horizontal (y axes) of the grid
for i in range(15):
peny.forward(700)
peny.right(180)
peny.forward(700)
peny.right(90)
peny.forward(25)
peny.right(90)
# for loop used to draw vertical (x axes) of the grid
# (penx.left(90) because turtle's forward is up ^ by default)
penx.left(90)
for i in range(29):
penx.forward(350)
penx.left(180)
penx.forward(350)
penx.left(90)
penx.forward(25)
penx.left(90)
# for loop that writes y values next to the grid. One square is equal to 25 meters (pixels)
# txty is moved to start at the right spot (center)
grid_y = 25
txty.right(180)
txty.forward(30)
txty.right(90)
for i in range(14):
txty.forward(25)
txty.write(
grid_y,
font=("Courier", 10)
)
grid_y += 25
# for loop that writes x values next to the grid. One square is also equal to 25 meters (pixels)
# txtx is moved to the right place (center)
grid_x = 25
txtx.right(90)
txtx.forward(20)
txtx.left(90)
for i in range(28):
txtx.forward(25)
txtx.write(
grid_x,
font=("Courier", 7)
)
grid_x += 25
# txt is moved to these coordinates to write height and lenght there
txt.goto(-600, 250)
# declaring variables used in the equations below
# t is time, get's updated each cycle in the for loop below (t AS SEEN BELOW IS NOT REAL TIME)
t = " "
# x0 is the horizontal distance from position [0; 0]
x0 = 0
# y0 is the vertical distance from position [0; 0]
y0 = 0
# g as always is gravitional acceleration on Earth
g = 9.8
# in range of ... variable defines how precise the path written by the canon ball will be
n = 10000
# list that stores every achieved height (can't be in the loop, would overwrite itself) and is then searched for the heighest number to
# display maximum height
list_of_heights = []
# for loop that takes care of displaying the flight of the canon ball
for i in range(n):
# t (time) gets updated each cycle
t = float(i)/n * 1000
# coordinates are defined by equations of motion of a body in a gravitational field
iks = x0 + vx0 * t
ypsilon = y0 + vy0 * t - 0.5 * g * t * t
# coordinates of the cannon ball are updated each cycle -> it moves on the screen
sq.goto(iks, ypsilon)
# coordinates the two red indicator squares get also updated each cycle -> they move
sqy.goto(0, sq.ycor())
sqx.goto(sq.xcor(), 0)
# these 3 lines append every achieved height to list_of_heights and then the list is searched for the highest number (highest altitude)
# top_height is updated each cycle -> top_height has the same value as sq.ycor() on the way up, when is the top reached, it stops on the highest spot
height = sq.ycor()
list_of_heights.append(height)
top_height = int(max(list_of_heights))
# pen that writes x and y coordinates (updated each cycle) to the left top corner
txt.clear()
txt.write(
"Distance: {} meters\nHeight: {} meters\n\nTop height: {} meters".format(int(sq.xcor()), int(sq.ycor()), top_height),
font=("Courier", 20, "normal")
)
# if that stops the loop when the ball hits the ground (y == 0)
if sq.ycor() < 0:
break
# this refreshes the screen each cycle
sc.update()
# the main screen would shut down after the end of the for loop -> this keeps the screen going for ever :)
while True:
sc.update()