golang纯Go游戏引擎开发插件库Oak的使用
Golang纯Go游戏引擎开发插件库Oak的使用
Oak是一个纯Go编写的游戏引擎,提供了丰富的功能来开发2D游戏。下面我将介绍如何使用Oak引擎进行游戏开发。
安装
go get -u github.com/oakmound/oak/v4
快速开始
这是一个最基本的Oak程序示例:
package main
import (
"github.com/oakmound/oak/v4"
"github.com/oakmound/oak/v4/scene"
)
func main() {
oak.AddScene("firstScene", scene.Scene{
Start: func(*scene.Context) {
// ... 绘制实体,绑定回调 ...
},
})
oak.Init("firstScene")
}
功能特性
Oak提供了以下主要功能:
-
窗口管理
- 支持多窗口同时运行
- 从shiny分叉的窗口和键盘事件处理
-
图像渲染
- 支持图像操作(通过gift库)
- 内置常见渲染类型(Sprite, Sequence, Switch, Composite等)
- 基本图形绘制(ColorBox, Line, Bezier等)
-
粒子系统
-
鼠标处理
-
手柄支持
-
音频支持
-
碰撞检测
- 基于R-Tree的碰撞检测
- 2D光线投射
- 碰撞空间管理
-
2D物理系统
-
事件处理
完整示例
下面是一个更完整的示例,展示如何创建一个简单的游戏场景:
package main
import (
"github.com/oakmound/oak/v4"
"github.com/oakmound/oak/v4/render"
"github.com/oakmound/oak/v4/scene"
)
func main() {
oak.AddScene("demo", scene.Scene{
Start: func(ctx *scene.Context) {
// 创建一个红色矩形
redBox := render.NewColorBox(50, 50, render.Red)
redBox.SetPos(100, 100)
render.Draw(redBox)
// 创建一个蓝色圆形
blueCircle := render.NewReverting(render.NewCircle(25, render.Blue))
blueCircle.SetPos(200, 200)
render.Draw(blueCircle)
// 绑定键盘事件
ctx.EventHandler.GlobalBind(func(key string, _ interface{}) int {
switch key {
case "ArrowUp":
blueCircle.ShiftY(-10)
case "ArrowDown":
blueCircle.ShiftY(10)
case "ArrowLeft":
blueCircle.ShiftX(-10)
case "ArrowRight":
blueCircle.ShiftX(10)
}
return 0
}, "KeyDown")
},
})
oak.Init("demo", func(c oak.Config) (oak.Config, error) {
c.Title = "Oak Demo"
c.Screen.Width = 800
c.Screen.Height = 600
return c, nil
})
}
粒子系统示例
package main
import (
"github.com/oakmound/oak/v4"
"github.com/oakmound/oak/v4/particle"
"github.com/oakmound/oak/v4/render"
"github.com/oakmound/oak/v4/scene"
"github.com/oakmound/oak/v4/vgfx"
)
func main() {
oak.AddScene("particles", scene.Scene{
Start: func(ctx *scene.Context) {
// 创建粒子发射器
emitter := particle.NewColorGenerator(
particle.Color(render.Red, render.Blue),
particle.Size(5, 10),
particle.Speed(1, 3),
particle.Angle(0, 360),
particle.LifeSpan(1*time.Second, 3*time.Second),
particle.Spread(10, 10),
particle.Pos(400, 300),
)
// 每秒生成30个粒子
particle.NewEmitter(emitter, 30*time.Millisecond, ctx.DrawStack)
},
})
oak.Init("particles")
}
碰撞检测示例
package main
import (
"github.com/oakmound/oak/v4"
"github.com/oakmound/oak/v4/collision"
"github.com/oakmound/oak/v4/entities"
"github.com/oakmound/oak/v4/render"
"github.com/oakmound/oak/v4/scene"
)
const (
Player collision.Label = iota
Enemy
)
func main() {
oak.AddScene("collision", scene.Scene{
Start: func(ctx *scene.Context) {
// 创建玩家
player := entities.NewMoving(100, 100, 32, 32,
render.NewColorBox(32, 32, render.Blue),
nil, 0, 0)
player.Space.UpdateLabel(Player)
// 创建敌人
enemy := entities.NewSolid(200, 100, 32, 32,
render.NewColorBox(32, 32, render.Red),
nil, 0)
enemy.Space.UpdateLabel(Enemy)
// 碰撞检测
player.Space.AddCollision(Enemy, func(s, s2 *collision.Space) {
render.Draw(render.NewText("Collision!", 300, 300))
})
},
})
oak.Init("collision")
}
总结
Oak是一个功能丰富的纯Go游戏引擎,适合开发2D游戏。它提供了从渲染、输入处理到物理和碰撞检测的全套功能。通过上面的示例,你可以快速开始使用Oak进行游戏开发。
更多关于golang纯Go游戏引擎开发插件库Oak的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang纯Go游戏引擎开发插件库Oak的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Oak游戏引擎插件库使用指南
Oak是一个轻量级、纯Go编写的2D游戏引擎,专注于简单性和易用性。下面我将详细介绍如何使用Oak进行游戏开发。
安装Oak
首先需要安装Oak库:
go get github.com/oakmound/oak/v3
基本框架
Oak游戏的基本结构包含场景(Scene)和实体(Entity):
package main
import (
"github.com/oakmound/oak/v3"
"github.com/oakmound/oak/v3/scene"
)
func main() {
// 初始化游戏窗口
oak.AddScene("firstScene", scene.Scene{
Start: func(ctx *scene.Context) {
// 游戏初始化逻辑
},
})
// 启动游戏
oak.Init("firstScene")
}
创建精灵(Sprite)
import (
"github.com/oakmound/oak/v3/render"
"github.com/oakmound/oak/v3/entities"
)
func createPlayer(ctx *scene.Context) {
// 加载精灵图片
sprite, err := render.LoadSprite("assets/player.png")
if err != nil {
panic(err)
}
// 创建实体
player := entities.New(ctx,
entities.WithPosition(100, 100),
entities.WithRenderable(sprite),
entities.WithSpeed(2, 2),
entities.WithDimensions(32, 32),
)
// 添加键盘控制
player.Bind(entities.OnCollision, func(_ *entities.Entity, _ interface{}) int {
// 碰撞处理逻辑
return 0
})
}
输入处理
import "github.com/oakmound/oak/v3/event"
func handleInput(ctx *scene.Context) {
// 键盘事件处理
event.GlobalBind(ctx, event.Enter, func(_ event.CID, _ interface{}) int {
// 每帧调用的逻辑
if oak.IsDown("W") {
// 上移
}
if oak.IsDown("A") {
// 左移
}
if oak.IsDown("S") {
// 下移
}
if oak.IsDown("D") {
// 右移
}
return 0
})
}
场景管理
func setupScenes() {
// 主菜单场景
oak.AddScene("mainMenu", scene.Scene{
Start: func(ctx *scene.Context) {
// 创建菜单UI
},
})
// 游戏场景
oak.AddScene("gameScene", scene.Scene{
Start: func(ctx *scene.Context) {
createPlayer(ctx)
handleInput(ctx)
},
})
// 切换场景
oak.AddCommand("switch", func(sceneName string) {
oak.SwitchScene(sceneName)
})
}
碰撞检测
import "github.com/oakmound/oak/v3/physics"
func setupCollision() {
// 创建碰撞空间
space := physics.NewSpace(0, 0, 800, 600, 0)
// 为实体添加碰撞体
player.AddCollision(space,
physics.NewLabel(physics.Label{
Group: "player",
Layers: "enemy|obstacle",
}),
)
// 碰撞回调
player.Bind(entities.OnCollision, func(_ *entities.Entity, other interface{}) int {
if label, ok := other.(physics.Label); ok {
if label.Group == "enemy" {
// 与敌人碰撞
}
}
return 0
})
}
音频处理
import "github.com/oakmound/oak/v3/audio"
func playSound() {
// 加载音效
sound, err := audio.Load("assets/sound.wav")
if err != nil {
panic(err)
}
// 播放音效
audio.Play(sound)
}
完整示例
package main
import (
"github.com/oakmound/oak/v3"
"github.com/oakmound/oak/v3/audio"
"github.com/oakmound/oak/v3/entities"
"github.com/oakmound/oak/v3/event"
"github.com/oakmound/oak/v3/physics"
"github.com/oakmound/oak/v3/render"
"github.com/oakmound/oak/v3/scene"
)
func main() {
oak.AddScene("game", scene.Scene{
Start: func(ctx *scene.Context) {
// 创建玩家
sprite, _ := render.LoadSprite("assets/player.png")
player := entities.New(ctx,
entities.WithPosition(100, 100),
entities.WithRenderable(sprite),
entities.WithSpeed(2, 2),
entities.WithDimensions(32, 32),
)
// 设置碰撞
space := physics.NewSpace(0, 0, 800, 600, 0)
player.AddCollision(space,
physics.NewLabel(physics.Label{
Group: "player",
Layers: "enemy|obstacle",
}),
)
// 键盘控制
event.GlobalBind(ctx, event.Enter, func(_ event.CID, _ interface{}) int {
if oak.IsDown("W") {
player.ShiftY(-player.Speed.Y())
}
if oak.IsDown("A") {
player.ShiftX(-player.Speed.X())
}
if oak.IsDown("S") {
player.ShiftY(player.Speed.Y())
}
if oak.IsDown("D") {
player.ShiftX(player.Speed.X())
}
return 0
})
// 加载背景音乐
music, _ := audio.Load("assets/music.mp3")
audio.Play(music)
},
})
oak.Init("game", func(c oak.Config) (oak.Config, error) {
c.Screen.Width = 800
c.Screen.Height = 600
c.Title = "My Oak Game"
return c, nil
})
}
最佳实践
- 资源管理:使用
render.SetDrawStack
管理渲染层级 - 性能优化:对于大量相似对象,使用
render.Batch
进行批量渲染 - 状态管理:利用
event.Caller
实现游戏状态机 - 调试工具:使用
oak.SetDebugOptions
开启调试信息
Oak引擎虽然轻量,但足以支持中小型2D游戏的开发。它的纯Go实现使得编译和部署非常方便,适合需要跨平台支持的项目。