I've tried to make platformer like this:
Code: Select all
--might be weird but...
lp=love.graphics;
lk=love.keyboard;
function love.update(dt)
heroUpdate(dt);
end
function love.draw()
mapDraw(10, 10);
heroDraw();
end
hero={
x=10,
y=20,
w=10,
h=10,
speed=75,
velocity=0,
jump=-50,
gravity=-70,
};
local map={
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0},
{1, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
};
local w=10;
local h=10;
function collision(x, y)
if hero.x < x+w and
hero.x+hero.w > x and
hero.y < y+h and
hero.y+hero.h > y then
return true;
else
return false;
end
end
function mapDraw(mapX, mapY)
for y=1, #map do
for x=1, #map[1] do
if map[y][x]==0 then
lp.setColor(30, 30, 30);
elseif map[y][x]==1 then
--collision happens
if collision(mapX+(w*x)-w, mapY+(y*h)-h) then
hero.velocity=0;
lp.setColor(255, 40, 255);
else
lp.setColor(255, 255, 40);
end
end
lp.rectangle("fill", mapX+(w*x)-w, mapY+(y*h)-h, w, h);
end
end
end
function heroUpdate(dt)
if lk.isDown("left") then
hero.x=hero.x-hero.speed*dt;
elseif lk.isDown("right") then
hero.x=hero.x+hero.speed*dt;
end
if lk.isDown("space") or lk.isDown("up") then
if hero.velocity==0 then
hero.velocity=hero.jump;
end
end
if hero.velocity~=0 then
hero.y=hero.y+hero.velocity*dt;
hero.velocity=hero.velocity-hero.gravity*dt;
end
end
function heroDraw()
lp.setColor(255, 40, 40);
lp.rectangle("fill", hero.x, hero.y, hero.w, hero.h);
end
How can i make it fall after it tries to walk on the same direction?
How can i make the boxes solid? (like when it hits it instantly stops.)
Thank you alot!