2
0

Main.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ------------------------------------------------
  2. -- Polycode Pong example by Ivan Safrin, 2013
  3. ------------------------------------------------
  4. -- create a new Screen and set its height to 480
  5. scene = PhysicsScene2D(1.0, 30)
  6. scene:getDefaultCamera():setOrthoSize(0.0, 4.0)
  7. -- load the playing field from the entity file and add it to the scene
  8. field = SceneEntityInstance(scene, "Resources/field.entity")
  9. scene:addChild(field)
  10. -- get a handle to the player paddles and ball in the scene file and begin tracking collision
  11. ball = field:getEntityById("ball", true)
  12. scene:trackCollisionChild(ball, PhysicsScene2DEntity.ENTITY_RECT)
  13. p1 = field:getEntityById("p1", true)
  14. scene:trackCollisionChild(p1, PhysicsScene2DEntity.ENTITY_RECT)
  15. p2 = field:getEntityById("p2", true)
  16. scene:trackCollisionChild(p2, PhysicsScene2DEntity.ENTITY_RECT)
  17. topWall = field:getEntityById("topWall", true)
  18. scene:trackCollisionChild(topWall, PhysicsScene2DEntity.ENTITY_RECT)
  19. bottomWall = field:getEntityById("bottomWall", true)
  20. scene:trackCollisionChild(bottomWall, PhysicsScene2DEntity.ENTITY_RECT)
  21. --load sounds
  22. hitSound = Sound("Resources/hit.wav")
  23. scoreSound = Sound("Resources/score.wav")
  24. ballSpeed = 4.0
  25. ballDirection = Vector2(1.0, 0.0)
  26. -- initialize scores and get references to the player score labels on the field
  27. p1scoreLabel = cast(field:getEntityById("p1ScoreLabel", true), SceneLabel)
  28. p2scoreLabel = cast(field:getEntityById("p2ScoreLabel", true), SceneLabel)
  29. p1Score = 0
  30. p2Score = 0
  31. function onCollision(t, event)
  32. -- we need to cast the event to PhysicsScreenEvent because this is a PhysicsScreen event
  33. physicsEvent = cast(event, PhysicsScene2DEvent)
  34. -- check if the colliding entity is the ball
  35. if physicsEvent.entity1 == ball then
  36. -- if colliding with player 1 or player 2 paddle
  37. if physicsEvent.entity2 == p1 or physicsEvent.entity2 == p2 then
  38. -- reverse the horizontal direction
  39. ballDirection.x = ballDirection.x * -1
  40. -- adjust the vertical direction based on where on the paddle it hit
  41. ballDirection.y = (ball:getPosition().y - physicsEvent:getSecondEntity():getPosition().y)/1.0
  42. if ballDirection.y > 1.0 then ballDirection.y = 1.0 end
  43. if ballDirection.y < -1.0 then ballDirection.y = -1.0 end
  44. else
  45. -- if collliding with the walls, simply reverse the vertical direction
  46. ballDirection.y = ballDirection.y * -1
  47. end
  48. -- play the hit sound
  49. hitSound:Play()
  50. end
  51. end
  52. -- add a collision listener to the Physics Screen
  53. -- onCollision will now be called every time there is a collion between
  54. -- entities that we are tracking
  55. scene:addEventListener(nil, onCollision, PhysicsScene2DEvent.EVENT_NEW_SHAPE_COLLISION)
  56. -- Update is called automatically every frame
  57. function Update(elapsed)
  58. -- check player 1 input
  59. if Services.Input:getKeyState(KEY_a) == true then
  60. p1:setPositionY( p1:getPosition().y + (3.0 * elapsed))
  61. elseif Services.Input:getKeyState(KEY_z) == true then
  62. p1:setPositionY( p1:getPosition().y - (3.0 * elapsed))
  63. end
  64. -- check player 2 input
  65. if Services.Input:getKeyState(KEY_UP) == true then
  66. p2:setPositionY( p2:getPosition().y + (3.0 * elapsed))
  67. elseif Services.Input:getKeyState(KEY_DOWN) == true then
  68. p2:setPositionY( p2:getPosition().y - (3.0 * elapsed))
  69. end
  70. -- limit the paddle positions so they don't go offscreen
  71. if p1:getPosition().y < -1.3 then p1:setPositionY(-1.3) end
  72. if p1:getPosition().y > 1.3 then p1:setPositionY(1.3) end
  73. if p2:getPosition().y < -1.3 then p2:setPositionY(-1.3) end
  74. if p2:getPosition().y > 1.3 then p2:setPositionY(1.3) end
  75. -- update the ball position
  76. ball:setPositionX(ball:getPosition().x + (ballDirection.x * ballSpeed * elapsed))
  77. ball:setPositionY( ball:getPosition().y + (ballDirection.y * ballSpeed * elapsed))
  78. -- check if the ball beyond player 1's paddle and increment player 2's score
  79. if ball:getPosition().x < -3 then
  80. ball:setPosition(0.0, 0.0)
  81. ballDirection.x = 1.0
  82. ballDirection.y = 0.0
  83. scoreSound:Play()
  84. p2Score = p2Score + 1
  85. p2scoreLabel:setText(""..p2Score)
  86. end
  87. -- check if the ball beyond player 2's paddle and increment player 1's score
  88. if ball:getPosition().x > 3 then
  89. ball:setPosition(0.0, 0.0)
  90. ball:setPositionY(0)
  91. ballDirection.x = -1.0
  92. ballDirection.y = 0.0
  93. scoreSound:Play()
  94. p1Score = p1Score + 1
  95. p1scoreLabel:setText(""..p1Score)
  96. end
  97. end