-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathcannon004_turtle.py
343 lines (295 loc) · 13.7 KB
/
cannon004_turtle.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# cannon shooting example
# see https://en.wikipedia.org/wiki/Projectile_motion
# https://de.wikipedia.org/wiki/Wurfparabel#/media/Datei:Wurfparabel_Zusammenfassung_aktualisierung.png
# this variant is text-only, but includes angle and a class instead a dict for data
# inspired by "Physics for Game Prgrammers, by Grant Palmer, Apress
import math
import turtle
class Data:
"""store global variables here"""
speed = 100
angle = 20
cannon_x = 0 # x-position of cannon
cannon_y = 0 # y-position of cannon
critical_distance_to_target = 1 # how close to the target a cannonball must land to count as a hit
dt = 0.01 # delta time, a smaller number makes a slower, but more precise calculation of the flight path
t_max = 100000 #
gravity = -9.81
target_x = 100 # x-postion of the target ,
target_y = 0
history = []
def ask_speed_and_angle():
#speed_default = Data.speed # data["speed"]
#try:
#new_speed = float(input(f"inital speed in [m/s] (enter={speed_default})"))
lines = "\n".join(Data.history)
new_speed = turtle.numinput(title= "please enter speed: (0 to quit)",
prompt= f"{lines}\n\nspeed in [m/s] >>>",
default= Data.speed,
)
#except ValueError:
# new_speed = speed_default
#print("speed is set to", new_speed)
Data.speed = new_speed #data["speed"] = new_speed
#angle_default = Data.angle # data["angle"]
#try:
#new_angle = float(input(f"angle in degree (enter={angle_default})"))
new_angle = turtle.numinput(title="please enter angle: (0 to quit)",
prompt = f"{lines}\n\nangle in [Grad] >>>",
default = Data.angle,
)
#except ValueError:
# new_angle = angle_default
#print("angle is set to", new_angle)
Data.angle = new_angle #data["angle"] = new_angle
#return data
def ask_game_parameters():
"""ask for every parameter except angle and speed and history
change this value depending on user input"""
keys = [k for k in Data.__dict__.keys() if "__" not in k and k not in ("angle", "speed", "history")]
for key in keys:
value = Data.__dict__[key]
#print(f"the current value for {key} is {value}")
#try:
# new_value = float(input(f"enter new value for {key}: (enter=accept {value})"))
#except ValueError:
# new_value = value
new_value = turtle.numinput(title="change or accept parameter",
prompt=f"enter new value for {key}",
default=value)
if new_value != value:
#if key in ["x0", "y0"] and new_value < 0:
# continue
#data[key] = new_value
## Data.__setattr__(key, value) # wrong!
setattr(Data, key, new_value) # right! updating a class attribute
#print(f"{key} changed to: {new_value}")
#return data
#display_parameters()
def play():
#print(f'the target is at x position {Data.target_x} and at y position 0')
#print(f'the cannon is at x position {Data.x0} and at y position {Data.y0}')
#print("change angle and speed to hit the target as close as possible. (or enter 0 for both values to quit)")
number = 1
Data.history = []
make_grid()
#turtle.clearscreen()
cannon=turtle.Turtle()
cannon.shape("arrow")
cannon.pencolor("blue")
cannon.speed(0)
cannon.penup()
cannon.goto(Data.cannon_x, Data.cannon_y)
cannon.write("cannon ", align="right", font=("Arial", 8, "normal"))
ball = turtle.Turtle()
ball.shape("circle")
red_value = (1 - number * 0.05) % 1 # can not be negative
ball.pencolor((red_value,0,0))
goal = turtle.Turtle()
goal.shape("square")
goal.penup()
goal.speed(0)
goal.goto(Data.target_x, Data.target_y)
goal.pencolor("green")
goal.write(" target", align="left", font=("Arial", 8, "normal"))
pen = turtle.Turtle()
pen.penup()
pen.speed(0)
while True:
pen.goto(Data.target_x, Data.target_x*2 - 10)
pen.write(f"shot number {number}", align="center", font=("Arial", 20, "normal"))
#print(f"shot number {number}")
ask_speed_and_angle() # ask for speed and angle
pen.clear()
if Data.speed == 0 and Data.angle == 0:
print("aborting game")
return
cannon.setheading(Data.angle)
#cannon.pendown()
#goal.pendown()
#ball.clear()
ball.penup()
ball.speed(0)
ball.goto(cannon.pos())
ball.pendown()
# max_range does not seem to work correctly when x0 and y0 is changed
#max_range = abs(
# (Data.speed ** 2 / Data.gravity) *
# math.cos(math.radians(Data.angle)) *
# (math.sin(math.radians(Data.angle)) + (math.sin(math.radians(Data.angle)) ** 2 +
# (2 * Data.y0 * Data.gravity / Data.speed ** 2)) ** 0.5)
#)
vx0 = math.cos(math.radians(Data.angle)) * Data.speed
vy0 = math.sin(math.radians(Data.angle)) * Data.speed
t = 0
y = 0
#if show_xy:
#ball.penup()
#ball.goto()
#cannonball = turtle.Turtle
while t < Data.t_max:
t += Data.dt # next time step
x = Data.cannon_x + vx0 * t # constant horizontal speed
y = Data.cannon_y + vy0 * t + 0.5 * Data.gravity * t * t
dx = x - ball.xcor()
dy = y - ball.ycor()
ball.goto(x,y)
#print(f"time: {t:.3f}, x:{x:.3f} y:{y:.3f} ")
distance_to_target = ball.pos() - goal.pos()
if abs(distance_to_target) <= Data.critical_distance_to_target:
break
elif Data.gravity < 0 and dy < 0 and y < goal.ycor():
break
elif Data.gravity > 0 and dy > 0 and y > goal.ycor():
break
if dx < 0 and x < Data.__lower_left_x:
break
if dx > 0 and x > Data.__upper_right_x:
break
#print("dist:", distance_to_target)
#pen.penup()
#pen.goto(Data.target_x, Data.target_x * 2 - 20)
#pen.write()
#distance_to_target = Data.target_x - max_range
text = f"your shot #{number} (speed: {Data.speed} angle: {Data.angle}) lands {abs(distance_to_target):.2f} m too {'short' if ball.xcor() < goal.xcor() else 'wide'}"
Data.history.append( text)
if abs(distance_to_target) < Data.critical_distance_to_target:
# print("you hit the target!")
turtle.textinput(title="You won!",
prompt = "\n".join(Data.history) + f"\ncritical distance to target = {Data.critical_distance_to_target}\nCongratulations!!!\nYou won\npress enter to continue")
#pen.write("you hit the target! (click to exit", align="center", font=('Arial', 12, 'normal'))
#turtle.exitonclick()
return
#text_y = Data.target_x * 2
#text_x = Data.target_x
#for line_number, line in enumerate(Data.history, 1):
# pen.goto(text_x, text_y - line_number * 10)
# pen.write(line, align="center", font=('Arial', 12, 'normal'))
#pen.penup()
#pen.goto(Data.target_x, Data.target_x * 2 - 40)
#pen.write("try again...", align="center", font=('Arial', 8, 'normal'))
#print("try again....")
number += 1
def quit():
turtle.bye()
def display_parameters():
"""returns an array of text lines (without \n) containing the formatted parameters"""
lines = []
lines.append("parameters:")
keys = [k for k in Data.__dict__.keys() if "__" not in k and k not in ("angle", "speed", "history")]
max_length = max([len(key) for key in Data.__dict__.keys()])
max_length2 = max([len(str(value)) for k, value in Data.__dict__.items() if k in keys])
lines.append("-"*max_length + "--" + "-" * max_length2 +"-----")
for key in keys:
value = Data.__dict__[key]
lines.append(f"{' ' * (max_length-len(key))}{key}: {value:>10.2f}")
lines.append("-" * max_length + "--" + "-" * max_length2 +"-----")
return lines
def calculate_world():
myscreen = turtle.getscreen()
Data.__lower_left_x = min(-10, Data.cannon_x - 10, Data.target_x * 2)
Data.__lower_left_y = min(-10, Data.cannon_y - 10, Data.target_y - 10)
Data.__upper_right_x = max(10, Data.cannon_x * 1, Data.target_x * 2)
Data.__upper_right_y = max(10, Data.cannon_y * 1, Data.target_x * 2, Data.target_y + 10)
myscreen.setworldcoordinates(Data.__lower_left_x, Data.__lower_left_y, Data.__upper_right_x, Data.__upper_right_y)
return myscreen
def make_grid():
myscreen = calculate_world()
#myscreen = turtle.getscreen()
myscreen.clearscreen()
#Data.__lower_left_x = min(-10, Data.cannon_x -10, Data.target_x * 2)
#Data.__lower_left_y = min(-10, Data.cannon_y -10, Data.target_y -10 )
#Data.__upper_right_x = max(10, Data.cannon_x * 1, Data.target_x * 2)
#Data.__upper_right_y = max(10, Data.cannon_y * 1, Data.target_x * 2, Data.target_y + 10)
#myscreen.setworldcoordinates(Data.__lower_left_x, Data.__lower_left_y, Data.__upper_right_x, Data.__upper_right_y)
gridpen = turtle.Turtle()
gridpen.speed(0)
gridpen.penup()
for y in range(Data.__lower_left_y, Data.__upper_right_y, (Data.__upper_right_y - Data.__lower_left_y) // 20 ):
gridpen.goto(Data.__lower_left_x, y)
gridpen.goto(gridpen.pos()[0] + 5, gridpen.pos()[1] + 1)
gridpen.write(f"{y:>4}", align="right", font=("CourierNew", 8, "normal") )
gridpen.goto(Data.__lower_left_x, y)
gridpen.pendown()
gridpen.goto(Data.__upper_right_x, y)
gridpen.penup()
for x in range(Data.__lower_left_x, Data.__upper_right_x, (Data.__upper_right_x - Data.__lower_left_x) // 20 ):
gridpen.goto(x, Data.__lower_left_y)
gridpen.goto(gridpen.pos()[0] + 5, gridpen.pos()[1] + 1)
gridpen.write(f"{x:>4}", align="right", font=("CourierNew", 8, "normal") )
gridpen.goto(x, Data.__lower_left_y)
gridpen.pendown()
gridpen.goto(x, Data.__upper_right_y)
gridpen.penup()
# thick zero axis
gridpen.pensize(2)
gridpen.goto(Data.__lower_left_x,0 )
gridpen.pendown()
gridpen.goto(Data.__upper_right_x, 0)
gridpen.penup()
gridpen.goto(0, Data.__lower_left_y)
gridpen.pendown()
gridpen.goto(0, Data.__upper_right_y)
gridpen.hideturtle()
def game():
print("*** Cannon game ***")
myscreen = calculate_world()
#myscreen = turtle.getscreen()
#myscreen.setup(width=.85, height=0.85, startx=None, starty=None) # turtle window is 85% of screen width/height
#Data.__lower_left_x = min(-10, Data.cannon_x * 2, Data.target_x * 2)
#Data.__lower_left_y = min(-10, Data.cannon_y * 2, Data.target_y * 2)
#Data.__upper_right_x = max(10, Data.target_x * 2, Data.cannon_x * 2)
#Data.__upper_right_y = max(10, Data.cannon_y * 2, Data.target_y * 2)
#myscreen.setworldcoordinates(Data.__lower_left_x, Data.__lower_left_y, Data.__upper_right_x, Data.__upper_right_y)
#myscreen.setworldcoordinates(min(0, Data.cannon_x), min(0, Data.cannon_y), max(Data.target_x * 2, 0), max(Data.target_x * 2, Data.target_y * 2, 0))
#turtle.clearscreen()
#write_menu()
while True:
turtle.clearscreen()
#make_grid()
menupen = turtle.Turtle()
menupen.pencolor("green")
menupen.speed(0)
menupen.penup()
menupen.goto(Data.__lower_left_x + (Data.__upper_right_x - Data.__lower_left_x) //2, Data.__upper_right_y - 10 )
menupen.right(90)
menupen.write("Cannon Game", align="center", font=('Arial', 20, 'bold'))
#menupen.goto(Data.target_x, Data.target_x * 2 - 40)
menupen.forward(20)
menupen.write("change angle and speed of your cannon to hit the target", align="center",
font=('Arial', 15, 'bold'))
menupen.forward(10)
for line in display_parameters():
menupen.forward(5)
menupen.write(line, align="left", font=('CourierNew', 12, 'normal'))
#print("please enter your command:")
#command = input("[c]hange game parameters, [p]lay, [q]uit (enter=play)")
command = turtle.textinput(title="Cannon Game Menu",
prompt = "press:\nc ... to change game parameters\np ... to play\nq ... to quit\n\ntype your command please (and ENTER) >>>")
if command.lower() == "p" or command == "":
play()
elif command.lower() == "c":
ask_game_parameters()
myscreen.setworldcoordinates(0, 0, Data.target_x * 2, Data.target_x * 2)
elif command.lower() == "q":
quit()
#myscreen.onkey(play, "p") # function name without parantheses!
#myscreen.onkey(ask_game_parameters, "c")
#myscreen.onkey(quit, "q")
#myscreen.listen() # set focus to turtlescreen and listen to keyboard events
#if command.lower() not in ["c", "p", "q", "", "change", "quit", "play",]:
# continue
#elif command.lower() in ["q", "quit"]:
# break
#elif command.lower() in ["c", "change"]:
# data = ask_game_parameters()
# continue
#elif command.lower() in ["p", "", "play"]:
# play(show_xy=True)
#elif command.lower() in ["d", "details"]:
# play(show_xy=True)
#else:
# print("i can not understand your command, please try again")
print ("bye bye! Thanks for playing")
if __name__ == "__main__":
game()