love2d-gamedev
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLove2D Game Development
Love2D游戏开发
Build polished 2D games with the Love2D framework—from first prototype to iOS release.
使用Love2D框架打造精致的2D游戏——从最初的原型到iOS版本发布。
Quick Reference
快速参考
| Topic | When to Use |
|---|---|
| Core Architecture | Understanding game loop, callbacks, modules |
| Graphics & Drawing | Images, colors, transforms, screen adaptation |
| Animation | Sprite sheets, quads, frame timing |
| Tiles & Maps | Tile-based levels, map loading |
| Collision | AABB, circle, and separating axis collision |
| Audio | Sound effects, music, volume control |
| Project Structure | File organization, conf.lua, distribution |
| Libraries | Popular community libraries |
| iOS Deployment | Build, touch controls, App Store |
| 主题 | 适用场景 |
|---|---|
| 核心架构 | 理解游戏循环、回调函数、模块 |
| 图形与绘制 | 图像、颜色、变换、屏幕适配 |
| 动画 | 精灵表、四边形、帧时序 |
| 瓦片与地图 | 基于瓦片的关卡、地图加载 |
| 碰撞检测 | AABB、圆形、分离轴碰撞检测 |
| 音频 | 音效、音乐、音量控制 |
| 项目结构 | 文件组织、conf.lua、发布打包 |
| 库 | 热门社区库 |
| iOS部署 | 构建、触摸控制、App Store上架 |
The Love2D Game Loop
Love2D游戏循环
Every Love2D game follows this pattern:
lua
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
endKey insight: (delta time) ensures consistent speed across frame rates.
dtlua
-- 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每个Love2D游戏都遵循以下模式:
lua
function love.load()
-- 启动时仅调用一次
-- 加载资源,初始化状态
end
function love.update(dt)
-- 每帧调用一次
-- dt = 距上一帧的时间(秒)
-- 在此处更新游戏逻辑
end
function love.draw()
-- 在update之后每帧调用一次
-- 所有渲染操作在此执行
end核心要点:(增量时间)确保在不同帧率下游戏速度保持一致。
dtlua
-- 错误写法:速度随帧率变化
player.x = player.x + 5
-- 正确写法:每秒移动200像素,与帧率无关
player.x = player.x + 200 * dtEssential Patterns
核心模式
Loading and Drawing Images
图像加载与绘制
lua
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)
endlua
function love.load()
playerImage = love.graphics.newImage("player.png")
end
function love.draw()
love.graphics.draw(playerImage, x, y)
-- 完整参数:draw(图像, x坐标, y坐标, 旋转角度, X轴缩放, Y轴缩放, 原点X, 原点Y)
endInput Handling
输入处理
lua
-- 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
endlua
-- 轮询模式(每帧检查)
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - 200 * dt
end
end
-- 事件驱动模式(每次按键触发一次)
function love.keypressed(key)
if key == "space" then
player:jump()
end
endScreen-Adaptive Positioning
屏幕自适应定位
Never hard-code screen dimensions:
lua
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永远不要硬编码屏幕尺寸:
lua
function love.load()
screenW, screenH = love.graphics.getDimensions()
end
function love.resize(w, h)
screenW, screenH = w, h
end
function love.draw()
-- 相对于屏幕定位
local centerX = screenW / 2
local bottomY = screenH - 50
endCore Modules
核心模块
| Module | Purpose | Key Functions |
|---|---|---|
| Rendering | |
| Sound | |
| Keyboard input | |
| Mouse input | |
| Touch input | |
| File I/O | |
| Timing | |
| Window control | |
| Box2D physics | |
| 模块 | 用途 | 关键函数 |
|---|---|---|
| 渲染 | |
| 音效 | |
| 键盘输入 | |
| 鼠标输入 | |
| 触摸输入 | |
| 文件I/O | |
| 计时 | |
| 窗口控制 | |
| Box2D物理引擎 | |
Project Setup
项目设置
Minimal Project
最小项目结构
my-game/
├── main.lua # Entry point (required)
└── conf.lua # Configuration (optional but recommended)my-game/
├── main.lua # 入口文件(必填)
└── conf.lua # 配置文件(可选但推荐)conf.lua Template
conf.lua模板
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
endlua
function love.conf(t)
t.window.title = "My Game"
t.window.width = 800
t.window.height = 600
t.version = "11.5" # Love2D版本
t.console = true # 在Windows上启用控制台
-- 禁用未使用的模块以加快启动速度
t.modules.joystick = false
t.modules.physics = false
endRunning the Game
运行游戏
bash
undefinedbash
undefinedmacOS
macOS
/Applications/love.app/Contents/MacOS/love /path/to/game
/Applications/love.app/Contents/MacOS/love /path/to/game
Create alias in ~/.zshrc
在~/.zshrc中创建别名
alias love="/Applications/love.app/Contents/MacOS/love"
---alias love="/Applications/love.app/Contents/MacOS/love"
---Common Patterns
常见模式
State Management
状态管理
lua
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
endlua
local gameState = "menu" # 菜单、游戏中、暂停、游戏结束
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
endObject-Oriented Entities
面向对象的实体
lua
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 Playerlua
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 PlayerCamera/Viewport
相机/视口
lua
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()
endlua
local camera = { x = 0, y = 0 }
function love.draw()
love.graphics.push()
love.graphics.translate(-camera.x, -camera.y)
-- 在此处绘制世界对象
drawWorld()
love.graphics.pop()
-- 在此处绘制UI(不受相机影响)
drawUI()
endAnti-Patterns to Avoid
需要避免的反模式
| Don't | Why | Do Instead |
|---|---|---|
| Hard-code coordinates | Breaks on different screens | Use percentages or anchors |
Forget | Speed varies with frame rate | Multiply by |
Load assets in | Loads every frame, kills performance | Load once in |
| Use global variables everywhere | Hard to track, name collisions | Use local variables and modules |
| Test only on desktop | Touch behaves differently | Test on device early |
| 不要做 | 原因 | 正确做法 |
|---|---|---|
| 硬编码坐标 | 在不同屏幕上会失效 | 使用百分比或锚点 |
移动时忘记 | 速度随帧率变化 | 乘以 |
在 | 每帧都加载,严重影响性能 | 在 |
| 到处使用全局变量 | 难以追踪,容易出现命名冲突 | 使用局部变量和模块 |
| 仅在桌面端测试 | 触摸操作表现不同 | 尽早在设备上测试 |
iOS Development
iOS开发
For iOS deployment, see the iOS Overview which covers:
- Build Setup - Xcode project, libraries, signing
- Touch Controls - Virtual joysticks, buttons, gestures
- Xcode Project Structure - Manual pbxproj editing
Quick iOS checklist:
- Download Love2D iOS source + Apple libraries
- Copy libraries to Xcode project
- Fix deployment target (8.0 → 15.0)
- Create (zip of Lua files)
game.love - Add to Xcode bundle resources
game.love - Configure signing and deploy
关于iOS部署,请查看iOS概述,其中涵盖:
- 构建设置 - Xcode项目、库、签名
- 触摸控制 - 虚拟摇杆、按钮、手势
- Xcode项目结构 - 手动编辑pbxproj
iOS快速检查清单:
- 下载Love2D iOS源码 + Apple库
- 将库复制到Xcode项目
- 修改部署目标(从8.0改为15.0)
- 创建(Lua文件的压缩包)
game.love - 将添加到Xcode的捆绑资源中
game.love - 配置签名并部署
Philosophy
设计理念
Love2D makes game development joyful through simplicity:
- Lua is approachable - Dynamic typing, clean syntax, fast iteration
- The API is consistent - Functions follow predictable patterns
- You own the game loop - No hidden magic, full control
- Cross-platform by default - Same code runs on Windows, macOS, Linux, iOS, Android
The goal isn't just "make it work." The goal is "make it feel great."
Smooth animations, responsive controls, adaptive layouts—that's the standard for polished games.
Love2D通过简洁性让游戏开发充满乐趣:
- Lua易于上手 - 动态类型、简洁语法、快速迭代
- API保持一致 - 函数遵循可预测的模式
- 完全掌控游戏循环 - 无隐藏逻辑,全面控制
- 默认跨平台 - 相同代码可在Windows、macOS、Linux、iOS、Android上运行
目标不仅仅是“能运行”,而是“体验出色”。
流畅的动画、响应迅速的控制、自适应布局——这些是精致游戏的标准。