13
13
import math
14
14
from Tkinter import *
15
15
16
+ # threading for collision sounds
17
+ import thread
18
+ from thread import start_new_thread
19
+ import sounds
20
+
16
21
####################################
17
22
# customize these functions
18
23
####################################
19
24
20
-
25
+ collided = False
26
+
27
+ def collideSound (collided ):
28
+ # plays sound when collided
29
+ if collided == True :
30
+ start_new_thread (sounds .play , ('smallExplosion.wav' ,))
31
+ collided = False
21
32
22
33
def init (data ):
23
34
data .timer = 0
@@ -61,11 +72,18 @@ def timerFired(data):
61
72
# the 300 is just data.width and data.height
62
73
63
74
if math .sqrt ((x - 300 )** 2 + (y - 300 )** 2 ) <= r1 + r2 :
75
+ #collision happens
76
+
77
+ collided = True
78
+ collideSound (collided )
79
+
64
80
x1 , y1 = bullet .cx , bullet .cy
65
81
x2 , y2 = data .width / 2 , data .height / 2
66
82
67
83
# the 10 is just the radius of the sample ship i used
68
84
r1 , r2 = bullet .r , 10
85
+
86
+ # https://stackoverflow.com/questions/1736734/circle-circle-collision
69
87
pcollision = ((x1 * r2 + x2 * r1 * 1. )/ (r1 + r2 ),(y1 * r2 + y2 * r1 * 1. )/ (r1 + r2 ))
70
88
71
89
data .collisions .append (Collision (pcollision [0 ],pcollision [1 ], 2 , bullet .r ))
@@ -85,6 +103,7 @@ def timerFired(data):
85
103
86
104
def redrawAll (canvas , data ):
87
105
x ,y = data .width / 2 , data .height / 2
106
+ canvas .create_rectangle (0 ,0 ,data .width ,data .height ,fill = "lightskyblue4" )
88
107
89
108
for collision in data .collisions :
90
109
collision .draw (canvas )
@@ -98,7 +117,7 @@ def redrawAll(canvas, data):
98
117
99
118
canvas .create_text (0 ,0 , anchor = NW , text = "Time: %d" % data .seconds )
100
119
canvas .create_text (data .width ,0 , anchor = NE , text = "Level: %d" % data .level )
101
-
120
+
102
121
103
122
####################################
104
123
# use the run function as-is
@@ -147,4 +166,5 @@ class Struct(object): pass
147
166
root .mainloop () # blocks until window is closed
148
167
print ("bye!" )
149
168
150
- run (600 , 600 )
169
+ start_new_thread (run , (600 , 600 ))
170
+ start_new_thread (sounds .play , ('backgroundMusic.wav' ,))
0 commit comments