Atlas__

GAME.LUA

Jul 3rd, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. local director = require("director")
  2. local physics = require("physics")
  3.  
  4. physics.start()
  5.  
  6. local playGame = true;
  7.  
  8. local localGroup
  9. localGroup = display.newGroup()
  10.  
  11. local background = display.newImage("clouds.png")
  12. background.x = display.contentWidth/2; background.y = display.contentHeight/2;
  13. localGroup:insert(background)
  14.  
  15. local score = display.newText( "Score: 0", 10, 25, nil, 24 )
  16. score:setTextColor( 255,255,255 )
  17. localGroup:insert(score)
  18.  
  19. scoreSet = 0
  20.  
  21. local floor = display.newRect(10,25,600,50)
  22. floor.x = display.contentWidth/2; floor.y = 368
  23. floor.name = "floor" -- This sets the name of the floor to later be used in collision functions
  24. localGroup:insert(floor)
  25. physics.addBody(floor, "static")
  26.  
  27. local paddle = display.newRect(0,0, 150, 75)
  28. paddle.x = display.contentWidth/2; paddle.y = 325
  29. paddle.name = "paddle" -- This sets the name of the paddle to later be used in collision functions
  30. localGroup:insert(paddle)
  31. physics.addBody(paddle, "kinematic")
  32.  
  33. local function onCrateCollision( self, event ) -- This is called when the crate collides with anything
  34. if ( event.phase == "began" ) then -- This is here to make sure its only called once at the start of the collision to avoid errors
  35. if ( event.other.name == "floor" or event.other.name == "paddle" ) then -- This checks to make sure its either the floor, or a paddle the crate is colliding with
  36. if (playGame == true) then
  37. hitSound = audio.loadSound("BEEPMULT.wav")
  38. audio.play(hitSound)
  39. self:removeSelf() -- Delete the crate
  40. self = nil
  41. end
  42. end
  43. end
  44. end
  45.  
  46. local function onPickupCollision( self, event )
  47. if ( event.phase == "began" ) then
  48. if ( event.other.name == "paddle" or event.other.name == "floor" ) then
  49. if playGame == true then
  50. pickUpSound = audio.loadSound("BEEPFM.wav")
  51. audio.play(pickUpSound)
  52. self:removeSelf()
  53. end
  54. end
  55. end
  56. end
  57.  
  58. function newCrate()
  59. j = display.newImage("crate.png");
  60. j.x = 30 + math.random(400)
  61. j.y = -100
  62. print("spawn")
  63. localGroup:insert(j)
  64. physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )
  65. j.collision = onCrateCollision -- This sets it so the onCrateCollision function above is attachesd to the crate object ( j )
  66. j.name = "crate" -- This line names the crate so we can identify it later in collisions
  67. j:addEventListener( "collision", j ) -- j the object ( crate )now has a event listener for when it collised. We set j.collision to onCrateCollision above so we can just call j as the last object here
  68.  
  69. end
  70.  
  71. function newPickup()
  72. rand = math.random(1000)
  73.  
  74. if (rand <100 ) then
  75. b = display.newImage("star.png");
  76. b.x = 30 + math.random(400)
  77. b.y = -100
  78. b:rotate(math.random(360))
  79. localGroup:insert(b)
  80. physics.addBody(b, { density = 0.9, friction = 0.3, bounce = 0.3, radius = 20} )
  81. b.collision = onPickupCollision
  82. b.name = "star"
  83. b:addEventListener( "collision", b )
  84. end
  85. end
  86.  
  87.  
  88. local function drag(event) -- The following function is used for dragging the paddle around and updating its position
  89.  
  90. local t = event.target
  91.  
  92. local phase = event.phase
  93. if phase == "began" then
  94. local parent = t.parent
  95. parent:insert ( t )
  96. display.getCurrentStage():setFocus( t )
  97.  
  98. t.isFocus = true;
  99. t.x0 = event.x - t.x
  100.  
  101. elseif t.isFocus then
  102.  
  103. if phase == "moved" then
  104. t.x = event.x - t.x0
  105.  
  106. elseif phase == "ended" or phase == "cancelled" then
  107. display.getCurrentStage():setFocus( nil )
  108. t.isFocus = false
  109. end
  110. end
  111. end
  112.  
  113.  
  114. -- Event Listeners --
  115. paddle:addEventListener( "touch", drag ) -- This will call the drag function when the paddle is touched
  116. floor:addEventListener( "collision", floor ) -- this will call the floor:collision function below if anything colides with the floor
  117. paddle:addEventListener( "collision", paddle ) -- this will call the paddle:collision function below if anything colides with the paddle
  118.  
  119. local dropCrates = timer.performWithDelay( 200, newCrate, 0 ) -- Sets the timer to drop crates
  120. local dropPickup = timer.performWithDelay ( 500, newPickup, 0 )
  121.  
  122. function onAlert(event)
  123. if ( event.action == "clicked" ) then
  124. local i = event.index
  125. if i == 2 then
  126. print("yes!")
  127. director:changeScene("main", "crossfade")
  128. elseif i == 1 then
  129. print("no!")
  130. director:changeScene("game", "crossfade")
  131. end
  132. end
  133. end
  134.  
  135. -- Check for Collisions --
  136. function paddle:collision (event) -- This function is called whenever any object collides with the paddle
  137. if event.other.name == "crate" then -- This checks to make sure the object collided is a crate (the below is stuff u added)
  138. paddle:removeEventListener("touch", drag)
  139. paddle:removeEventListener("collision", paddle )
  140. floor:removeEventListener("collision", floor)
  141. local alert = native.showAlert( "OH NO!", "YOU LOST, SORRY!",
  142. { "Retry", "Menu" }, onAlert)
  143. end
  144.  
  145. if event.other.name == "star" then
  146. scoreSet = scoreSet + 5
  147. score.text = "Score: " .. scoreSet
  148. end
  149. end
  150.  
  151. function floor:collision (event) -- This function is called whenever any object collides with the floor
  152. if ( event.other.name == "crate" ) then -- This checks to make sure the object collided is a crate . If so we then add to the score and update the score text
  153. print("yo")
  154. scoreSet = scoreSet +1 -- Add one to the current score
  155. score.text = "Score: " .. scoreSet -- Display the updated score
  156. end
  157. end
  158.  
  159.  
  160.  
  161. function new()
  162. return localGroup
  163. end
Advertisement
Add Comment
Please, Sign In to add comment