2PlayerShooter/player.lua
Simon Kellet 6c1b73d970 init push
2024-04-19 23:46:04 +01:00

40 lines
947 B
Lua

Player = Object:extend()
function Player:new(x, y, p, health, image, speed)
self.image = love.graphics.newImage(image)
self.x = x
self.y = y
self.p = p
self.health = health
self.speed = speed
self.width = self.image:getWidth()
self.height = self.image:getHeight()
end
function Player:update(dt)
if self.p == 1 then
if love.keyboard.isDown("d") then
self.x = self.x + (self.speed * dt)
elseif love.keyboard.isDown("a") then
self.x = self.x - (self.speed * dt)
end
elseif self.p == 2 then
if love.keyboard.isDown("right") then
self.x = self.x + (self.speed * dt)
elseif love.keyboard.isDown("left") then
self.x = self.x - (self.speed * dt)
end
end
--check if player is out of bounds
if self.x < 0 then
self.x = 0
elseif self.x + self.width > love.graphics.getWidth() then
self.x = love.graphics.getWidth() - self.width
end
end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y)
end