完善小游戏逻辑
This commit is contained in:
+85
-50
@@ -1,8 +1,8 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
@@ -12,51 +12,77 @@ import (
|
||||
)
|
||||
|
||||
func TenChinaGameView() {
|
||||
game := NewTenGame(nil)
|
||||
game.StartShow()
|
||||
}
|
||||
|
||||
type TenGame struct {
|
||||
lock sync.Mutex
|
||||
|
||||
playerMax int // 玩家总人数
|
||||
playerID2Image map[int]fyne.Resource // 各个玩家对应的图标
|
||||
defaultImage fyne.Resource
|
||||
|
||||
CurrentRoundPlayer int // 当前行动的玩家
|
||||
players [][]int // 各个玩家已完成下棋数据
|
||||
|
||||
winCallback func(int) // 胜利返回
|
||||
|
||||
Items []*canvas.Image // 各个板块对象
|
||||
w fyne.Window
|
||||
|
||||
itemSize fyne.Size //单个格子大小
|
||||
itemX float32 // 格子总数x
|
||||
itemY float32 // 格子总数y
|
||||
}
|
||||
|
||||
func NewTenGame(winCallback func(int)) *TenGame {
|
||||
game := &TenGame{
|
||||
playerID2Image: map[int]fyne.Resource{
|
||||
0: theme.RadioButtonIcon(),
|
||||
1: theme.CancelIcon(),
|
||||
},
|
||||
defaultImage: theme.CheckButtonIcon(),
|
||||
|
||||
CurrentRoundPlayer: 0,
|
||||
players: make([][]int, 2),
|
||||
winCallback: winCallback,
|
||||
itemSize: fyne.NewSize(100, 100),
|
||||
itemX: 3,
|
||||
itemY: 3,
|
||||
}
|
||||
return game
|
||||
}
|
||||
|
||||
func (t *TenGame) StartShow() {
|
||||
myApp := fyne.CurrentApp()
|
||||
myWindow := myApp.NewWindow("Game")
|
||||
gridWrap := container.NewGridWrap(fyne.NewSize(100, 100))
|
||||
game := NewTenGame(2, func(i int) {
|
||||
fmt.Println("win", i)
|
||||
dialog.ShowInformation("游戏结束", fmt.Sprintf("%d 获得胜利", i), myWindow)
|
||||
//myWindow.Close()
|
||||
})
|
||||
for _, i := range game.Items {
|
||||
gridWrap.Add(container.NewBorder(nil, nil, nil, nil, i))
|
||||
}
|
||||
myWindow.SetContent(container.NewScroll(gridWrap))
|
||||
myWindow.Resize(fyne.NewSize(320, 318))
|
||||
t.w = myWindow
|
||||
myWindow.SetContent(t.CanvasObject())
|
||||
myWindow.Resize(fyne.NewSize(t.itemX*t.itemSize.Width+20, t.itemY*t.itemSize.Height+20))
|
||||
myWindow.CenterOnScreen()
|
||||
myWindow.Show()
|
||||
}
|
||||
|
||||
type TenGame struct {
|
||||
lock sync.Mutex
|
||||
playerMax int
|
||||
CurrentRoundPlayer int
|
||||
players [][]int
|
||||
Items []*widget.Button
|
||||
winCallback func(int)
|
||||
}
|
||||
|
||||
func NewTenGame(playerMax int, winCallback func(int)) *TenGame {
|
||||
game := &TenGame{
|
||||
playerMax: playerMax,
|
||||
CurrentRoundPlayer: 0,
|
||||
players: make([][]int, playerMax),
|
||||
winCallback: winCallback,
|
||||
}
|
||||
|
||||
var items = make([]*widget.Button, 9)
|
||||
for i := 0; i < 9; i++ {
|
||||
func (t *TenGame) CanvasObject() fyne.CanvasObject {
|
||||
gridWrap := container.NewGridWrap(t.itemSize)
|
||||
for i := 0; i < int(t.itemX*t.itemY); i++ {
|
||||
var itemIndex = i
|
||||
var item = widget.NewButton("", func() {
|
||||
game.Play(0, itemIndex)
|
||||
|
||||
image := canvas.NewImageFromResource(t.defaultImage)
|
||||
image.FillMode = canvas.ImageFillOriginal
|
||||
image.Resize(t.itemSize) //fy
|
||||
t.Items = append(t.Items, image)
|
||||
|
||||
toggle := widget.NewButton("", func() {
|
||||
t.Play(0, itemIndex)
|
||||
})
|
||||
item.SetIcon(theme.FyneLogo())
|
||||
items[i] = item
|
||||
toggle.Resize(t.itemSize)
|
||||
//toggle.Alignment = 3
|
||||
|
||||
gridWrap.Add(container.NewBorder(nil, nil, nil, nil, container.NewWithoutLayout(image, toggle)))
|
||||
}
|
||||
game.Items = items
|
||||
return game
|
||||
return container.NewScroll(gridWrap)
|
||||
}
|
||||
|
||||
func (t *TenGame) Play(userIndex int, pos int) error {
|
||||
@@ -67,8 +93,9 @@ func (t *TenGame) Play(userIndex int, pos int) error {
|
||||
//}
|
||||
t.play(pos)
|
||||
|
||||
// 推进
|
||||
t.CurrentRoundPlayer++
|
||||
if t.CurrentRoundPlayer+1 > t.playerMax {
|
||||
if t.CurrentRoundPlayer+1 > len(t.playerID2Image) {
|
||||
t.CurrentRoundPlayer = 0
|
||||
}
|
||||
return nil
|
||||
@@ -80,27 +107,35 @@ func (t *TenGame) Play(userIndex int, pos int) error {
|
||||
7, 8, 9
|
||||
*/
|
||||
func (t *TenGame) play(pos int) {
|
||||
// TODO
|
||||
// 更新玩家数据
|
||||
playerIntList := t.players[t.CurrentRoundPlayer]
|
||||
playerIntList = append(playerIntList, pos)
|
||||
// 刷新数据
|
||||
if t.CurrentRoundPlayer == 1 {
|
||||
t.Items[pos].SetIcon(theme.ConfirmIcon())
|
||||
} else {
|
||||
t.Items[pos].SetIcon(theme.CancelIcon())
|
||||
}
|
||||
|
||||
// check 胜利 判断
|
||||
slices.Sort(playerIntList)
|
||||
t.players[t.CurrentRoundPlayer] = playerIntList
|
||||
|
||||
// 刷新展示
|
||||
t.Items[pos].Resource = t.playerID2Image[t.CurrentRoundPlayer]
|
||||
t.Items[pos].Refresh()
|
||||
|
||||
// check 胜利 判断
|
||||
if t.winCheck(playerIntList) {
|
||||
t.winCallback(t.CurrentRoundPlayer)
|
||||
endView := widget.NewButton("关闭", func() {
|
||||
t.w.Close()
|
||||
})
|
||||
|
||||
img := canvas.NewImageFromResource(t.playerID2Image[t.CurrentRoundPlayer])
|
||||
img.FillMode = canvas.ImageFillOriginal
|
||||
|
||||
v := container.NewBorder(nil, endView, nil, nil,
|
||||
container.NewBorder(nil, nil, img, nil, widget.NewLabel("胜利")))
|
||||
dialog.NewCustomWithoutButtons("游戏结束", v, t.w).Show()
|
||||
if t.winCallback != nil {
|
||||
t.winCallback(t.CurrentRoundPlayer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TenGame) winCheck(playerIntList []int) bool {
|
||||
fmt.Println("playerIntList:", playerIntList)
|
||||
slices.Sort(playerIntList)
|
||||
if isArithmeticSequence(playerIntList, 1) ||
|
||||
isArithmeticSequence(playerIntList, 2) ||
|
||||
isArithmeticSequence(playerIntList, 3) ||
|
||||
|
||||
+6
-2
@@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
func mainUserViews(w fyne.Window) fyne.CanvasObject {
|
||||
TenChinaGameView()
|
||||
|
||||
var userCard = widget.NewCard("", "", nil)
|
||||
user1 := repo.User.GetUserInfo(global.DB)
|
||||
refresh := func(user *models.Users) {
|
||||
@@ -43,7 +43,11 @@ func mainUserViews(w fyne.Window) fyne.CanvasObject {
|
||||
} else {
|
||||
status = "离线"
|
||||
}
|
||||
userCard.SetContent(widget.NewLabel(fmt.Sprintf("状态:%s", status)))
|
||||
v := container.NewVBox(
|
||||
widget.NewLabel(fmt.Sprintf("状态:%s", status)),
|
||||
widget.NewButton("游戏", TenChinaGameView),
|
||||
)
|
||||
userCard.SetContent(v)
|
||||
}
|
||||
refresh(user1)
|
||||
return container.NewBorder(widget.NewToolbar(
|
||||
|
||||
Reference in New Issue
Block a user