-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (133 loc) · 2.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
_ "embed"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
rand2 "math/rand"
"strconv"
"time"
)
//go:embed img/fu.jpeg
var fuImg []byte
type Theme int
const (
Do = iota
Num
)
var failKeepNum int
var score int
var TBtn Theme
var musicSingle = make(map[int]string)
var musicSingleDo = make(map[string]string)
var singleCardWget = &widget.Card{}
var btn = make([]*widget.Button, 7)
var randSingleNums = 0
var RandSeed = &rand2.Rand{}
func init() {
musicSingle[1] = "Do"
musicSingle[2] = "Ri"
musicSingle[3] = "Mi"
musicSingle[4] = "Fa"
musicSingle[5] = "Suo"
musicSingle[6] = "La"
musicSingle[7] = "Xi"
musicSingleDo["Do"] = "1"
musicSingleDo["Ri"] = "2"
musicSingleDo["Mi"] = "3"
musicSingleDo["Fa"] = "4"
musicSingleDo["Suo"] = "5"
musicSingleDo["La"] = "6"
musicSingleDo["Xi"] = "7"
TBtn = Do
RandSeed = rand2.New(rand2.NewSource(time.Now().UnixNano()))
}
func main() {
myApp := app.New()
//resource := fyne.NewStaticResource("time_icon", icon)
//myApp.SetIcon(resource)
myWindow := myApp.NewWindow("Music Practice")
grid := container.New(layout.NewGridLayout(1), singleSyllable(), numsBtn())
myWindow.SetContent(grid)
myWindow.Resize(fyne.NewSize(500, 100))
myWindow.ShowAndRun()
}
func singleSyllable() *fyne.Container {
singleCardWget = widget.NewCard("", "", nil)
singleCardWget.SetImage(&canvas.Image{
Resource: &fyne.StaticResource{
StaticName: "name",
StaticContent: fuImg,
},
})
calScore(0)
refreshCard()
leftBtn := widget.NewButton("Do", func() {
if TBtn == Do {
refreshCard()
return
}
TBtn = Do
refreshCard()
for _, obj := range btn {
obj.SetText(musicSingleDo[obj.Text])
}
})
rightBtn := widget.NewButton("Num", func() {
if TBtn == Num {
refreshCard()
return
}
TBtn = Num
refreshCard()
for _, obj := range btn {
n, _ := strconv.Atoi(obj.Text)
obj.SetText(musicSingle[n])
}
})
grid := container.New(layout.NewGridLayout(3), leftBtn, singleCardWget, rightBtn)
return grid
}
func numsBtn() *fyne.Container {
wgets := make([]fyne.CanvasObject, 7)
for i := 0; i < 7; i++ {
nums := i + 1
btn[i] = widget.NewButton(strconv.Itoa(nums), func() {
if nums == randSingleNums {
failKeepNum = 0
calScore(+10)
refreshCard()
} else {
failKeepNum++
calScore(-failKeepNum * 10)
}
})
wgets[i] = btn[i]
}
rand2.Seed(time.Now().UnixNano())
rand2.Shuffle(len(wgets), func(i, j int) {
wgets[i], wgets[j] = wgets[j], wgets[i]
})
return container.New(layout.NewGridLayout(7), wgets...)
}
func refreshCard() {
randSingleNums = getMusicSingle()
if TBtn == Do {
singleCardWget.SetTitle(musicSingle[randSingleNums])
} else {
singleCardWget.SetTitle(strconv.Itoa(randSingleNums))
}
}
func calScore(cNums int) {
score += cNums
if score < 0 {
score = 0
}
singleCardWget.SetSubTitle("Score " + strconv.Itoa(score))
}
func getMusicSingle() int {
return RandSeed.Intn(7) + 1
}