Advertisement
Descaii

2D Game

Mar 19th, 2014
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.30 KB | None | 0 0
  1. --http://www49.zippyshare.com/v/38178937/file.html
  2.  
  3. Player = game.Players.LocalPlayer
  4. if Player:FindFirstChild("IsPlaying") then
  5.     CPos = Player.IsPlaying
  6. else
  7.     CPos = Instance.new("Vector3Value",Player)
  8. end
  9. if Player.Character then
  10.     Player.Character:Destroy()
  11. end
  12. CHealth = Instance.new("Vector3Value",CPos)
  13. CHealth.Name = "Health"
  14. Remote = Instance.new("BindableFunction",CPos)
  15. Remote.OnInvoke = function(Source)
  16.     return loadstring(Source)()
  17. end
  18. Chatted = Instance.new("RemoteEvent",CPos)
  19. Chatted.Name = "Chatted"
  20. Remote.Name = "Remote"
  21. CPos.Name = "IsPlaying"
  22. Mouse = Player:GetMouse()
  23. PlayerGui = Player:WaitForChild("PlayerGui")
  24. Screen = Instance.new("ScreenGui",PlayerGui)
  25. Screen.Name = "2DGame"
  26. Background = Instance.new("ImageLabel",Screen)
  27. BackgroundID = 161283188
  28. Background.Image = "http://www.roblox.com/asset/?id="..BackgroundID-1
  29. Background.Size = UDim2.new(0,1000,0,600)
  30. Background.Position = UDim2.new(0.5,-500,0.5,-300)
  31. Position = Vector2.new(480,0)
  32. Velocity = Vector2.new(0,0)
  33. Players = {}
  34. JumpHeight = 30
  35. WalkSpeed = 2
  36. Decline = 5
  37. Health = 100
  38. MaxHealth = 100
  39. Count = 0
  40. GlobalObjects = {}
  41. Worlds = {
  42.     --{Height = 400,ID = 161283188,Size = Vector2.new(1000,600),Objects={}},
  43.     --{Height = 400,ID = 161397987,Size = Vector2.new(1000,600),Objects={}},
  44.     --{Height = 400,ID = 161402869,Size = Vector2.new(1000,600),Objects={}},
  45.     --{Height = 400,ID = 161413779,Size = Vector2.new(1000,600),Objects={}}
  46. }
  47. function NewWorld(Name,Height,ID,TSize)
  48.     local A = {}
  49.     A.Height = Height
  50.     A.ID = ID
  51.     A.Size = TSize
  52.     A.Objects = {}
  53.     A.Name = Name
  54.     function A:NewObject(LSize,LPosition)
  55.         local B = Instance.new("Frame",Background)
  56.         B.Size = UDim2.new(0,LSize.X,0,LSize.Y)
  57.         B.Position = UDim2.new(0,LPosition.X,0,LPosition.Y)
  58.         local C = {}
  59.         C.B = B
  60.         C.World = A
  61.         function C:IsAbove(V)
  62.             local X = V.X
  63.             local Y = V.Y
  64.             if Y < B.Position.Y.Offset+LSize.Y/2 and X > B.Position.X.Offset and X < B.Position.X.Offset+LSize.X then
  65.                 return true
  66.             else
  67.                 return nil
  68.             end
  69.         end
  70.         function C:IsBelow(V)
  71.             local X = V.X
  72.             local Y = V.Y
  73.             if Y > B.Position.Y.Offset+LSize.Y/2 and X > B.Position.X.Offset and X < B.Position.X.Offset+LSize.X then
  74.                 return true
  75.             else
  76.                 return nil
  77.             end
  78.         end
  79.         table.insert(A.Objects,C)
  80.         table.insert(GlobalObjects,Y)
  81.     end
  82.     table.insert(Worlds,A)
  83.     return A
  84. end
  85. First = NewWorld("Grasslands(Earth)",500,161283188,Vector2.new(1000,600))
  86. First:NewObject(Vector2.new(100,50),Vector2.new(0,400))
  87. First:NewObject(Vector2.new(100,50),Vector2.new(150,350))
  88. NewWorld("Terrain(Earth)",500,161397987,Vector2.new(1000,600))
  89. NewWorld("Cloudy(Earth)",500,161402869,Vector2.new(1000,600))
  90. NewWorld("Maket(Planet)",500,161413779,Vector2.new(1000,600))
  91. function ChangeToWorld(Num)
  92.     if Worlds[Num] then
  93.         for i,v in pairs(RealWorld.Objects) do
  94.             v.B.Visible = false
  95.         end
  96.         CurrentWorld = Num
  97.     end
  98.     local Length
  99.     if tonumber(Num) then
  100.         Length = 1
  101.     else
  102.         Length = #Num
  103.     end
  104.     for i,v in pairs(Worlds) do
  105.         if string.sub(v.Name,1,Length) == Num then
  106.             for i,v in pairs(RealWorld.Objects) do
  107.                 v.B.Visible = false
  108.             end
  109.             CurrentWorld = i
  110.         end
  111.     end
  112. end
  113. function ChangeWorld(up)
  114.     if up then
  115.         ChangeToWorld(CurrentWorld+1)
  116.         Position = Vector2.new(10,Position.Y)
  117.     else
  118.         ChangeToWorld(CurrentWorld-1)
  119.         Position = Vector2.new(Worlds[CurrentWorld].Size.X-50,Position.Y)
  120.     end
  121. end
  122. function GetOffsetByRotation(Rotation,Position,Offset)
  123. end
  124. CurrentWorld = 1
  125. RealWorld = Worlds[CurrentWorld]
  126. PlayerUI = Instance.new("Frame",Background)
  127. PlayerUI.Size = UDim2.new(0,40,0,100)
  128. PlayerUI.Position = UDim2.new(0,Position.X,0,Position.Y)
  129. Head = Instance.new("Frame",Background)
  130. Head.Size = UDim2.new(0,40,0,30)
  131. Eye = Instance.new("Frame",Head)
  132. Eye.BackgroundColor3 = Color3.new(0,0,0)
  133. Eye.Position = UDim2.new(0,5,0,10)
  134. Eye.Size = UDim2.new(0,10,0,10)
  135. HealthUI = Instance.new("Frame",Background)
  136. HealthUI.Size = UDim2.new(0,80,0,10)
  137. HealthUI.BackgroundColor3 = Color3.new(255,0,0)
  138. HealthUI.ZIndex = 5
  139. local HealthUIBK = Instance.new("Frame",HealthUI)
  140. HealthUIBK.Size = UDim2.new(1,0,1,0)
  141. HealthUIBK.BackgroundColor3 = Color3.new(0,255,0)
  142. HealthUIBK.ZIndex = 6
  143. local Control = Instance.new("TextLabel",Background)
  144. Control.Size = UDim2.new(0,500,0,20)
  145. Control.Text = "W = jump, A = go left, D = go right"
  146. Control.TextColor3 = Color3.new(0,0,0)
  147. Control.BackgroundTransparency = 1
  148. Control.TextScaled = true
  149. Control.Position = UDim2.new(0.5,-250,1,-20)
  150. Control.ZIndex = 10
  151. GoForward = Instance.new("TextButton",Background)
  152. GoForward.Size = UDim2.new(0,40,0,40)
  153. GoForward.Text = ">"
  154. GoForward.TextScaled = true
  155. GoForward.BackgroundColor3 = Color3.new(255,255,0)
  156. GoForward.Position = UDim2.new(1,-50,0.5,-20)
  157. GoForward.ZIndex = 10
  158. GoBackward = Instance.new("TextButton",Background)
  159. GoBackward.Size = UDim2.new(0,40,0,40)
  160. GoBackward.Text = "<"
  161. GoBackward.TextScaled = true
  162. GoBackward.BackgroundColor3 = Color3.new(255,255,0)
  163. GoBackward.Position = UDim2.new(0,10,0.5,-20)
  164. GoBackward.ZIndex = 10
  165. GoForward.MouseButton1Click:connect(function()
  166.     if Position.X > RealWorld.Size.X-200 then
  167.         ChangeWorld(true)
  168.     end
  169. end)
  170. GoBackward.MouseButton1Click:connect(function()
  171.     if Position.X < 200 then
  172.         ChangeWorld(false)
  173.     end
  174. end)
  175. Title = Instance.new("TextLabel",Background)
  176. Title.Size = UDim2.new(0,200,0,50)
  177. Title.Text = "World"
  178. Title.TextColor3 = Color3.new(0.5,0.5,0.5)
  179. Title.TextWrapped = false
  180. Title.BackgroundTransparency = 1
  181. Title.FontSize = "Size14"
  182. Title.Position = UDim2.new(0.5,-100,0,0)
  183. HD = false
  184. FD = false
  185. Jump = false
  186. mc = math.ceil
  187. Chats = {}
  188. Commands = {}
  189. function ChatTxt(chat)
  190.     local Chat = {}
  191.     Chat.Pos = 0
  192.     Chat.Gui = Instance.new("TextLabel",Background)
  193.     Chat.Gui.Text = chat
  194.     Chat.Age = 0
  195.     Chat.Gui.Size = UDim2.new(0,Chat.Gui.TextBounds.X+30,0,Chat.Gui.TextBounds.Y+10)
  196.     Chat.Gui.BackgroundColor3 = Color3.new(1,1,1)
  197.     Chat.Gui.BackgroundTransparency = 0.5
  198.     Chat.Gui.BorderSizePixel = 0
  199.     for i,v in pairs(Chats) do
  200.         v.Pos = v.Pos +Chat.Gui.Size.Y.Offset+10
  201.     end
  202.     table.insert(Chats,Chat)
  203.     if #Chats > 3 then
  204.         Chats[1].Gui:Destroy()
  205.         table.remove(Chats,1)
  206.     end
  207.     for i,v in pairs(Commands) do
  208.         if (string.sub(chat,1,#v.Cmd)) == v.Cmd then
  209.             v.func(string.sub(chat,#v.Cmd+1))
  210.         end
  211.     end
  212. end
  213. Commands.Tp = {Cmd = "/tp ",func = function(n)
  214.     for i,v in pairs(Players) do
  215.         if string.sub(v.Player.Name,1,#n) == n then
  216.             ChangeToWorld(v.World)
  217.             ChatTxt("Got tp to "..v.Player.Name)
  218.             Position = Vector2.new(v.Pos.Value.X,v.Pos.Value.Y)
  219.         end
  220.     end
  221. end}
  222. Commands.Warp = {Cmd = "/warp ",func = function(n)
  223.     ChangeToWorld(n)
  224. end}
  225. Player.Chatted:connect(function(chat)
  226.     ChatTxt(chat)
  227.     for i,v in pairs(game.Players:GetPlayers()) do
  228.         Chatted:FireServer(chat)
  229.     end
  230. end)
  231. function Point(UD1,UD2)
  232.     local X=UD1.X.Offset-UD2.X.Offset
  233.     local Y=UD1.Y.Offset-UD2.Y.Offset
  234.     local Hyp=math.sqrt(X^2+Y^2)
  235.     local AS=math.atan2(Y,X)
  236.     return 360*(AS/(math.pi*2))
  237. end
  238. --0.5,0.5,0.5
  239. function NewPlayer(Pos)
  240.     local NHealth = Pos:findFirstChild("Health")
  241.     local NRemote = Pos:findFirstChild("Remote")
  242.     local NChatted = Pos:findFirstChild("Chatted")
  243.     if NHealth and NRemote and NChatted and Pos.Parent and Pos.Parent:IsA("Player") then
  244.         local NPlayerInstance = Pos.Parent
  245.         local NPlayer = Instance.new("Frame",Background)
  246.         NPlayer.Size = UDim2.new(0,40,0,100)
  247.         NPlayer.Position = UDim2.new(0,Position.X,0,Position.Y-100)
  248.         local NHead = Instance.new("Frame",Background)
  249.         NHead.Size = UDim2.new(0,40,0,30)
  250.         local NEye = Instance.new("Frame",NHead)
  251.         NEye.BackgroundColor3 = Color3.new(0,0,0)
  252.         NEye.Position = UDim2.new(0,5,0,10)
  253.         NEye.Size = UDim2.new(0,10,0,10)
  254.         local C = Instance.new("TextLabel",Background)
  255.         C.TextColor3 = Color3.new(1,1,1)
  256.         C.TextStrokeColor3 = Color3.new(0,0,0)
  257.         C.TextStrokeTransparency = 0.5
  258.         C.Text = tostring(Pos.Parent)
  259.         C.ZIndex = 6
  260.         C.Size = UDim2.new(0,C.TextBounds.X,0,C.TextBounds.Y)
  261.         C.BackgroundTransparency = 1
  262.         local D = {}
  263.         D.F = NPlayer
  264.         D.Pos = Pos
  265.         D.H = NHead
  266.         D.C = C
  267.         D.World = NHealth.Value.Z
  268.         D.Chats = {}
  269.         for i,v in pairs(Pos:GetChildren()) do
  270.             D[v.Name]=v
  271.         end
  272.         D.ChatTxt = function(chat)
  273.             if not D.Deleted then
  274.                 local Chat = {}
  275.                 Chat.Pos = 0
  276.                 Chat.Gui = Instance.new("TextLabel",Background)
  277.                 Chat.Gui.Text = chat
  278.                 Chat.Age = 0
  279.                 Chat.Gui.Size = UDim2.new(0,Chat.Gui.TextBounds.X+30,0,Chat.Gui.TextBounds.Y+10)
  280.                 Chat.Gui.BackgroundColor3 = Color3.new(1,1,1)
  281.                 Chat.Gui.BackgroundTransparency = 0.5
  282.                 Chat.Gui.BorderSizePixel = 0
  283.                 for i,v in pairs(D.Chats) do
  284.                     v.Pos = v.Pos +Chat.Gui.Size.Y.Offset+10
  285.                 end
  286.                 table.insert(D.Chats,Chat)
  287.                 if #D.Chats > 3 then
  288.                     D.Chats[1].Gui:Destroy()
  289.                     table.remove(D.Chats,1)
  290.                 end
  291.             end
  292.         end
  293.         D.Player = NPlayerInstance
  294.         D.Deleted = false
  295.         function D:Destroy()
  296.             if not D.Deleted then
  297.                 for i,v in pairs(Players) do
  298.                     if v == D then
  299.                         table.remove(Players,i)
  300.                         v.F:Destroy()
  301.                         v.C:Destroy()
  302.                         v.H:Destroy()
  303.                         v.Deleted = true
  304.                         for i,v in pairs(v.Chats) do
  305.                             v.Gui:Destroy()
  306.                         end
  307.                     end
  308.                 end
  309.             end
  310.         end
  311.         NPlayerInstance.CharacterAdded:connect(function()
  312.             D:Destroy()
  313.             for i,v in pairs(NPlayerInstance:GetChildren()) do
  314.                 if v.Name == "IsPlaying" then
  315.                     v:Destroy()
  316.                 end
  317.             end
  318.         end)
  319.         D.Player.Chatted:connect(function(chat)
  320.             D.ChatTxt(chat)
  321.         end)
  322.         table.insert(Players,D)
  323.         --print("New Player: " ..tostring(Pos.Parent),Pos.Value)
  324.     end
  325. end
  326. function FindPlayer(Name)
  327.     local FoundName = nil
  328.     for i,v in pairs(Players) do
  329.         if v.Pos.Parent then
  330.             if v.Pos.Parent.Name == Name then
  331.                 FoundName = true
  332.             end
  333.         end
  334.     end
  335.     return FoundName
  336. end
  337. Mouse.KeyDown:connect(function(k)
  338.     local k = string.lower(k)
  339.     if k == "a" then
  340.         FD = true
  341.     end
  342.     if k == "d" then
  343.         HD = true
  344.     end
  345.     if k == "w" then
  346.         Jump = true
  347.     end
  348. end)
  349. Mouse.KeyUp:connect(function(k)
  350.     local k = string.lower(k)
  351.     if k == "a" then
  352.         FD = false
  353.     end
  354.     if k == "d" then
  355.         HD = false
  356.     end
  357.     if k == "w" then
  358.         Jump = false
  359.     end
  360. end)
  361. coroutine.resume(coroutine.create(function()
  362.     while wait(1) do
  363.         for i,d in pairs(Players) do
  364.             for i,v in pairs(d.Chats) do
  365.                 v.Age = v.Age +1
  366.                 if v.Age > 5 then
  367.                     v.Gui:Destroy()
  368.                     table.remove(d.Chats,i)
  369.                 end
  370.             end
  371.         end
  372.         for i,v in pairs(Chats) do
  373.             v.Age = v.Age +1
  374.             if v.Age > 5 then
  375.                 v.Gui:Destroy()
  376.                 table.remove(Chats,i)
  377.             end
  378.         end
  379.     end
  380. end))
  381. while wait() do
  382.     Position = Position +Velocity
  383.     RealWorld = Worlds[CurrentWorld]
  384.     if Worlds[CurrentWorld+1] and Position.X > RealWorld.Size.X-200 then
  385.         GoForward.Visible = true
  386.     else
  387.         GoForward.Visible = false
  388.     end
  389.     if Worlds[CurrentWorld-1] and Position.X < 200 then
  390.         GoBackward.Visible = true
  391.     else
  392.         GoBackward.Visible = false
  393.     end
  394.     Count = Count +1
  395.     --print(Worlds)
  396.     --print(RealWorld)
  397.     local HP = Head.AbsolutePosition
  398.     local Height = RealWorld.Height
  399.     Background.Size = UDim2.new(0,RealWorld.Size.X,0,RealWorld.Size.Y)
  400.     Title.Text = RealWorld.Name
  401.     local FoundObj = false
  402.     for i,v in pairs(RealWorld.Objects) do
  403.         v.B.Visible = true
  404.         if v:IsAbove(Position) then
  405.             if Position.Y > v.B.Position.Y.Offset then
  406.                 Position = Vector2.new(Position.X,v.B.Position.Y.Offset)
  407.                 Velocity = Velocity -Vector2.new(0,(Position.X-v.B.Position.X.Offset)*0.1)
  408.             end
  409.             --print(v:IsAbove(Position-Vector2.new(0,100)))
  410.             Height = v.B.Position.Y.Offset
  411.             --print("Above",Height)
  412.             --print(math.abs(math.ceil(Velocity.X)),math.abs(math.ceil(Velocity.Y)))
  413.             FoundObj = true
  414.         end
  415.     end
  416.     if not FoundObj then
  417.         Height = RealWorld.Height
  418.     end
  419.     PlayerUI.Position = UDim2.new(0,Position.X,0,Position.Y-100)   
  420.     Head.Position = PlayerUI.Position-UDim2.new(0,0,0,30)
  421.     Head.Rotation = Point(UDim2.new(0,HP.X,0,HP.Y),UDim2.new(0,Mouse.X,0,Mouse.Y))
  422.     Background.Position = UDim2.new(0.5,-RealWorld.Size.X/2,0.5,-RealWorld.Size.Y/2)
  423.     if Background.Image ~= "http://www.roblox.com/asset/?id="..RealWorld.ID-1 then
  424.         Background.Image = "http://www.roblox.com/asset/?id="..RealWorld.ID-1
  425.     end
  426.     CPos.Value = Vector3.new(Position.X,Position.Y,Head.Rotation)
  427.     if Position.Y < Height then
  428.         --print(Position.Y < Height)
  429.         Velocity = Vector2.new(Velocity.X,Velocity.Y+Decline)
  430.     else
  431.         --print(Position.Y < Height)
  432.         Velocity = Vector2.new(Velocity.X,-Velocity.Y*0.5)
  433.         if Jump then
  434.             Velocity = Velocity-Vector2.new(0,JumpHeight)
  435.         end
  436.     end
  437.     --print(mc(Position.X),mc(Position.Y),mc(Velocity.X),mc(Velocity.Y))
  438.     if Position.X < 0 then
  439.         Position = Vector2.new(0,Position.Y)
  440.         Velocity = Vector2.new(-Velocity.X*0.5,Velocity.Y)
  441.     end
  442.     if Position.X > 1000-40 then
  443.         Position = Vector2.new(1000-40,Position.Y)
  444.         Velocity = Vector2.new(-Velocity.X*0.5,Velocity.Y)
  445.     end
  446.     if HD then
  447.         Velocity = Velocity +Vector2.new(WalkSpeed,0)
  448.     end
  449.     if FD then
  450.         Velocity = Velocity -Vector2.new(WalkSpeed,0)
  451.     end
  452.     Velocity = Velocity*0.9
  453.     for _,d in pairs(Chats) do
  454.         d.Gui.Position = PlayerUI.Position-UDim2.new(0,(-5)+(d.Gui.TextBounds.X/2),0,90+(d.Pos))
  455.     end
  456.     CHealth.Value = Vector3.new(Health,MaxHealth,CurrentWorld)
  457.     HealthUI.Position = PlayerUI.Position+UDim2.new(0,-20,0,-60)
  458.     HealthUIBK.Size = UDim2.new(Health/MaxHealth,0,1,0)
  459.     for i,v in pairs(game.Players:GetPlayers()) do
  460.         if v ~= Player then
  461.             if v:findFirstChild("IsPlaying") then
  462.                 if not FindPlayer(v.Name) then
  463.                     NewPlayer(v:findFirstChild("IsPlaying"))
  464.                 end
  465.             end
  466.         end
  467.     end
  468.     for i,v in pairs(Players) do
  469.         v.World = v.Health.Value.Z
  470.         if v.World ~= CurrentWorld then
  471.             v.F.Visible = false
  472.             v.H.Visible = false
  473.             v.C.Visible = false
  474.             for i,d in pairs(v.Chats) do
  475.                 d.Gui.Visible = false
  476.             end
  477.         else
  478.             v.F.Visible = true
  479.             v.H.Visible = true
  480.             v.C.Visible = true
  481.             for i,d in pairs(v.Chats) do
  482.                 d.Gui.Visible = true
  483.             end
  484.         end
  485.         v.F.Position = UDim2.new(0,v.Pos.Value.X,0.5,v.Pos.Value.Y-400)
  486.         v.H.Position = v.F.Position -UDim2.new(0,0,0,30)
  487.         v.H.Rotation = v.Pos.Value.Z
  488.         v.C.Position = v.F.Position -UDim2.new(0,0,0,90)
  489.         v.C.Text = tostring(v.Pos.Parent)
  490.         --print(v.Chatted)
  491.         for _,d in pairs(v.Chats) do
  492.             d.Gui.Position = v.F.Position-UDim2.new(0,(-5)+(d.Gui.TextBounds.X/2),0,120+(d.Pos))
  493.         end
  494.         v.C.Size = UDim2.new(0,v.C.TextBounds.X,0,v.C.TextBounds.Y)
  495.         --print(v.Count)
  496.         --print(v.Health.Value.Z)
  497.         if not v.Pos.Parent or not v.Pos.Parent:IsA("Player") or not v.Pos.Parent:IsDescendantOf(game.Players) then
  498.             v:Destroy()
  499.         end
  500.         v.Count = v.Health.Value.Z
  501.     end
  502. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement