Main.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ------------------------------------------------
  2. -- Polycode Pong example by Ivan Safrin, 2013
  3. ------------------------------------------------
  4. -- create a new Screen and set its height to 480
  5. screen = PhysicsScreen(1.0, 30)
  6. screen:setNormalizedCoordinates(true, 480)
  7. -- load the playing field from the entity file and add it to the screen
  8. field = ScreenEntityInstance("Resources/field.entity2d")
  9. screen:addChild(field)
  10. -- get a handle to the player paddles and ball in the screen file and begin tracking collision
  11. ball = field:getScreenEntityById("ball", true)
  12. screen:trackCollisionChild(ball, PhysicsScreenEntity.ENTITY_RECT)
  13. p1 = field:getScreenEntityById("p1", true)
  14. screen:trackCollisionChild(p1, PhysicsScreenEntity.ENTITY_RECT)
  15. p2 = field:getScreenEntityById("p2", true)
  16. screen:trackCollisionChild(p2, PhysicsScreenEntity.ENTITY_RECT)
  17. topWall = field:getScreenEntityById("topWall", true)
  18. screen:trackCollisionChild(topWall, PhysicsScreenEntity.ENTITY_RECT)
  19. bottomWall = field:getScreenEntityById("bottomWall", true)
  20. screen:trackCollisionChild(bottomWall, PhysicsScreenEntity.ENTITY_RECT)
  21. --load sounds
  22. hitSound = Sound("Resources/hit.wav")
  23. scoreSound = Sound("Resources/score.wav")
  24. ballSpeed = 400.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:getScreenEntityById("p1scoreLabel", true), ScreenLabel)
  28. p2scoreLabel = cast(field:getScreenEntityById("p2scoreLabel", true), ScreenLabel)
  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, PhysicsScreenEvent)
  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.position.y - physicsEvent:getSecondEntity().position.y)/80.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. screen:addEventListener(nil, onCollision, PhysicsScreenEvent.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.position.y = p1.position.y - (300 * elapsed)
  61. elseif Services.Input:getKeyState(KEY_z) == true then
  62. p1.position.y = p1.position.y + (300 * elapsed)
  63. end
  64. -- check player 2 input
  65. if Services.Input:getKeyState(KEY_UP) == true then
  66. p2.position.y = p2.position.y - (300 * elapsed)
  67. elseif Services.Input:getKeyState(KEY_DOWN) == true then
  68. p2.position.y = p2.position.y + (300 * elapsed)
  69. end
  70. -- limit the paddle positions so they don't go offscreen
  71. if p1.position.y < -180 then p1.position.y = -180 end
  72. if p1.position.y > 180 then p1.position.y = 180 end
  73. if p2.position.y < -180 then p2.position.y = -180 end
  74. if p2.position.y > 180 then p2.position.y = 180 end
  75. -- update the ball position
  76. ball.position.x = ball.position.x + (ballDirection.x * ballSpeed * elapsed)
  77. ball.position.y = ball.position.y + (ballDirection.y * ballSpeed * elapsed)
  78. -- check if the ball beyond player 1's paddle and increment player 2's score
  79. if ball.position.x < -328 then
  80. ball.position.x = -200
  81. ball.position.y = 0
  82. ballDirection.x = 1.0
  83. ballDirection.y = 0.0
  84. scoreSound:Play()
  85. p2Score = p2Score + 1
  86. p2scoreLabel:setText(""..p2Score)
  87. end
  88. -- check if the ball beyond player 2's paddle and increment player 1's score
  89. if ball.position.x > 328 then
  90. ball.position.x = 200
  91. ball.position.y = 0
  92. ballDirection.x = -1.0
  93. ballDirection.y = 0.0
  94. scoreSound:Play()
  95. p1Score = p1Score + 1
  96. p1scoreLabel:setText(""..p1Score)
  97. end
  98. end