Advertisement
KrYn0MoRe

snake game v1.6 (local)

Oct 12th, 2020 (edited)
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.72 KB | None | 0 0
  1. warn([[
  2. = Made by KrYn0MoRe (AlmightyToast) =
  3. - Settings added
  4. - Boosting added (hold space)
  5. ]])
  6.  
  7. local plr = game:GetService("Players").LocalPlayer
  8.  
  9. --
  10.  
  11. ScreenGui0 = Instance.new("ScreenGui")
  12. Frame1 = Instance.new("Frame")
  13. Frame2 = Instance.new("Frame")
  14. TextBox3 = Instance.new("TextBox")
  15. TextButton4 = Instance.new("TextButton")
  16. TextButton5 = Instance.new("TextButton")
  17. Frame6 = Instance.new("Frame")
  18. TextButton7 = Instance.new("TextButton")
  19. TextButton8 = Instance.new("TextButton")
  20. TextButton9 = Instance.new("TextButton")
  21. TextButton10 = Instance.new("TextButton")
  22. Frame11 = Instance.new("Frame")
  23. TextLabel12 = Instance.new("TextLabel")
  24. TextLabel13 = Instance.new("TextLabel")
  25. TextLabel14 = Instance.new("TextLabel")
  26. TextLabel15 = Instance.new("TextLabel")
  27. TextLabel16 = Instance.new("TextLabel")
  28. ScreenGui0.Parent = nil
  29. ScreenGui0.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  30. Frame1.Name = "Main"
  31. Frame1.Parent = ScreenGui0
  32. Frame1.Position = UDim2.new(0.5, -250, 0.5, -250)
  33. Frame1.Size = UDim2.new(0, 500, 0, 500)
  34. Frame1.BackgroundColor = BrickColor.new("Institutional white")
  35. Frame1.BackgroundColor3 = Color3.new(1, 1, 1)
  36. Frame1.BackgroundTransparency = 1
  37. Frame2.Name = "Map"
  38. Frame2.Parent = Frame1
  39. Frame2.Size = UDim2.new(1, 0, 1, 0)
  40. Frame2.BackgroundColor = BrickColor.new("Institutional white")
  41. Frame2.BackgroundColor3 = Color3.new(1, 1, 1)
  42. TextBox3.Name = "Title"
  43. TextBox3.Parent = Frame1
  44. TextBox3.Position = UDim2.new(0, 0, -0.200000003, 50)
  45. TextBox3.Size = UDim2.new(1, 0, 0, 50)
  46. TextBox3.BackgroundColor = BrickColor.new("Institutional white")
  47. TextBox3.BackgroundColor3 = Color3.new(1, 1, 1)
  48. TextBox3.BackgroundTransparency = 1
  49. TextBox3.Font = Enum.Font.SourceSans
  50. TextBox3.FontSize = Enum.FontSize.Size14
  51. TextBox3.Text = "Score: 0"
  52. TextBox3.TextColor = BrickColor.new("Institutional white")
  53. TextBox3.TextColor3 = Color3.new(1, 1, 1)
  54. TextBox3.TextScaled = true
  55. TextBox3.TextSize = 14
  56. TextBox3.TextStrokeTransparency = 0
  57. TextBox3.TextWrap = true
  58. TextBox3.TextWrapped = true
  59. TextButton4.Name = "settings_button"
  60. TextButton4.Parent = Frame1
  61. TextButton4.Position = UDim2.new(1.01800001, 0, -0.0020000299, 0)
  62. TextButton4.Size = UDim2.new(0, 100, 0, 100)
  63. TextButton4.BackgroundColor = BrickColor.new("Institutional white")
  64. TextButton4.BackgroundColor3 = Color3.new(1, 1, 1)
  65. TextButton4.BackgroundTransparency = 0.5
  66. TextButton4.Font = Enum.Font.SourceSans
  67. TextButton4.FontSize = Enum.FontSize.Size14
  68. TextButton4.Text = "Settings"
  69. TextButton4.TextColor = BrickColor.new("Institutional white")
  70. TextButton4.TextColor3 = Color3.new(1, 1, 1)
  71. TextButton4.TextScaled = true
  72. TextButton4.TextSize = 14
  73. TextButton4.TextStrokeTransparency = 0
  74. TextButton4.TextWrap = true
  75. TextButton4.TextWrapped = true
  76. TextButton5.Name = "close"
  77. TextButton5.Parent = Frame1
  78. TextButton5.Position = UDim2.new(0.931999981, 0, -0.0839999989, 0)
  79. TextButton5.Size = UDim2.new(0, 34, 0, 34)
  80. TextButton5.BackgroundColor = BrickColor.new("Really red")
  81. TextButton5.BackgroundColor3 = Color3.new(1, 0, 0)
  82. TextButton5.Font = Enum.Font.SourceSans
  83. TextButton5.FontSize = Enum.FontSize.Size14
  84. TextButton5.Text = "X"
  85. TextButton5.TextColor = BrickColor.new("Institutional white")
  86. TextButton5.TextColor3 = Color3.new(1, 1, 1)
  87. TextButton5.TextScaled = true
  88. TextButton5.TextSize = 14
  89. TextButton5.TextStrokeTransparency = 0
  90. TextButton5.TextWrap = true
  91. TextButton5.TextWrapped = true
  92. Frame6.Name = "Settings"
  93. Frame6.Parent = Frame1
  94. Frame6.Visible = false
  95. Frame6.Size = UDim2.new(1, 0, 1, 0)
  96. Frame6.BackgroundColor = BrickColor.new("Institutional white")
  97. Frame6.BackgroundColor3 = Color3.new(1, 1, 1)
  98. TextButton7.Name = "theme"
  99. TextButton7.Parent = Frame6
  100. TextButton7.Position = UDim2.new(0.399999976, 0, 0.717999995, 0)
  101. TextButton7.Size = UDim2.new(0, 100, 0, 100)
  102. TextButton7.BackgroundColor = BrickColor.new("Institutional white")
  103. TextButton7.BackgroundColor3 = Color3.new(1, 1, 1)
  104. TextButton7.Font = Enum.Font.SourceSans
  105. TextButton7.FontSize = Enum.FontSize.Size14
  106. TextButton7.Text = "LightMode"
  107. TextButton7.TextColor = BrickColor.new("Institutional white")
  108. TextButton7.TextColor3 = Color3.new(1, 1, 1)
  109. TextButton7.TextScaled = true
  110. TextButton7.TextSize = 14
  111. TextButton7.TextStrokeTransparency = 0
  112. TextButton7.TextWrap = true
  113. TextButton7.TextWrapped = true
  114. TextButton8.Name = "trail"
  115. TextButton8.Parent = Frame6
  116. TextButton8.Position = UDim2.new(0.699999928, 0, 0.717999995, 0)
  117. TextButton8.Size = UDim2.new(0, 100, 0, 100)
  118. TextButton8.BackgroundColor = BrickColor.new("Really red")
  119. TextButton8.BackgroundColor3 = Color3.new(1, 0, 0)
  120. TextButton8.Font = Enum.Font.SourceSans
  121. TextButton8.FontSize = Enum.FontSize.Size14
  122. TextButton8.Text = "Trail Fade"
  123. TextButton8.TextColor = BrickColor.new("Institutional white")
  124. TextButton8.TextColor3 = Color3.new(1, 1, 1)
  125. TextButton8.TextScaled = true
  126. TextButton8.TextSize = 14
  127. TextButton8.TextStrokeTransparency = 0
  128. TextButton8.TextWrap = true
  129. TextButton8.TextWrapped = true
  130. TextButton9.Name = "color"
  131. TextButton9.Parent = Frame6
  132. TextButton9.Position = UDim2.new(0.101999953, 0, 0.717999995, 0)
  133. TextButton9.Size = UDim2.new(0, 100, 0, 100)
  134. TextButton9.BackgroundColor = BrickColor.new("Lime green")
  135. TextButton9.BackgroundColor3 = Color3.new(0, 1, 0)
  136. TextButton9.Font = Enum.Font.SourceSans
  137. TextButton9.FontSize = Enum.FontSize.Size14
  138. TextButton9.Text = "Snake Color 2"
  139. TextButton9.TextColor = BrickColor.new("Institutional white")
  140. TextButton9.TextColor3 = Color3.new(1, 1, 1)
  141. TextButton9.TextScaled = true
  142. TextButton9.TextSize = 14
  143. TextButton9.TextStrokeTransparency = 0
  144. TextButton9.TextWrap = true
  145. TextButton9.TextWrapped = true
  146. TextButton10.Name = "start"
  147. TextButton10.Parent = Frame1
  148. TextButton10.Position = UDim2.new(1.01800001, 0, 0.199999973, 0)
  149. TextButton10.Size = UDim2.new(0, 100, 0, 100)
  150. TextButton10.BackgroundColor = BrickColor.new("Institutional white")
  151. TextButton10.BackgroundColor3 = Color3.new(1, 1, 1)
  152. TextButton10.BackgroundTransparency = 0.5
  153. TextButton10.Font = Enum.Font.SourceSans
  154. TextButton10.FontSize = Enum.FontSize.Size14
  155. TextButton10.Text = "Start"
  156. TextButton10.TextColor = BrickColor.new("Institutional white")
  157. TextButton10.TextColor3 = Color3.new(1, 1, 1)
  158. TextButton10.TextScaled = true
  159. TextButton10.TextSize = 14
  160. TextButton10.TextStrokeTransparency = 0
  161. TextButton10.TextWrap = true
  162. TextButton10.TextWrapped = true
  163. Frame11.Name = "Input"
  164. Frame11.Parent = Frame1
  165. Frame11.Position = UDim2.new(-0.459999979, 0, 0.197999999, 0)
  166. Frame11.Size = UDim2.new(0.405999988, 0, 0.540000021, 0)
  167. Frame11.BackgroundColor = BrickColor.new("Institutional white")
  168. Frame11.BackgroundColor3 = Color3.new(1, 1, 1)
  169. TextLabel12.Name = "w"
  170. TextLabel12.Parent = Frame11
  171. TextLabel12.Position = UDim2.new(0.374384224, 0, 0.0962962955, 0)
  172. TextLabel12.Size = UDim2.new(0, 50, 0, 50)
  173. TextLabel12.BackgroundColor = BrickColor.new("Really red")
  174. TextLabel12.BackgroundColor3 = Color3.new(1, 0, 0)
  175. TextLabel12.Font = Enum.Font.SourceSans
  176. TextLabel12.FontSize = Enum.FontSize.Size14
  177. TextLabel12.Text = "W"
  178. TextLabel12.TextColor = BrickColor.new("Really black")
  179. TextLabel12.TextColor3 = Color3.new(0, 0, 0)
  180. TextLabel12.TextScaled = true
  181. TextLabel12.TextSize = 14
  182. TextLabel12.TextWrap = true
  183. TextLabel12.TextWrapped = true
  184. TextLabel13.Name = "s"
  185. TextLabel13.Parent = Frame11
  186. TextLabel13.Position = UDim2.new(0.374384224, 0, 0.34074074, 0)
  187. TextLabel13.Size = UDim2.new(0, 50, 0, 50)
  188. TextLabel13.BackgroundColor = BrickColor.new("Really red")
  189. TextLabel13.BackgroundColor3 = Color3.new(1, 0, 0)
  190. TextLabel13.Font = Enum.Font.SourceSans
  191. TextLabel13.FontSize = Enum.FontSize.Size14
  192. TextLabel13.Text = "S"
  193. TextLabel13.TextColor = BrickColor.new("Really black")
  194. TextLabel13.TextColor3 = Color3.new(0, 0, 0)
  195. TextLabel13.TextScaled = true
  196. TextLabel13.TextSize = 14
  197. TextLabel13.TextWrap = true
  198. TextLabel13.TextWrapped = true
  199. TextLabel14.Name = "d"
  200. TextLabel14.Parent = Frame11
  201. TextLabel14.Position = UDim2.new(0.68472904, 0, 0.34074074, 0)
  202. TextLabel14.Size = UDim2.new(0, 50, 0, 50)
  203. TextLabel14.BackgroundColor = BrickColor.new("Really red")
  204. TextLabel14.BackgroundColor3 = Color3.new(1, 0, 0)
  205. TextLabel14.Font = Enum.Font.SourceSans
  206. TextLabel14.FontSize = Enum.FontSize.Size14
  207. TextLabel14.Text = "D"
  208. TextLabel14.TextColor = BrickColor.new("Really black")
  209. TextLabel14.TextColor3 = Color3.new(0, 0, 0)
  210. TextLabel14.TextScaled = true
  211. TextLabel14.TextSize = 14
  212. TextLabel14.TextWrap = true
  213. TextLabel14.TextWrapped = true
  214. TextLabel15.Name = "a"
  215. TextLabel15.Parent = Frame11
  216. TextLabel15.Position = UDim2.new(0.0738915801, 0, 0.34074074, 0)
  217. TextLabel15.Size = UDim2.new(0, 50, 0, 50)
  218. TextLabel15.BackgroundColor = BrickColor.new("Lime green")
  219. TextLabel15.BackgroundColor3 = Color3.new(0, 1, 0)
  220. TextLabel15.Font = Enum.Font.SourceSans
  221. TextLabel15.FontSize = Enum.FontSize.Size14
  222. TextLabel15.Text = "A"
  223. TextLabel15.TextColor = BrickColor.new("Really black")
  224. TextLabel15.TextColor3 = Color3.new(0, 0, 0)
  225. TextLabel15.TextScaled = true
  226. TextLabel15.TextSize = 14
  227. TextLabel15.TextWrap = true
  228. TextLabel15.TextWrapped = true
  229. TextLabel16.Name = "space"
  230. TextLabel16.Parent = Frame11
  231. TextLabel16.Position = UDim2.new(0.0738915503, 0, 0.577777803, 0)
  232. TextLabel16.Size = UDim2.new(0, 174, 0, 96)
  233. TextLabel16.BackgroundColor = BrickColor.new("Really red")
  234. TextLabel16.BackgroundColor3 = Color3.new(1, 0, 0)
  235. TextLabel16.Font = Enum.Font.SourceSans
  236. TextLabel16.FontSize = Enum.FontSize.Size14
  237. TextLabel16.Text = "Boost (Space)"
  238. TextLabel16.TextColor = BrickColor.new("Really black")
  239. TextLabel16.TextColor3 = Color3.new(0, 0, 0)
  240. TextLabel16.TextScaled = true
  241. TextLabel16.TextSize = 14
  242. TextLabel16.TextWrap = true
  243. TextLabel16.TextWrapped = true
  244.  
  245. --
  246.  
  247. ScreenGui0.Parent = plr:FindFirstChildOfClass('PlayerGui')
  248. script.Parent = ScreenGui0
  249.  
  250. local cube_ui = Instance.new("Frame")
  251. cube_ui.Name = "cube"
  252. cube_ui.Parent = script
  253. cube_ui.Size = UDim2.new(0, 30, 0, 30)
  254. cube_ui.BackgroundColor = BrickColor.new("Institutional white")
  255. cube_ui.BackgroundColor3 = Color3.new(1, 1, 1)
  256.  
  257. local gui = script.Parent
  258. local main = gui.Main
  259. local _settings = main.Settings
  260. local title = main.Title
  261. local start_button = main.start
  262. local settings_button = main.settings_button
  263. local theme_button = _settings.theme
  264. local trail_button = _settings.trail
  265. local color_button = _settings.color
  266. local close_button = main.close
  267. local input_ui = main.Input
  268. local map = main.Map
  269.  
  270. local food = {}
  271. local cubes = {}
  272. local body = {}
  273. local last_direction = 'a'
  274. local direction = last_direction
  275. local c_direction = direction
  276. local default_rate = 0.15
  277. local rate = default_rate
  278. local max_rate = 0--.15
  279. local rate_interval = 0.001
  280. local map_size = 510
  281. local cube_size = 30
  282. local score = 0
  283. local theme = 1 -- 1 = lightmode, 2 = darkmode
  284. local color = 2
  285. local x,y = 0,0
  286. local boost = false
  287. local eyes
  288. local themes = {
  289.     function()
  290.         theme_button.Text = 'Light Mode'
  291.         _settings.BackgroundColor3 = Color3.new(1,1,1)
  292.         input_ui.BackgroundColor3 = Color3.new(1,1,1)
  293.         start_button.BackgroundColor3 = Color3.new(1,1,1)
  294.         theme_button.BackgroundColor3 = Color3.new(1,1,1)
  295.         settings_button.BackgroundColor3 = Color3.new(1,1,1)
  296.         map.BackgroundColor3 = Color3.new(1,1,1)
  297.     end;
  298.     function()
  299.         theme_button.Text = 'Dark Mode'
  300.         _settings.BackgroundColor3 = Color3.fromRGB(77, 77, 77)
  301.         input_ui.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
  302.         start_button.BackgroundColor3 = Color3.new(0,0,0)
  303.         theme_button.BackgroundColor3 = Color3.new(0,0,0)
  304.         settings_button.BackgroundColor3 = Color3.new(0,0,0)
  305.         map.BackgroundColor3 = Color3.fromRGB(49, 49, 49)
  306.     end;
  307. }
  308. local snake_colors = {
  309.     Color3.new(1, 0, 1), -- pink
  310.     Color3.new(0, 1, 0), -- green
  311.     Color3.new(0, 0, 1), -- blue
  312.     Color3.new(1, 1, 0), -- yellow
  313. }
  314.  
  315. main.Position = UDim2.new(0.5,-map_size/2,0.5,-map_size/2)
  316. main.Size = UDim2.new(0,map_size,0,map_size)
  317.  
  318. local playing = false
  319. local paused = false
  320. local can_pause = false
  321. local trail = false
  322.  
  323. local main_music = Instance.new("Sound",gui)
  324. main_music.SoundId = 'rbxassetid://704165118'
  325. main_music.Looped = true
  326. main_music.Volume = 0
  327. main_music:Play()
  328. game:GetService("TweenService"):Create(main_music,TweenInfo.new(1),{Volume = 1}):Play()
  329.  
  330. theme_button.MouseButton1Click:Connect(function()
  331.     local click_s = Instance.new("Sound",gui)
  332.     click_s.SoundId = 'rbxassetid://452267918'
  333.     click_s.Volume = 0.5
  334.     click_s:Play()
  335.     theme = theme + 1
  336.     if theme > #themes then
  337.         theme = 1
  338.     end
  339.     themes[theme]()
  340.     update_grid()
  341. end)
  342.  
  343. close_button.MouseButton1Click:Connect(function()
  344.     gui:Destroy()
  345. end)
  346.  
  347. trail_button.MouseButton1Click:Connect(function()
  348.     local click_s = Instance.new("Sound",gui)
  349.     click_s.SoundId = 'rbxassetid://452267918'
  350.     click_s.Volume = 0.5
  351.     click_s:Play()
  352.     trail = not trail
  353.     if trail then
  354.         trail_button.BackgroundColor3 = Color3.new(0,1,0)
  355.     else
  356.         trail_button.BackgroundColor3 = Color3.new(1,0,0)
  357.     end
  358.     for i = 1,#body do
  359.         color_cube(body[i],i)
  360.     end
  361. end)
  362.  
  363. settings_button.MouseButton1Click:Connect(function()
  364.     local click_s = Instance.new("Sound",gui)
  365.     click_s.SoundId = 'rbxassetid://271111328'
  366.     click_s.Volume = 0.7
  367.     click_s:Play()
  368.     _settings.Visible = not _settings.Visible
  369. end)
  370.  
  371. color_button.MouseButton1Click:Connect(function()
  372.     local click_s = Instance.new("Sound",gui)
  373.     click_s.SoundId = 'rbxassetid://452267918'
  374.     click_s.Volume = 0.5
  375.     click_s:Play()
  376.     color = color + 1
  377.     if color > #snake_colors then
  378.         color = 1
  379.     end
  380.     color_button.Text = 'Snake Color ' .. color
  381.     color_button.BackgroundColor3 = snake_colors[color]
  382.     for i = 1,#body do
  383.         color_cube(body[i],i)
  384.     end
  385. end)
  386.  
  387. start_button.MouseButton1Click:Connect(function()
  388.     local click_s = Instance.new("Sound",gui)
  389.     click_s.SoundId = 'rbxassetid://452267918'
  390.     click_s.Volume = 0.5
  391.     click_s:Play()
  392.     if playing and can_pause then
  393.         paused = not paused
  394.         if paused then
  395.             start_button.Text = 'Resume'
  396.             boost = false
  397.         else
  398.             start_button.Text = 'Pause'
  399.         end
  400.     elseif not playing and not paused then
  401.         if main_music then
  402.             game:GetService("TweenService"):Create(main_music,TweenInfo.new(1),{Volume = 0}):Play()
  403.             game:GetService("Debris"):AddItem(main_music)
  404.         end
  405.         start()
  406.     end
  407. end)
  408.  
  409. function update_input()
  410.     input_ui['w'].BackgroundColor3 = Color3.new(1,0,0)
  411.     input_ui['a'].BackgroundColor3 = Color3.new(1,0,0)
  412.     input_ui['s'].BackgroundColor3 = Color3.new(1,0,0)
  413.     input_ui['d'].BackgroundColor3 = Color3.new(1,0,0)
  414.     input_ui[c_direction].BackgroundColor3 = Color3.new(0,1,0)
  415.     if boost then
  416.         input_ui['space'].BackgroundColor3 = Color3.new(0,1,0)
  417.     else
  418.         input_ui['space'].BackgroundColor3 = Color3.new(1,0,0)
  419.     end
  420. end
  421.  
  422. game:GetService("UserInputService").InputBegan:Connect(function(input,gamepress)
  423.     if input.UserInputType == Enum.UserInputType.Keyboard and playing and not gamepress then
  424.         local key = input.KeyCode
  425.         if key == Enum.KeyCode.A then
  426.             local s = 'a'
  427.             if s ~= c_direction then
  428.                 c_direction = s
  429.             end
  430.             update_input()
  431.         elseif key == Enum.KeyCode.D then
  432.             local s = 'd'
  433.             if s ~= c_direction then
  434.                 c_direction = s
  435.             end
  436.             update_input()
  437.         elseif key == Enum.KeyCode.W then
  438.             local s = 'w'
  439.             if s ~= c_direction then
  440.                 c_direction = s
  441.             end
  442.             update_input()
  443.         elseif key == Enum.KeyCode.S then
  444.             local s = 's'
  445.             if s ~= c_direction then
  446.                 c_direction = s
  447.             end
  448.             update_input()
  449.         elseif key == Enum.KeyCode.Space then
  450.             boost = true
  451.             update_input()
  452.         end
  453.     end
  454. end)
  455. game:GetService("UserInputService").InputEnded:Connect(function(input,gamepress)
  456.     if input.UserInputType == Enum.UserInputType.Keyboard and boost and playing and not gamepress then
  457.         local key = input.KeyCode
  458.         --[[
  459.         key = tostring(key)
  460.         key = string.lower(string.sub(key,string.len(key),string.len(key)))
  461.         if key == c_direction then
  462.             boost = false
  463.         end
  464.         ]]
  465.         if key == Enum.KeyCode.Space then
  466.             boost = false
  467.             update_input()
  468.         end
  469.     end
  470. end)
  471.  
  472. function add_food(add)
  473.     local cube = cubes[math.random(1,#cubes)]
  474.     if not table.find(body,cube) then
  475.         if add then
  476.             score = score + 1
  477.             rate = rate-rate_interval
  478.             if max_rate > rate then
  479.                 rate = max_rate
  480.             end
  481.             update_score()
  482.             local eat_s = Instance.new("Sound",gui)
  483.             eat_s.SoundId = 'rbxassetid://4959631727'
  484.             eat_s.Volume = 0.4
  485.             eat_s:Play()
  486.         end
  487.         table.insert(food,#food+1,cube)
  488.         color_cube(cube)
  489.     else
  490.         add_food(add)
  491.     end
  492. end
  493.  
  494. function add_body()
  495.     local rng = math.random(1,#cubes)
  496.     local cube = cubes[rng]
  497.     if not table.find(food,cube) then
  498.         y = cube.y.Value
  499.         x = cube.x.Value
  500.         table.insert(body,#body+1,cube)
  501.     else
  502.         add_body()
  503.     end
  504. end
  505.  
  506. function add_eyes()
  507.     eyes = cube_ui:Clone()
  508.     eyes.Position = UDim2.new(0,0,0,0)
  509.     eyes.Size = UDim2.new(1,0,1,0)
  510.     eyes.BackgroundTransparency = 1
  511.     eyes.Parent = body[1]
  512.     local s = map_size/cube_size/5
  513.     local e1 = Instance.new("Frame")
  514.     e1.BackgroundColor3 = Color3.new()
  515.     e1.Position = UDim2.new(0,s,0,s)
  516.     e1.Size = UDim2.new(0,s,0,s)
  517.     e1.Parent = eyes
  518.     local e2 = Instance.new("Frame")
  519.     e2.BackgroundColor3 = Color3.new()
  520.     e2.Position = UDim2.new(0,s,1,-(s*2))
  521.     e2.Size = UDim2.new(0,s,0,s)
  522.     e2.Parent = eyes
  523. end
  524.  
  525. function color_cube(v,i)
  526.     if table.find(food,v) then
  527.         v.BackgroundColor3 = Color3.new(1,0,0)
  528.     elseif table.find(body,v) then
  529.         if trail and body[1] ~= v then
  530.             v.BackgroundColor3 = Color3.new(
  531.                 snake_colors[color].r-(i/#body),
  532.                 snake_colors[color].g-(i/#body),
  533.                 snake_colors[color].b-(i/#body)
  534.             )
  535.         else
  536.             v.BackgroundColor3 = Color3.new(
  537.                 snake_colors[color].r,
  538.                 snake_colors[color].g,
  539.                 snake_colors[color].b
  540.             )
  541.         end
  542.     else
  543.         if theme == 2 then
  544.             v.BackgroundColor3 = Color3.fromRGB(49, 49, 49)
  545.         elseif theme == 3 then
  546.             v.BackgroundColor3 = Color3.fromRGB(255, 170, 0)
  547.         else
  548.             v.BackgroundColor3 = Color3.new(1,1,1)
  549.         end
  550.     end
  551. end
  552.  
  553. function move(cdir)
  554.     if c_direction ~= direction and c_direction ~= last_direction then
  555.         last_direction = direction
  556.     end
  557.     direction = c_direction
  558.     if cdir then
  559.         direction = cdir
  560.     else
  561.         cdir = direction
  562.     end
  563.     local rot = 0
  564.     if cdir == 'a' then -- left
  565.         x = x - 1
  566.         rot = 0
  567.     elseif cdir == 'd' then -- right
  568.         x = x + 1
  569.         rot = 180
  570.     elseif cdir == 'w' then -- up
  571.         y = y - 1 -- reversed
  572.         rot = 90
  573.     elseif cdir == 's' then -- down
  574.         y = y + 1 -- reversed
  575.         rot = -90
  576.     end
  577.     if 0 >= x or 0 >= y or x > cube_size or y > cube_size then
  578.         return lose()
  579.     end
  580.     --warn('x: ' .. x .. 'y:' .. y)
  581.     local opposite = false
  582.     if last_direction == 'a' and direction == 'd' then
  583.         opposite = true
  584.     elseif last_direction == 'w' and direction == 's' then
  585.         opposite = true
  586.     elseif last_direction == 's' and direction == 'w' then
  587.         opposite = true
  588.     elseif last_direction == 'd' and direction == 'a' then
  589.         opposite = true
  590.     end
  591.     local last_c = nil
  592.     local next_cube = cubes[(y*cube_size-cube_size)+x]
  593.     if table.find(food,next_cube) then
  594.         local b2 = {unpack(body)}
  595.         for i = 1,#body do
  596.             body[i+1] = b2[i]
  597.         end
  598.         body[1] = next_cube
  599.         eyes.Rotation = rot
  600.         eyes.Parent = body[1]
  601.         food[table.find(food,next_cube)] = nil
  602.         for i = 1,#body do
  603.             color_cube(body[i],i)
  604.         end
  605.         add_food(true)
  606.         return
  607.     end
  608.     for i = 1,#body do
  609.         if not last_c then
  610.             last_c = body[i]
  611.             color_cube(last_c,i)
  612.             if next_cube and not table.find(body,next_cube) then
  613.                 body[i] = next_cube
  614.                 eyes.Rotation = rot
  615.                 eyes.Parent = body[i]
  616.                 color_cube(last_c,i)
  617.             elseif next_cube and table.find(body,next_cube) and opposite then
  618.                 boost = false
  619.                 direction = last_direction
  620.                 c_direction = direction
  621.                 update_input()
  622.                 if last_direction == 'a' then -- center
  623.                     x = x - 1
  624.                 elseif last_direction == 'd' then -- center
  625.                     x = x + 1
  626.                 elseif last_direction == 'w' then -- center
  627.                     y = y - 1 -- reversed
  628.                 elseif last_direction == 's' then -- center
  629.                     y = y + 1 -- reversed
  630.                 end
  631.                 direction = cdir
  632.                 return move(last_direction)
  633.             else
  634.                 eyes.Rotation = rot
  635.                 eyes.Parent = body[i]
  636.                 return lose()
  637.             end
  638.         else
  639.             local last_c2 = body[i]
  640.             body[i] = last_c
  641.             color_cube(last_c2,i)
  642.             last_c = last_c2
  643.         end
  644.         color_cube(body[i],i)
  645.     end
  646. end
  647.  
  648. function update_grid()
  649.     for i,v in pairs(cubes) do
  650.         if table.find(food,v) then
  651.             v.BackgroundColor3 = Color3.new(1,0,0)
  652.         elseif table.find(body,v) then
  653.             v.BackgroundColor3 = Color3.new(0,1,0)
  654.         else
  655.             if theme == 2 then
  656.                 v.BackgroundColor3 = Color3.fromRGB(49, 49, 49)
  657.             elseif theme == 3 then
  658.                 v.BackgroundColor3 = Color3.fromRGB(255, 170, 0)
  659.             else
  660.                 v.BackgroundColor3 = Color3.new(1,1,1)
  661.             end
  662.         end
  663.     end
  664. end
  665.  
  666. function set()
  667.     map:ClearAllChildren()
  668.     food = {}
  669.     body = {}
  670.     cubes = {}
  671.     boost = false
  672.     --direction = 'a'
  673.     rate = default_rate
  674.     score = 0
  675.     x,y = 0,0
  676.     if eyes then
  677.         eyes:Destroy()
  678.     end
  679.     --title.Text = 'Score: ' .. score
  680.     --playing = false
  681.     --paused = false
  682.     for Y = 1,cube_size do
  683.         for X = 1,cube_size do
  684.             local cu = cube_ui:Clone()
  685.             if theme == 2 then
  686.                 cu.BackgroundColor3 = Color3.fromRGB(49, 49, 49)
  687.             else
  688.                 cu.BackgroundColor3 = Color3.new(1,1,1)
  689.             end
  690.             cu.Name = (Y .. ' ' .. X)
  691.             local yv = Instance.new("IntValue")
  692.             yv.Name = 'y'
  693.             yv.Value = Y
  694.             yv.Parent = cu
  695.             local xv = Instance.new("IntValue")
  696.             xv.Name = 'x'
  697.             xv.Value = X
  698.             xv.Parent = cu
  699.             local s = map_size/cube_size
  700.             cu.Position = UDim2.new(0,s*X-s,0,s*Y-s)
  701.             cu.Size = UDim2.new(0,s,0,s)
  702.             cu.Parent = map
  703.             table.insert(cubes,(Y*cube_size-cube_size)+X,cu)
  704.         end
  705.         wait(0)
  706.     end
  707.     add_body()
  708.     add_eyes()
  709.     add_food()
  710.     update_grid()
  711. end
  712.  
  713. function lose()
  714.     can_pause = false
  715.     title.Text = 'Ended with score of ' .. score
  716.     start_button.Text = '...'
  717.     local death_s = Instance.new("Sound",gui)
  718.     death_s.SoundId = 'rbxassetid://4649069508'
  719.     death_s.Volume = 1
  720.     death_s:Play()
  721.     local death_s2 = Instance.new("Sound",gui)
  722.     death_s2.SoundId = 'rbxassetid://3037922660'
  723.     death_s2.Volume = 1
  724.     death_s2:Play()
  725.     for i = 1,10 do
  726.         for i,v in pairs(body) do
  727.             v.BackgroundColor3 = Color3.new(1,0,0)
  728.         end
  729.         wait(1/2/i)
  730.         for i = 1,#body do
  731.             color_cube(body[i],i)
  732.         end
  733.         wait(1/2/i)
  734.     end
  735.     start_button.Text = 'Start'
  736.     main_music = Instance.new("Sound",gui)
  737.     main_music.SoundId = 'rbxassetid://704165118'
  738.     main_music.Looped = true
  739.     main_music.Volume = 0
  740.     main_music:Play()
  741.     game:GetService("TweenService"):Create(main_music,TweenInfo.new(1),{Volume = 1}):Play()
  742.     playing = false
  743. end
  744.  
  745. function update_score()
  746.     title.Text = 'Score: ' .. tostring(score)
  747. end
  748.  
  749. function start()
  750.     if not playing and not paused then else return end
  751.     playing = true
  752.     can_pause = false
  753.     start_button.Text = 'Starting..'
  754.     set()
  755.     local t = 5
  756.     for i = t,0,-1 do
  757.         title.Text = 'Starting.. (' .. i .. ')'
  758.         wait(1)
  759.     end
  760.     title.Text = 'Go!'
  761.     local start_s = Instance.new("Sound",gui)
  762.     start_s.SoundId = 'rbxassetid://3044638823'
  763.     start_s.Volume = 1
  764.     start_s:Play()
  765.     wait(1)
  766.     can_pause = true
  767.     start_button.Text = 'Pause'
  768.     update_score()
  769.     while playing do
  770.         if not paused then
  771.             move()
  772.             --update_grid()
  773.         end
  774.         if boost then
  775.             wait(rate/2)
  776.         else
  777.             wait(rate)
  778.         end
  779.     end
  780. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement