Code: Select all
function love.load()
-- Returns the angle between two points
function math.getAngle(x1,y1, x2,y2) return math.atan2(x2-x1, y2-y1) end
-- Returns the distance between two points
function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end
love.graphics.setBackgroundColor(211,217,255)
-- mocky is a table, containing an image, x and y coordinates, and a speed
mocky = {
image = love.graphics.newImage("mocky.gif"),
x = 50, y = 50, speed = 100
}
-- enemies is a table of tables, where each table is a seperate enemy with an image, coordinates, and speed
enemies = {
{
image = love.graphics.newImage("Chomp1.png"),
x = 200, y = 300, speed = 80
},
{
image = love.graphics.newImage("Chomp2.png"),
x = 300, y = 300, speed = 60
}
}
end
function love.update(dt)
-- mocky movement code
if love.keyboard.isDown("right") then mocky.x = mocky.x + mocky.speed * dt
elseif love.keyboard.isDown("left") then mocky.x = mocky.x - mocky.speed * dt
end
if love.keyboard.isDown("down") then mocky.y = mocky.y + mocky.speed * dt
elseif love.keyboard.isDown("up") then mocky.y = mocky.y - mocky.speed * dt
end
-- enemy movement code
for k,enemy in pairs(enemies) do
local dx,dy = mocky.x - enemy.x, mocky.y - enemy.y
local len = math.sqrt(dx*dx + dy*dy)
if len > 0 then
enemy.x = enemy.x + dx/len * enemy.speed * dt
enemy.y = enemy.y + dy/len * enemy.speed * dt
end
end
end
function love.draw()
love.graphics.draw(mocky.image, mocky.x, mocky.y)
for k,enemy in pairs(enemies) do love.graphics.draw(enemy.image, enemy.x, enemy.y) end
end
Thanks alot guys. This is great progress. My priority now would be to actually have collision on them that causes mocky's death, so I'll take a look above at what vrld has said about the overlapping rectangles and try and see if I can integrate it into the above script....
vrld wrote:Edit:You need to add a , after the love.graphics.newImage(...).Head Over Heels wrote:Code: Select all
Syntax Error: main.lua:19: '}' expected (to close '{' at line 17) near 'x'
Cheers, can't believe I missed that!