I'll put in the code that I copied from the "shooting at the mouse" tutorial.
Code: Select all
function love.load()
player = {}
player.x = 500
player.y = 500
bullets = {}
bulletSpeed = 1000
end
function love.update(dt)
if love.keyboard.isDown("a") then
player.x = player.x - 300*dt
end
if love.keyboard.isDown("d") then
player.x = player.x + 300*dt
end
if love.keyboard.isDown("w") then
player.y = player.y - 300*dt
end
if love.keyboard.isDown("s") then
player.y = player.y + 300*dt
end
for index,key in ipairs(bullets) do
key.x = key.x + (key.dx * dt)
key.y = key.y + (key.dy * dt)
end
end
function love.mousepressed(x, y, button)
if button == "l" then
local startX = player.x + 40 / 2
local startY = player.y + 40 / 2
local mouseX = x
local mouseY = y
local angle = math.atan2((mouseY - startY), (mouseX - startX))
local bulletDx = bulletSpeed * math.cos(angle)
local bulletDy = bulletSpeed * math.sin(angle)
table.insert(bullets, {x = startX, y = startY, dx = bulletDx, dy = bulletDy})
end
end
function love.draw()
love.graphics.rectangle("fill",player.x,player.y,40,40)
for i,v in ipairs(bullets) do
love.graphics.circle("fill", v.x, v.y, 10)
end
end