create random onstacles

Good morning, how can i create random obstacles in my videogames with lua code?

Thanks

Here is a quick example:

local numObstacles = 10		-- number of obstacles you want to create

for i = 1, numObstacles do
	local widthObstacle = 50
	local heightObstacle = 50

	-- randomize x and y coordinates of obstacles
	local xRandom = math.random( widthObstacle, display.contentWidth - widthObstacle )
	local yRandom = math.random( heightObstacle, display.contentHeight - heightObstacle )

	-- create obstacle
	local obstacle = display.newRect( xRandom, yRandom, widthObstacle, heightObstacle )
end
1 Like