Guest User

Untitled

a guest
Feb 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. display.setStatusBar( display.HiddenStatusBar )
  2. local physics = require("physics")
  3.  
  4. physics.start(true)
  5. physics.setDrawMode("hybrid")
  6.  
  7. local w,h = display.contentWidth, display.contentHeight
  8.  
  9. local moveGroup = display.newGroup()
  10.  
  11. -- Ground
  12. local bump = display.newImage("ramp.png")
  13. bump:setReferencePoint(display.CenterDownReferencePoint)
  14. bump.x = w-240
  15. bump.y = h-150
  16. moveGroup:insert(bump)
  17.  
  18. local ground1 = display.newRect(moveGroup, 0, h-120, w, 20)
  19. ground1:setFillColor(255,0,0)
  20.  
  21. physics.addBody(ground1, "static", {friction = 0.5, bounce = 0.5, density=2.0})
  22. physics.addBody(bump, "dynamic", {friction = 0.5, bounce = 0.5, density=2.0, shape={-40, 20, 40, -20, 40, 20}})
  23. -- Car
  24. local frame = display.newRect(50, h-200, 60, 40)
  25. frame:setFillColor(0,128,0)
  26. local leftWheel = display.newCircle(50, h-160, 20)
  27. leftWheel:setFillColor(0,255,0)
  28. local rightWheel = display.newCircle(110, h-160, 20)
  29. rightWheel:setFillColor(0,255,0)
  30.  
  31. physics.addBody(leftWheel, "dynamic", {friction = 2, bounce = 0.4, radius = 20})
  32. physics.addBody(rightWheel, "dynamic", {friction = 2, bounce = 0.4, radius = 20})
  33. physics.addBody(frame, "dynamic", {friction = 0.5, bounce = 0.1})
  34.  
  35. local jointLeftWheelFrame = physics.newJoint("pivot", leftWheel, frame, 50, h-160)
  36. local jointRightWheelFrame = physics.newJoint("pivot", rightWheel, frame, 110, h-160)
  37.  
  38. -- Controls
  39. --======================
  40. -- throttle button
  41. local applyingForce = 0
  42.  
  43. local buttonTouched = function(event)
  44.     if (event.target.name == "throttle") then
  45.         if (event.phase == "began") then
  46.             applyingForce = 1
  47.         elseif (event.phase == "ended") then
  48.             applyingForce = 0
  49.         end
  50.     elseif(event.target.name == "2") then
  51.         if (event.phase == "began") then
  52.             applyingForce = -1
  53.         elseif (event.phase == "ended") then
  54.             applyingForce = 0
  55.         end
  56.     elseif(event.target.name == "3") then
  57.         if (event.phase == "began") then
  58.             frame:applyForce(5,0,frame.x, frame.y)
  59.         end
  60.     end
  61. end
  62.  
  63. local controlThrottle = display.newRoundedRect(10, 230, 30, 70, 10)
  64. controlThrottle.strokeWidth = 2
  65. controlThrottle:setFillColor(0,0,200)
  66. controlThrottle:setStrokeColor(0,0,150)
  67. controlThrottle.name = "throttle"
  68. controlThrottle:addEventListener("touch", buttonTouched)
  69.  
  70. local controlThrottleLabel = display.newText("<", 20, 250, nil, 20)
  71.  
  72. local control2 = display.newRoundedRect(40, 230, 30, 70, 10)
  73. control2.strokeWidth = 2
  74. control2:setFillColor(0,0,200)
  75. control2:setStrokeColor(0,0,150)
  76. control2.name = "2"
  77. control2:addEventListener("touch", buttonTouched)
  78.  
  79. local control2Label = display.newText(">", 45, 250, nil, 20)
  80.  
  81. local control3 = display.newRoundedRect(70, 230, 30, 70, 10)
  82. control3.strokeWidth = 2
  83. control3:setFillColor(0,0,200)
  84. control3:setStrokeColor(0,0,150)
  85. control3.name = "3"
  86. control3:addEventListener("touch", buttonTouched)
  87.  
  88. local control3Label = display.newText("^", 80, 250, nil, 20)
  89.  
  90. local gameLoop = function(event)
  91.     moveGroup.x = moveGroup.x - 5 * applyingForce
  92.     --ground1.x = ground1.x - 5 * applyingForce
  93. end
  94.  
  95. Runtime:addEventListener("enterFrame", gameLoop)
  96.  
  97. --physics.stop()
Add Comment
Please, Sign In to add comment