Code: Select all
graphics = love.graphics
graphics.setDefaultFilter("nearest", "nearest")
local gun = {
img = graphics.newImage("gun.png"),
x = 100,
y = 100,
}
local angle = 0
local half_pi = math.pi / 2
local bul = {
x = 0,
y = 0,
vx = 0,
vy = 0,
speed = 500,
}
function love.update(dt)
bul.x = bul.x + bul.vx * dt
bul.y = bul.y + bul.vy * dt
end
function love.mousemoved(x, y)
angle = math.atan2(y - gun.y * 2, x - gun.x * 2)
end
function love.mousepressed(x, y, b)
if b == 1 then
bul.x = gun.x + 7 * math.cos(angle)
bul.y = gun.y + 7 * math.sin(angle)
bul.vx = bul.speed * math.cos(angle)
bul.vy = bul.speed * math.sin(angle)
end
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
end
end
function love.draw()
graphics.push()
graphics.scale(2)
graphics.draw(gun['img'], gun.x, gun.y, angle, 1, (angle > half_pi or angle < -half_pi) and -1 or 1, 6, 3)
graphics.circle("line", bul.x, bul.y, 1)
graphics.pop()
end