简短的回答,你不能使用 Löve API,但使用 Lua。
Löve2D 使用 PhysFS(对 IO 功能的抽象)来创建沙盒环境。这样,没有经验的程序员就不会在测试或执行期间意外删除整个硬盘驱动器或至少造成一些损坏。这意味着,在 Löves 启动时(在编译文件 boot.lua 内),设置可写和可读文件夹,以后不能取消设置。因此,到目前为止,Löve API 本身不允许将文件保存在预期的存储位置之外。
然而,Lua 本身提供标准的os 和io 功能,这些功能不通过 PhysFS。从那里你有几个选择:
通过 Löve2D 保存文件,然后通过os.rename 将其移动到所需文件夹或使用io.open 复制以读取文件并将其写入所需位置。
或者您可以直接使用io 将屏幕截图保存在所需位置。
无论哪种方式,您都可能遇到两个问题:
请注意,以下代码仅适用于 Windows。如果这不是问题,请继续,如果你不想让它在 linux 下也能工作,你可能需要调整一些东西。
首先,Windows 将图片保存在C:\Users\<username>\Pictures 中。
我们将通过environment variable 访问用户配置文件文件夹。
然后我们需要创建一个folder,如果您希望将这些屏幕截图保存在子目录中。
旁注:我假设love.getIdentity 返回游戏名称。
local function mkdir(path)
-- Notice we are using %q to allow for spaces (and prevent command injection)
os.execute(string.format("mkdir %q", path)) -- System dependant
end
local function combinePaths(...)
return (table.concat({...}, "\\"):gsub("\\+", "\\")) -- System dependant
end
local saveScreenshot
function love.load()
love.filesystem.setIdentity("My Game")
local screenshotDirectory = combinePaths(love.filesystem.getUserDirectory(), "Pictures", love.filesystem.getIdentity())
mkdir(screenshotDirectory)
function saveScreenshot()
local fileName = os.date("%Y.%m.%d--%H-%M-%S")..".png"
local filePath = combinePaths(love.filesystem.getSaveDirectory(), fileName)
local destPath = combinePaths(screenshotDirectory, fileName)
-- We are using a callback, because the screenshot is saved after draw() finishes executing
---@param image love.ImageData
love.graphics.captureScreenshot(function(image)
image:encode("png", fileName)
os.rename(filePath, destPath)
end)
end
end
function love.keypressed(key)
if key == "c" then
saveScreenshot()
end
end
function love.draw()
love.graphics.circle("fill", 400, 300, 200)
end