Skip to content

Commit 7ebf039

Browse files
committed
sounds on collisions plus background music
1 parent 064371b commit 7ebf039

File tree

14 files changed

+106
-3
lines changed

14 files changed

+106
-3
lines changed

.DS_Store

4 KB
Binary file not shown.

leapMotion/.DS_Store

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
31.3 MB
Binary file not shown.
172 KB
Binary file not shown.
1.24 MB
Binary file not shown.
270 Bytes
Binary file not shown.
120 Bytes
Binary file not shown.

leapMotion/LeapDeveloperKit_2.3.1/LeapSDK/lib/defender.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@
1313
import math
1414
from Tkinter import *
1515

16+
# threading for collision sounds
17+
import thread
18+
from thread import start_new_thread
19+
import sounds
20+
1621
####################################
1722
# customize these functions
1823
####################################
1924

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
2132

2233
def init(data):
2334
data.timer = 0
@@ -61,11 +72,18 @@ def timerFired(data):
6172
# the 300 is just data.width and data.height
6273

6374
if math.sqrt((x - 300)**2 + (y - 300)**2) <= r1 + r2:
75+
#collision happens
76+
77+
collided = True
78+
collideSound(collided)
79+
6480
x1, y1 = bullet.cx, bullet.cy
6581
x2, y2 = data.width/2, data.height/2
6682

6783
# the 10 is just the radius of the sample ship i used
6884
r1, r2 = bullet.r, 10
85+
86+
# https://stackoverflow.com/questions/1736734/circle-circle-collision
6987
pcollision = ((x1*r2+x2*r1*1.)/(r1+r2),(y1*r2+y2*r1*1.)/(r1+r2))
7088

7189
data.collisions.append(Collision(pcollision[0],pcollision[1], 2, bullet.r))
@@ -85,6 +103,7 @@ def timerFired(data):
85103

86104
def redrawAll(canvas, data):
87105
x,y = data.width/2, data.height/2
106+
canvas.create_rectangle(0,0,data.width,data.height,fill = "lightskyblue4")
88107

89108
for collision in data.collisions:
90109
collision.draw(canvas)
@@ -98,7 +117,7 @@ def redrawAll(canvas, data):
98117

99118
canvas.create_text(0,0, anchor = NW, text = "Time: %d" % data.seconds)
100119
canvas.create_text(data.width,0, anchor = NE, text = "Level: %d" % data.level)
101-
120+
102121

103122
####################################
104123
# use the run function as-is
@@ -147,4 +166,5 @@ class Struct(object): pass
147166
root.mainloop() # blocks until window is closed
148167
print("bye!")
149168

150-
run(600, 600)
169+
start_new_thread(run, (600, 600))
170+
start_new_thread(sounds.play, ('backgroundMusic.wav',))

0 commit comments

Comments
 (0)