-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·70 lines (59 loc) · 2.08 KB
/
main.lua
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
-- define W and H of the screen
W, H = display.contentWidth, display.contentHeight
-- startup the physics engine
local physics = require("physics")
physics.start()
-- load sounds
local wheee = audio.loadSound("wheee.wav")
local ouch = audio.loadSound("ouch.wav")
local gotcha = audio.loadSound("gotcha.wav")
-- sky background
local sky = display.newImageRect("sky.jpg", W, H)
sky.x, sky.y = W/2, H/2
-- draw a circle
local circle = display.newImageRect( "colorful_basketball_128.png", 40, 40 )
circle.x, circle.y = 100, H/2
physics.addBody(circle, "dynamic", {radius=20, density=0.5, friction=1, bounce=0.35})
-- draw a floor
local floor = display.newImageRect("grass.jpg", W, 30)
floor.x, floor.y = W/2, H-15
physics.addBody(floor, "static", {density=1.0, friction=1, bounce=0.65})
-- touch event for the circle to kick the ball
function circle:touch(event)
if (event.phase == "began") then
self:applyLinearImpulse(10, -5, self.x, self.y)
audio.play(wheee)
end
end
circle:addEventListener("touch", circle)
-- make some blocks to knock down
for i=1, 4 do
local block = display.newImageRect("crate.jpg", 30, 30)
block.x, block.y = W-100, H-30-30*i
physics.addBody(block, "dynamic", {density=0.5, friction=1, bounce=0.35})
end
for i=1, 4 do
local block = display.newImageRect("crate.jpg", 30, 30)
block.x, block.y = W-200, H-30-30*i
physics.addBody(block, "dynamic", {density=0.5, friction=1, bounce=0.35})
end
-- add a roof
local roof = display.newImageRect("crate.jpg", 130, 20)
roof.x, roof.y = W-150, H-300
physics.addBody(roof, "dynamic")
-- add a target circle
local target = display.newImageRect( "dora.png", 60, 60 )
target.x, target.y = W-150, H-100
physics.addBody(target, "dynamic", {radius=30, density=0.5, friction=1, bounce=0.35})
-- ouch on collision with target
function onCollision(event)
if (event.phase == "began") then
if (event.object1 == circle and event.object2 == target) then
audio.play(gotcha)
end
if (event.object1 ~= circle and event.object1 ~= floor and event.object2 == target) then
audio.play(ouch)
end
end
end
Runtime:addEventListener("collision", onCollision);