Skip to content

Instantly share code, notes, and snippets.

@cyberience
Forked from sixman9/main.lua
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberience/f83609799a9b28fe27cb to your computer and use it in GitHub Desktop.
Save cyberience/f83609799a9b28fe27cb to your computer and use it in GitHub Desktop.
--[[
Provided by Tim Closs see https://devnet.madewithmarmalade.com/questions/3701/kinematic-bodies-in-marmalade-quick.html
Ping pong game.
Drag the bats up and down, and attempt to keep the ball in play.
The bats and ball are physics "sensor" objects, so generate collision
events but no physical responses.
--]]
local dw = director.displayWidth
local dh = director.displayHeight
local batW = 20
local batH = 80
local ballSpeed = 3
physics:setGravity(0, 0)
-- Touch listener to drag bats
function dragBat(event)
local bat = event.target
if event.phase == "began" then
bat.yTouchLast = event.y
elseif event.phase == "moved" then
bat.yVel = event.y - bat.yTouchLast;
bat.yTouchLast = event.y
bat.y = bat.y + bat.yVel
end
end
-- Collision listener
function hit(event)
if event.phase == "began" then
local bat, ball
if event.nodeA.radius then
bat = event.nodeB
ball = event.nodeA
else
ball = event.nodeB
bat = event.nodeA
end
if ball.vx < 0 then
ball.x = bat.x + (bat.w + ball.w)/2
else
ball.x = bat.x - (bat.w + ball.w)/2
end
ball.vx = -ball.vx
ball.vy = bat.yVel or 0
return true
end
end
-- Create left bat
local batL = director:createRectangle( {
x=batW*3/2, y=dh/2, w=batW, h=batH, name="batL",
color=color.darkRed, strokeColor=color.red,
} )
physics:addNode(batL, {isSensor=true})
batL:addEventListener("touch", dragBat)
batL:addEventListener("collision", hit)
-- Create right bat
local batR = director:createRectangle( {
x=dw-batW*3/2, y=dh/2, w=batW, h=batH, name="batR",
color=color.darkGreen, strokeColor=color.green,
} )
physics:addNode(batR, {isSensor=true})
batR:addEventListener("touch", dragBat)
batR:addEventListener("collision", hit)
-- Create ball
local ball = director:createCircle( {
x=dw/2, y=dh/2, radius=batW/2, name="ball",
} )
physics:addNode(ball, {isSensor=true, radius=ball.radius})
ball:addEventListener("collision", hit)
ball.inTimer = 2 -- count down from 2 seconds before starting ball
-- Update for ball
function updateBall(event)
-- Are we simply waiting for the ball to appear again?
if ball.inTimer > 0 then
ball.inTimer = ball.inTimer - system.deltaTime
if ball.inTimer < 0 then
ball.inTimer = 0
ball.isVisible = true
end
end
-- Bounce off top/bottom of display?
if ball.y < ball.radius then
ball.y = ball.radius
ball.vy = -ball.vy
elseif ball.y > dh - ball.radius then
ball.y = dh - ball.radius
ball.vy = -ball.vy
end
-- Move ball: add velocity to position
ball.x = ball.x + ball.vx
ball.y = ball.y + ball.vy
-- Ball out of play (off left/right)? If so, re-center
if ball.x < 0 or ball.x > dw then
ball.x = dw/2
ball.y = dh/2
ball.vx = ballSpeed
ball.vy = 0
ball.inTimer = 2 -- count down from 2 seconds before starting ball again
ball.isVisible = false
end
end
system:addEventListener("update", updateBall)
-- Add our own velocity properties to the ball sprite object
ball.vx = ballSpeed
ball.vy = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment