How can i restrict the position of the frames?

I’m trying to make a little snake game but i can’t get the frames to be within the boundaries of the larger black frame

image
in the image here, the apple is outside the boundaries of the box

this is the code for the snake:

snake.BackgroundColor3 = Color3.fromRGB(0,255,0)
snake.BorderSizePixel = 0
snake.Size = UDim2.fromScale(0.05,0.05)
	
local randomPosX = math.random(0,SnakeFrame.AbsoluteSize.X-snake.AbsoluteSize.X)/SnakeFrame.AbsoluteSize.X
local randomPosY = math.random(0,SnakeFrame.AbsoluteSize.Y-snake.AbsoluteSize.Y)/SnakeFrame.AbsoluteSize.Y
	
local roundedPosX = math.round(randomPosX*10)/10
local roundedPosY = math.round(randomPosY*10)/10
	
snake.Position = UDim2.fromScale(roundedPosX,roundedPosY)
	
snake.Parent = SnakeFrame

The SnakeFrame’s size is 1,0,1,0 of the parent frame (0.5,0,0.5,0)

Use

Frame.Positon = math.clamp(Frame.Positon.X.Scale, minimum, maximum) 

Something similar to that, if math.clamp only supports integers (which it probably doesn’t) just mutliply the number and divide the final product.

We need the code for the apple spawning.

Simply do the same concept as @Laser_Gun5540 said but with the apple. Here’s a concept because I do not have your script:

apple.Position = UDIm2.new(math.clamp(scale,min,max),0,math.clamp(scale,min,max),0)

Edit: min and max are the corresponding edge positions of the frame, left, right, down, and up respectively

im not sure what the code actually does but the ui most likely went out of the frame because the anchor point is 0,0

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.