https://love2d.org/wiki/Tutorial:Gridlocked_Player
What I'm trying to do is randomize this bit of code, it don't have to be in any specific order at all.
Code: Select all
map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
(It's in the draw function)
Code: Select all
if map[y][x] == 1 then
https://www.tutorialspoint.com/lua/lua_for_loop.htm
https://www.tutorialspoint.com/lua/lua_tables.htm
Code: Select all
for init,max/min value, increment
do
statement(s)
end
What i had in mind was taking the large map table and randomly generating each section, the same was it's displayed above, so something like
Code: Select all
--for loop is creating however many sections i need inside the map table to make a map
for i = 10, 1, -1
do
mapSection = {}
mapSection.i = math.random(0,1)
table.insert (map, i, mapSection)
end
Code: Select all
function love.load()
-- we need to create tables like this in a loop
--then inject them into a map table.
map = {}
for i = 10, 1, -1
do
mapSection = {}
mapSection.i = math.random(0,1)
table.insert (map, i, mapSection)
end
world = map
end
function love.draw()
for y=1, #world do
for x=1, #world[y] do -- 1-wall, 2-tree, 3-water, 4-rock r,g,b
if world[y][x] == 1 then
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
end
end
end
end
Code: Select all
function love.load()
allMaps = {
map1 = { -- 1-wall, 2-tree, 3-water, 4-rock, 5-door
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1 }, -- this one will always be a wall just about
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
{ 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 },
{ 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1 },
{ 1, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 4, 0, 3, 3, 0, 0, 0, 0, 4, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 5 },
{ 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1 } -- this one will just about always be 1 maybe unless a door?
},
map2 = {
{ 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- this one will always be a wall just about
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },-- only the first and last one will be a wall, everything in between will be random?
{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } -- this one will just about always be 1 maybe unless a door?
},
map3 = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, -- this one will always be a wall just about
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
{ 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },
{ 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } -- this one will just about always be 1 maybe unless a door?
},
map4 = {
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1 }, -- this one will always be a wall just about
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } -- this one will just about always be 1 maybe unless a door?
},
map5 = { -- 1-wall, 2-tree, 3-water, 4-rock
{ 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 3, 3, 3, 1 }, -- this one will always be a wall just about
{ 3, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 }, -- only the first and last one will be a wall, everything in between will be random?
{ 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1 },
{ 3, 3, 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1 },
{ 3, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 0, 1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 3, 0, 0, 4, 0, 3, 3, 0, 0, 0, 0, 4, 0, 1 },
{ 3, 3, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 1 },
{ 3, 3, 3, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1 } -- this one will just about always be 1 maybe unless a door?
}
}
math.randomseed(os.time()*360)
mapSelect = math.random(1,5) ------------------make sure to put the last map# here!
if mapSelect == 1 then
world = allMaps.map1
elseif mapSelect == 2 then
world = allMaps.map2
elseif mapSelect == 3 then
world = allMaps.map3
elseif mapSelect == 4 then
world = allMaps.map4
end
end
player = {
x = 50,
y = 50,
width = 32,
height = 32,
grid_x = 256,
grid_y = 256,
act_x = 200,
act_y = 200,
speed = 32
}
function love.update(dt)
player.act_y = player.act_y - (player.act_y - player.grid_y) * player.speed * dt
player.act_x = player.act_x - (player.act_x - player.grid_x) * player.speed * dt
end
function love.draw()
love.graphics.rectangle("fill", player.act_x, player.act_y, player.width, player.height)
for y=1, #world do
for x=1, #world[y] do -- 1-wall, 2-tree, 3-water, 4-rock r,g,b
if world[y][x] == 1 then
love.graphics.setColor(1,1,1,1)
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
love.graphics.setColor(1,1,1,1)
elseif world[y][x] == 2 then
love.graphics.setColor(0,1,0,1)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
love.graphics.setColor(1,1,1,1)
elseif world[y][x] == 3 then
love.graphics.setColor(0,0,1,1)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
love.graphics.setColor(1,1,1,1)
elseif world[y][x] == 4 then
love.graphics.setColor(.5,.5,.5,1)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
love.graphics.setColor(1,1,1,1)
elseif world[y][x] == 5 then
love.graphics.setColor(.04,.03,0,1)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
love.graphics.setColor(.9,.9,0,1)
love.graphics.circle("fill", x * 32+5, y * 32+16, 5)
love.graphics.setColor(1,1,1,1)
end
end
end
--reset the color
love.graphics.setColor( 1, 1, 1, 1 )
end
function testMap(x, y)
--0 false, 1 true, 2 door
--if tile is 1-4
if world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] >= 1 and world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] < 5 then
return 0
--else if tile is 5
elseif world[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 5 then
return 2
end
return 1
end
function love.keypressed(key)
if key == "up" then
if testMap(0, -1) == 1 then
player.grid_y = player.grid_y - 32
end
elseif key == "down" then
if testMap(0, 1) == 1 then
player.grid_y = player.grid_y + 32
end
elseif key == "left" then
if testMap(-1, 0) == 1 then
player.grid_x = player.grid_x - 32
end
elseif key == "right" then
if testMap(1, 0) == 1 then
player.grid_x = player.grid_x + 32
end
elseif key == "escape" then
love.load()
end
end