网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
三.主角的诞生
现在要来创造我们的主角了,首先创建一个类代表游戏中所有可移动的物体
package model
import(
“game2D/resource”
“game2D/constant”
“github.com/go-gl/mathgl/mgl32”
)
//可移动的游戏对象
type MoveObj struct{
GameObj
//在上下左右方向是否可移动
stockUp,stockDown,stockLeft,stockRight bool
//水平移动速度
movementSpeed float32
//飞行速度
fallSpeed float32
//下坠速度
flySpeed float32
//移动时的动画纹理
moveTextures []*resource.Texture2D
//静止时的纹理
stantTexture *resource.Texture2D
//游戏地图
gameMap *GameMap
//当前运动帧
moveIndex int
//运动帧之间的切换阈值
moveDelta float32
}
func NewMoveObject(gameObj GameObj,movementSpeed,flySpeed float32, moveTextures []*resource.Texture2D,gameMap *GameMap) *MoveObj{
moveObj := &MoveObj{GameObj:gameObj,
movementSpeed:movementSpeed,
fallSpeed:100,
gameMap:gameMap,
moveTextures:moveTextures,
flySpeed:flySpeed,
moveIndex:0,
moveDelta:0,
stantTexture:gameObj.texture}
return moveObj
}
//恢复静止
func (moveObj *MoveObj) Stand(){
moveObj.texture = moveObj.stantTexture
}
//由用户主动发起的运动
func(moveObj *MoveObj) Move(direction constant.Direction, delta float32){
shift := mgl32.Vec2{0,0}
if(direction ==constant. DOWN){
if(!moveObj.stockDown && moveObj.y + moveObj.size[1] < moveObj.gameMap.Height){
shift[1] += moveObj.flySpeed * delta
}
}
if(direction == constant.UP){
if(!moveObj.stockUp && moveObj.y > 0){
shift[1] -= moveObj.flySpeed * delta
}
}
if(direction == constant.LEFT){
moveObj.ReverseX()
if(moveObj.moveIndex >= len(moveObj.moveTextures)){
moveObj.moveIndex = 0
}
moveObj.moveDelta += delta
if(moveObj.moveDelta > 0.1){
moveObj.moveDelta = 0
moveObj.texture = moveObj.moveTextures[moveObj.moveIndex]
moveObj.moveIndex += 1
}
if(!moveObj.stockLeft && moveObj.x > 0){
shift[0] -= moveObj.movementSpeed * delta
}
}
if(direction == constant.RIGHT){
moveObj.ForWardX()
if(moveObj.moveIndex >= len(moveObj.moveTextures)){
moveObj.moveIndex = 0
}
moveObj.moveDelta += delta
if(moveObj.moveDelta > 0.1){
moveObj.moveDelta = 0
moveObj.texture = moveObj.moveTextures[moveObj.moveIndex]
moveObj.moveIndex += 1
}
if(!moveObj.stockRight && moveObj.x + moveObj.size[0] < moveObj.gameMap.Width){
shift[0] += moveObj.movementSpeed * delta
}
}
isCol,position := moveObj.gameMap.IsColl(moveObj.GameObj,shift)
if(isCol){
moveObj.SetPosition(position)
}else{
moveObj.x += shift[0]
moveObj.y += shift[1]
}
}
这个类只有一个特殊的move方法,传入一个代表方向的常量和帧与帧之间的延迟,当往左或往右运动时会将自身纹理切换为运动的纹理并在度过指定时间之后切换运动帧来形成动画效果,当静止时调用Stand
方法会将纹理帧切换为静止时的图像。注意,在往左或又运动时我们只需要将纹理动画直接求镜像而不用创建新的纹理。当然这种动画的实现方式是不太优雅的,更好的方法是调用gl.SubTextImage
方法将纹理图像直接替换为指定的图像而不是切换纹理,或者将多张图片拼成为一张,在运动时调整纹理坐标来显示不同图像。不过理解了这一种,其他两种也不会有问题。
4.还不够抽象
目前为止,我们已经有了地图,游戏角色,摄像头和精灵渲染器,但在man
方法里直接创建和修改这些对象似乎不太优雅,我们创建一个Game类来进一步封装
package game
import(
“game2D/resource”
“game2D/sprite”
“game2D/camera”
“game2D/model”
“game2D/constant”
“github.com/go-gl/mathgl/mgl32”
“github.com/go-gl/gl/v4.1-core/gl”
“github.com/go-gl/glfw/v3.2/glfw”
)
type GameState int
const(
GAME_ACTIVE GameState = 0
GAME_MENU GameState = 1
)
type Game struct{
//游戏状态
state GameState
//屏幕大小
screenWidth, screenHeight float32
//世界大小
worldWidth, worldHeight float32
//精灵渲染器
renderer *sprite.SpriteRenderer
//游戏地图
gameMap *model.GameMap
//摄像头
camera *camera.Camera2D
//玩家
player *model.MoveObj
//按键状态
Keys [1024]bool
}
func NewGame(screenWidth, screenHeight, wordWidth, wordHeight float32) *Game{
game := Game{screenWidth:screenWidth,
screenHeight:screenHeight,
worldWidth:wordWidth,
worldHeight:wordHeight,
state:GAME_ACTIVE}
return &game
}
func (game *Game) Init(){
//初始化着色器
resource.LoadShader(“./glsl/shader.vs”, “./glsl/shader.fs”, “sprite”)
shader := resource.GetShader(“sprite”)
shader.Use()
shader.SetInt(“image”, 0)
//设置投影
projection := mgl32.Ortho(0, float32(game.screenWidth),float32(game.screenHeight),0, -1, 1)
shader.SetMatrix4fv(“projection”, &projection[0])
//初始化精灵渲染器
game.renderer = sprite.NewSpriteRenderer(shader)
//加载资源
resource.LoadTexture(gl.TEXTURE0,“./image/stone.png”,“stone”)
resource.LoadTexture(gl.TEXTURE0,“./image/soil.png”,“soil”)
resource.LoadTexture(gl.TEXTURE0,“./image/man-stand.png”,“man-stand”)
resource.LoadTexture(gl.TEXTURE0,“./image/1.png”,“1”)
resource.LoadTexture(gl.TEXTURE0,“./image/2.png”,“2”)
resource.LoadTexture(gl.TEXTURE0,“./image/3.png”,“3”)
resource.LoadTexture(gl.TEXTURE0,“./image/4.png”,“4”)
resource.LoadTexture(gl.TEXTURE0,“./image/5.png”,“5”)
resource.LoadTexture(gl.TEXTURE0,“./image/6.png”,“6”)
//创建游戏地图
game.gameMap = model.NewGameMap(game.worldWidth, game.worldHeight,“testMapFile”)
//创建测试游戏人物
gameObj := model.NewGameObj(resource.GetTexture(“man-stand”),
game.worldWidth/2,
game.worldHeight/2,
&mgl32.Vec2{70,100},
0,
&mgl32.Vec3{1,1,1})
//创建摄像头,将摄像头同步到玩家位置
game.camera = camera.NewDefaultCamera(game.worldHeight,
game.worldWidth,
game.screenWidth,
game.screenHeight,
mgl32.Vec2{game.worldWidth/2 - game.screenWidth/2, game.worldHeight/2 - game.screenHeight/2})
game.player = model.NewMoveObject(*gameObj,1000,1000,[]*resource.Texture2D{resource.GetTexture(“1”),
resource.GetTexture(“2”),
resource.GetTexture(“3”),
resource.GetTexture(“4”),
resource.GetTexture(“5”),
resource.GetTexture(“6”),},game.gameMap)
}
//处理输入
func (game *Game) ProcessInput(delta float64){
if(game.state == GAME_ACTIVE){
playerMove := false
if(game.Keys[glfw.KeyA]){
playerMove = true
game.player.Move(constant.LEFT,float32(delta))
}
if(game.Keys[glfw.KeyD]){
playerMove = true
game.player.Move(constant.RIGHT,float32(delta))
}
if(game.Keys[glfw.KeyW]){
playerMove = true
game.player.Move(constant.UP,float32(delta))
}
if(game.Keys[glfw.KeyS]){
playerMove = true
game.player.Move(constant.DOWN,float32(delta))
}
if(!playerMove){
game.player.Stand()
}
}
}
func (game *Game) Update(delta float64){
}
//渲染每一帧
func (game *Game) Render(delta float64){
resource.GetShader(“sprite”).SetMatrix4fv(“view”,game.camera.GetViewMatrix())
//game.player.MoveBy(float32(delta))
game.player.Draw(game.renderer)
//摄像头跟随
position := game.player.GetPosition()
size := game.player.GetSize()
game.camera.InPosition(position[0] - game.screenWidth /2 + size[0], position[1] - game.screenHeight / 2 + size[1])
game.gameMap.Draw(game.camera.GetPosition(),
mgl32.Vec2{game.screenWidth,game.screenHeight},
game.renderer)
}
在game
类中我们在初始化函数里加载资源创建地图和其他需要的对象,在渲染函数中始终将摄像头的位置与主角位置同步,并在ProcessInput
方法内将按键信息转化为方向信息
5.动起来
万事俱备,现在要做的是在main方法内让程序跑起来了
package main
import(
“github.com/go-gl/glfw/v3.2/glfw”
“github.com/go-gl/gl/v4.1-core/gl”
“game2D/game”
“runtime”
)
const (
width = 800
height = 600
WORD_WIDTH float32 = 1500
WORD_HEIGHT float32 = 1000
)
var (
windowName = "æˆ‘çˆ±ä½ "
game2D = game.NewGame(width,height,WORD_WIDTH,WORD_HEIGHT)
deltaTime = 0.0
lastFrame = 0.0
)
func main(){
runtime.LockOSThread()
window := initGlfw()
defer glfw.Terminate()
initOpenGL()
game2D.Init()
for !window.ShouldClose() {
currentFrame := glfw.GetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfw.PollEvents()
game2D.ProcessInput(deltaTime)
game2D.Update(deltaTime)
gl.Clear(gl.COLOR_BUFFER_BIT);
game2D.Render(deltaTime)
window.SwapBuffers()
}
}
func initGlfw() *glfw.Window {
if err := glfw.Init(); err != nil {
panic(err)
}
glfw.WindowHint(glfw.Resizable, glfw.False)
window, err := glfw.CreateWindow(width, height, windowName, nil, nil)
window.SetKeyCallback(KeyCallback)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
return window
}
func initOpenGL(){
if err := gl.Init(); err != nil {
panic(err)
}
gl.Viewport(0, 0, width, height);
gl.Enable(gl.CULL_FACE);
gl.Enable(gl.BLEND);
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
nil)
window.SetKeyCallback(KeyCallback)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
return window
}
func initOpenGL(){
if err := gl.Init(); err != nil {
panic(err)
}
gl.Viewport(0, 0, width, height);
gl.Enable(gl.CULL_FACE);
gl.Enable(gl.BLEND);
}
[外链图片转存中…(img-WSKaDsFg-1715715094679)]
[外链图片转存中…(img-eBSQwYUA-1715715094679)]
[外链图片转存中…(img-2AbibUYP-1715715094680)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新