local lg = love.graphics local function make_player(x, y, width, height, screen) local player = { position={ x=x, y=y, theta=-math.pi / 2 }, width=width, height=height, screen=screen } player.draw = function(self) local w, h = self.width / 2, self.height / 2 lg.push() lg.setColor(0, 0, 0) lg.translate(self.position.x, self.position.y) lg.rotate(self.position.theta) lg.polygon("fill", -w,-h, -w,h, w,0) lg.pop() end player.translate = function(self, amount) local w, h = self.width / 2, self.height / 2 local new_x = self.position.x + (amount * math.cos(self.position.theta)) local new_y = self.position.y + (amount * math.sin(self.position.theta)) if new_x + w > self.screen.width then new_x = self.screen.width - w end if new_x - w < 0 then new_x = w end if new_y + h > self.screen.height then new_y = self.screen.height - h end if new_y - h < 0 then new_y = h end self.position.x = new_x self.position.y = new_y end player.rotate = function(self, degrees) local radians = math.rad(degrees) local new_theta = self.position.theta + radians if new_theta < 0 then new_theta = new_theta + (2 * math.pi) end if new_theta > 2 * math.pi then new_theta = new_theta - (2 * math.pi) end self.position.theta = new_theta end return player end local screen = { width=400, height=240 } local player = make_player(screen.width / 2, screen.height / 2, 10, 10, screen) function love.load() love.window.setMode(screen.width, screen.height) end function love.update(dt) if love.keyboard.isDown("w") then player:translate(1) end if love.keyboard.isDown("s") then player:translate(-1) end if love.keyboard.isDown("d") then player:rotate(5) end if love.keyboard.isDown("a") then player:rotate(-5) end end function love.draw() lg.setColor(1, 1, 1) lg.rectangle("fill", 0, 0, screen.width, screen.height) player:draw() end