• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Dec 14th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import turtle
2. import math
3. import random
4.
5. SCREEN_X = 1200
6. SCREEN_Y = 800
7.
8. window = turtle.Screen()
9. window.bgpic('images/background.png')
10. window.setup(1200 + 3, 800 + 3)
11. window.screensize(SCREEN_X,SCREEN_Y)
12. # window.tracer(n=2)
13.
14. BASE_X, BASE_Y = 0, -300
15.
16.
18.     dx = x2 - x1
19.     dy = y2 - y1
20.     length = (dx ** 2 + (dy) ** 2) ** 0.5
21.     cos_alpha = dx/length
22.     alpha = math.acos(cos_alpha)
23.     alpha = math.degrees(alpha)
24.     if dy < 0:
25.         alpha = - alpha
26.     return alpha
27.
28.
29. def fire_missile(x, y):
30.     missile = turtle.Turtle(visible=False)
31.     missile.speed(0)
32.     missile.color('white')
33.     missile.penup()
34.     missile.setpos(x=BASE_X, y=BASE_Y)
35.     missile.pendown()
36.     heading = calc_heading(x1=BASE_X, x2=x, y1=BASE_Y, y2=y)
38.     missile.showturtle()
39.     info = {'missile': missile, 'target': [x,y], 'state': 'launched', 'radius': 0}
40.     our_missiles.append(info)
41.
42. def fire_enemy_missile(x, y):
43.     enemy_x = random.randint(-SCREEN_X/2, SCREEN_X/2)
44.     enemy_y = SCREEN_Y/2
45.     missile = turtle.Turtle(visible=False)
46.     missile.speed(0)
47.     missile.color('red')
48.     missile.penup()
49.     missile.setpos(x=enemy_x, y=enemy_y)
50.     missile.pendown()
51.     heading = calc_heading(x1=enemy_x, x2=x, y1=enemy_y, y2=y)
53.     missile.showturtle()
54.     missile.forward(10)
55.
56.     flare = turtle.Turtle(visible=True)
57.     flare.color('#d000ff')
58.     flare.shape('circle')
59.     flare.shapesize(0.2)
60.     flare.speed(0)
61.     flare.penup()
62.     flare.setpos(x=enemy_x, y=enemy_y)
64.
65.     info = {'missile': missile, 'target': [x,y], 'state': 'launched', 'radius': 0, 'flare': flare}
66.     enemy_missiles.append(info)
67.
68.
69. window.onclick(fire_missile)
70.
71. our_missiles = []
72. enemy_missiles = []
73.
74. while True:
75.     window.update()
76.
77.     if len(enemy_missiles) < 5:
78.         fire_enemy_missile(BASE_X, BASE_Y)
79.
80.     for missile_info in our_missiles:
81.         state = missile_info['state']
82.         missile = missile_info['missile']
83.         if state == 'launched':
84.             missile.forward(4)
85.             target = missile_info['target']
86.             if missile.distance(x=target[0], y=target[1]) < 20:
87.                 missile_info['state'] = 'explode'
88.                 missile.shape('circle')
89.         elif state == 'explode':
90.             missile_info['radius'] += 1
92.             if missile_info['radius'] > 5:
93.                 missile_info['state'] = 'dead'
94.                 missile.clear()
95.                 missile.hideturtle()
96.
97.     for missile_info in enemy_missiles:
98.         state = missile_info['state']
99.         missile = missile_info['missile']
100.         flare = missile_info['flare']
101.         if state == 'launched':
102.             missile.forward(4)
103.             flare.forward(4)
104.             flare.shapesize(random.randint(1, 4)/10)
105.             target = missile_info['target']
106.             if missile.distance(x=target[0], y = target[1]) < 20:
107.                missile_info['state'] = 'explode'
108.                missile.shape('circle')
109.         elif state == 'explode':
110.             missile_info['radius'] += 1
112.             if missile_info['radius'] > 5:
113.                 missile_info['state'] = 'dead'
114.                 missile.clear()
115.                 missile.hideturtle()
116.
117.     dead_our_missiles = [info for info in our_missiles if info['state'] == 'dead']