关于 love2d-gamedev
面向 Love2D 2D 游戏开发的参考技能,涵盖 Lua 常用模式、项目配置以及 iOS 部署要点。
主要功能
- 01游戏循环与回调模式
- 02渲染、动画与地图
- 03碰撞和物理技巧
- 04音频与输入处理
- 05项目配置和 iOS 部署
适用场景
- 01组织 Love2D 游戏循环
- 02添加精灵、地图和碰撞
- 03处理键盘、鼠标或触摸输入
- 04准备将游戏部署到 iOS
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ chongdashu/ love2d-pocket-bomber-game --skill love2d-gamedev安装后在 Claude Code 中输入/love2d-gamedev确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/animation.mdaudio.mdcollision.mdcore-architecture.mdgraphics-drawing.mdlibraries.mdproject-structure.mdtiles-maps.mdreferences/ios/overview.mdsetup.mdtouch-controls.mdxcode-project.md详细介绍
Love2D 游戏开发
使用 Love2D 框架打造精致的 2D 游戏,从首个原型到 iOS 发布。
快速参考
| 主题 | 使用场景 |
|---|---|
| 核心架构 | 了解游戏循环、回调和模块 |
| 图形与绘制 | 图像、颜色、变换和屏幕适配 |
| 动画 | 精灵表、四边形和帧计时 |
| 瓦片与地图 | 基于瓦片的关卡、地图加载 |
| 碰撞 | AABB、圆形和分离轴碰撞 |
| 音频 | 音效、音乐、音量控制 |
| 项目结构 | 文件组织、conf.lua、分发 |
| 库 | 常用社区库 |
| iOS 部署 | 构建、触控控制、App Store |
Love2D 游戏循环
每个 Love2D 游戏都遵循以下模式:
function love.load()
-- Called once at startup
-- Load assets, initialize state
end
function love.update(dt)
-- Called every frame
-- dt = time since last frame (seconds)
-- Update game logic here
end
function love.draw()
-- Called every frame after update
-- All rendering happens here
end
关键点:dt(时间增量)可确保不同帧率下速度一致。
-- WRONG: Speed varies with frame rate
player.x = player.x + 5
-- RIGHT: 200 pixels per second, regardless of FPS
player.x = player.x + 200 * dt
基本模式
加载和绘制图像
function love.load()
playerImage = love.graphics.newImage("player.png")
end
function love.draw()
love.graphics.draw(playerImage, x, y)
-- Full signature: draw(image, x, y, rotation, scaleX, scaleY, originX, originY)
end
输入处理
-- Polling (check every frame)
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - 200 * dt
end
end
-- Event-based (fires once per press)
function love.keypressed(key)
if key == "space" then
player:jump()
end
end
屏幕自适应定位
切勿将屏幕尺寸写死:
function love.load()
screenW, screenH = love.graphics.getDimensions()
end
function love.resize(w, h)
screenW, screenH = w, h
end
function love.draw()
-- Position relative to screen
local centerX = screenW / 2
local bottomY = screenH - 50
end
核心模块
| 模块 | 用途 | 关键函数 |
|---|---|---|
love.graphics | 渲染 | draw、rectangle、circle、print、setColor |
love.audio | 音效 | newSource、play、stop、setVolume |
love.keyboard | 键盘输入 | isDown、keypressed 回调 |
love.mouse | 鼠标输入 | getPosition、isDown、回调 |
love.touch | 触控输入 | touchpressed、touchmoved、touchreleased |
love.filesystem | 文件 I/O | read、write、getInfo |
love.timer | 计时 | getDelta、getTime、getFPS |
love.window | 窗口控制 | setMode、getMode、setTitle |
love.physics | Box2D 物理 | newWorld、newBody、newFixture |
项目设置
最小项目
my-game/
├── main.lua # Entry point (required)
└── conf.lua # Configuration (optional but recommended)
conf.lua 模板
function love.conf(t)
t.window.title = "My Game"
t.window.width = 800
t.window.height = 600
t.version = "11.5" -- Love2D version
t.console = true -- Enable console on Windows
-- Disable unused modules for faster startup
t.modules.joystick = false
t.modules.physics = false
end
运行游戏
# macOS
/Applications/love.app/Contents/MacOS/love /path/to/game
# Create alias in ~/.zshrc
alias love="/Applications/love.app/Contents/MacOS/love"
常见模式
状态管理
local gameState = "menu" -- menu, playing, paused, gameover
function love.update(dt)
if gameState == "playing" then
updateGame(dt)
end
end
function love.draw()
if gameState == "menu" then
drawMenu()
elseif gameState == "playing" then
drawGame()
end
end
面向对象实体
local Player = {}
Player.__index = Player
function Player:new(x, y)
return setmetatable({
x = x, y = y,
speed = 200,
image = love.graphics.newImage("player.png")
}, Player)
end
function Player:update(dt)
if love.keyboard.isDown("right") then
self.x = self.x + self.speed * dt
end
end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y)
end
return Player
摄像机/视口
local camera = { x = 0, y = 0 }
function love.draw()
love.graphics.push()
love.graphics.translate(-camera.x, -camera.y)
-- Draw world objects here
drawWorld()
love.graphics.pop()
-- Draw UI here (not affected by camera)
drawUI()
end
应避免的反模式
| 不要这样做 | 原因 | 应改为 |
|---|---|---|
| 将坐标写死 | 在不同屏幕上会出现问题 | 使用百分比或锚点 |
移动时忘记使用 dt | 速度会随帧率变化 | 乘以 dt |
在 update/draw 中加载资源 | 每帧都会加载,严重影响性能 | 在 love.load 中加载一次 |
| 到处使用全局变量 | 难以追踪,且名称会冲突 | 使用局部变量和模块 |
| 只在桌面端测试 | 触控行为有所不同 | 尽早在设备上测试 |
iOS 开发
有关 iOS 部署,请参阅 iOS 概览,其中涵盖:
- 构建设置 - Xcode 项目、库、签名
- 触控控制 - 虚拟摇杆、按钮、手势
- Xcode 项目结构 - 手动编辑 pbxproj
iOS 快速检查清单:
- 下载 Love2D iOS 源码和 Apple 库
- 将库复制到 Xcode 项目
- 修正部署目标(8.0 → 15.0)
- 创建
game.love(Lua 文件的 zip 包) - 将
game.love添加到 Xcode bundle 资源 - 配置签名并部署
理念
Love2D 通过简单易用的设计,让游戏开发充满乐趣:
- Lua 易于上手 - 动态类型、简洁的语法、快速迭代
- API 一致 - 函数遵循可预测的模式
- 游戏循环由你掌控 - 没有隐藏的魔法,完全由你控制
- 默认支持跨平台 - 同一份代码可在 Windows、macOS、Linux、iOS 和 Android 上运行
目标不只是“让它能运行”,而是“让它体验出色”。
流畅的动画、灵敏的操控、自适应的布局——这才是精良游戏的标准。
评论
支持 Markdown