Draw array
Posted: Mon Jun 07, 2021 7:35 pm
Hello.
I'm pretty sure this is not the best way to achieve my goal (draw the player in the middle of screen and everything near him, emulating a what I think is called "camera movement"??), however this is the only way I can think of.
I'm pretty sure this is not the best way to achieve my goal (draw the player in the middle of screen and everything near him, emulating a what I think is called "camera movement"??), however this is the only way I can think of.
Code: Select all
function love.load()
map={}
w_max=100
h_max=100
for y=1,h_max do
map[y]={}
for x=1,w_max do
map[y][x]=love.math.random(0,1)
end
end
playerX,playerY=1,1
oldchar=map[playerY][playerX]
map[playerY][playerX]=2
font_size=12
love.graphics.setNewFont(font_size)
end
function love.draw()
local char
for y=1,#map do
for x=1,#map[y] do
if map[y][x]==0 then char="."
elseif map[y][x]==1 then char="#"
elseif map[y][x]==2 then char="@" end
love.graphics.print(char,(x-1)*font_size,(y-1)*font_size)
end
end
end
function love.keypressed(k)
map[playerY][playerX]=oldchar
if k=="up" then playerY=playerY-1
elseif k=="down" then playerY=playerY+1
elseif k=="left" then playerX=playerX-1
elseif k=="right" then playerX=playerX+1 end
oldchar=map[playerY][playerX]
map[playerY][playerX]=2
end