Code: Select all
function love.load()
player = {
x = love.graphics.getWidth()/2,
y = love.graphics.getHeight()/2,
walkspeed = 300,
walkspeedx = 0,
walkspeedy = 0,
keybinds = {
up = "w",
down = "s",
left = "a",
right = "d",
shoot = "space"
},
dia = 17
}
bullet = {
x = 0,
y = 0,
dia = 5,
speed = 20,
speedx = 0,
speedy = 0,
}
bullets = {}
end
function love.update(dt)
movement(dt)
shoot(dt)
end
function love.draw(dt)
love.graphics.setColor(0,0,255)
love.graphics.circle("fill", player.x, player.y, player.dia)
love.graphics.setColor(255,0,0)
for i, v in pairs(bullets) do
love.graphics.circle("fill",v.x,v.y,v.dia)
end
end
function shoot(dt)
if love.keyboard.isDown(player.keybinds.shoot) then
mx,my = love.mouse.getPosition()
bullet.speedx = (mx - player.x)/ math.sqrt((mx - player.x) * (mx - player.x) + (my - player.y) * (my - player.y))
bullet.speedy = (my - player.y)/ math.sqrt((mx - player.x) * (mx - player.x) + (my - player.y) * (my - player.y))
bullet.x = player.x
bullet.y = player.y
table.insert(bullets, bullet)
end
for i, v in pairs(bullets) do
v.x = v.x + v.speedx * v.speed * dt
v.y = v.y + v.speedy * v.speed * dt
end
end
function movement(dt)
if love.keyboard.isDown(player.keybinds.left) then
player.walkspeedx = 0 - player.walkspeed
elseif love.keyboard.isDown(player.keybinds.right) then
player.walkspeedx = player.walkspeed
else
player.walkspeedx = 0
end
if love.keyboard.isDown(player.keybinds.up) then
player.walkspeedy = 0 - player.walkspeed
elseif love.keyboard.isDown(player.keybinds.down) then
player.walkspeedy = player.walkspeed
else
player.walkspeedy = 0
end
player.x = player.x + player.walkspeedx * dt
player.y = player.y + player.walkspeedy * dt
end