PC版本规划如下:
本节主要是完成:游客登录 涉及到的知识点,主要是按钮,背景相关。 以下代码是个按钮实例,大家可以简单看下:
func NewGame() *Game {
g := &Game{}
g.button1 = &Button{
Rect: image.Rect(567, 567, 695, 599), //按钮的位置和坐标,可以调整位置!
Text: `Guest login`,
}
g.button1.SetOnPressed(func(b *Button) {
g.textBoxLog.AppendLine("Button 1 Pressed")
})
return g
}
g.button1 = &Button{
Rect: image.Rect(567, 567, 695, 599), //按钮的位置和坐标,可以调整位置!
Text: `Guest login`,
}
其中,按钮的位置坐标及大小:
Rect: image.Rect(567, 567, 695, 599), //按钮的位置和坐标,可以调整位置!
目前完成了按钮的UI,下节我们来调整下点击事件后,和登录服务器链接;后续会涉及到网络编程。主要是http、websocoket、rpc.
下面给大家分享一个另外的Ebitengine引擎的按钮点击实例,效果如下:
// Copyright 2018 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"image"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebitenui"
"github.com/hajimehoshi/ebitenui/layout"
)
type App struct {
root *ebitenui.Widget
root1 *ebitenui.Button
}
func (a *App) Update(screen *ebiten.Image) error {
a.root.Update()
if ebiten.IsRunningSlowly() {
return nil
}
a.root.Draw(screen)
return nil
}
func main() {
// 创建整体的区域大小
app := &App{
root: ebitenui.NewWidget(nil),
}
app.root.Resize(image.Rect(0, 0, 640, 480))
//------------------------------------------------------------------------------------------------------------------
// 创建面板
panel := ebitenui.NewWidget(&ebitenui.Panel{})
panel.Resize(image.Rect(30, 30, 300, 200))
//------------------------------------------------------------------------------------------------------------------
// 创建按钮的区域
button := ebitenui.NewWidget(&ebitenui.Button{
OnClick: func() {
fmt.Println("Button Click")
},
OnMouseDown: func() {
fmt.Println("Button MouseDown")
},
OnMouseUp: func() {
fmt.Println("Button MouseUp")
},
})
button.Resize(image.Rect(400, 400, 460, 460))
//------------------------------------------------------------------------------------------------------------------
// 面板中增加按钮
panel.AddChild(button, image.Rect(0, 0, layout.MaxAnchor, layout.MaxAnchor))
// 全部增加到整体上
app.root.AddChild(panel, image.Rect(0, 0, layout.MaxAnchor, layout.MaxAnchor))
//------------------------------------------------------------------------------------------------------------------
if err := ebiten.Run(app.Update, 640, 480, 1, "UI Example"); err != nil {
panic(err)
}
}
本节就暂时到这里,明天继续EbitEngine编程。
社区自己开发的IO小游戏,欢迎体验:
同学们,兴趣是最好的老师;只争朝夕,不负韶华!加油!
参考资料:
Go语言中文文档
http://www.golang.ltd/