Dog Animation Demo – Fyne GUI Golang tutorial 48

Dog Animation Demo – Fyne GUI Golang tutorial 48

 

Source Code


package main
import (
    "math/rand"
    "time"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/storage"
    "fyne.io/fyne/v2/widget"
)
func main() {
    a := app.New()
    w := a.NewWindow("Dog And Cat Play")
    x := float32(10)
    y := float32(10)
    rand.Seed(time.Now().UnixNano())
    cx := float32(rand.Intn(400))
    cy := float32(rand.Intn(400))
    w.Resize(fyne.NewSize(400, 400))
    entry := widget.NewEntry()
    entry.Resize(fyne.NewSize(200, 40))
    entry.Move(fyne.NewPos(100, 100))
    label := widget.NewLabel("Your selection")
    label.Resize(fyne.NewSize(200, 70))
    label.Move(fyne.NewPos(110, 60))
    entry2 := widget.NewSelectEntry([]string{"Item 1", "Item 2"})
    entry2.SetPlaceHolder("Select one or type")
    entry2.Resize(fyne.NewSize(200, 40))
    entry2.Move(fyne.NewPos(100, 180))
    cat_img := canvas.NewImageFromURI(storage.NewFileURI("cat.png"))
    cat_img.Resize(fyne.NewSize(100, 100))
    cat_img.Move(fyne.NewPos(cx, cy))
    // entry.OnChanged(x := entry2.Text)
    dog_img := canvas.NewImageFromURI(storage.NewFileURI("dog.png"))
    dog_img.Resize(fyne.NewSize(100, 100))
    dog_img.Move(fyne.NewPos(100, 180))
    down_btn := widget.NewButton("Down", func() {
        // x = float32(x + 10)
        n := float32(y + 10)
        canvas.NewPositionAnimation(
            fyne.NewPos(n, y),
            fyne.NewPos(x, n),
            canvas.DurationStandard,
            func(p fyne.Position) {
                dog_img.Move(p)
                dog_img.Refresh()
            }).Start()
        //dog_img.Move(fyne.NewPos(x, y))
        dog_img.Refresh()
        y = n
    })
    // anim1 := canvas.NewPositionAnimation(
    //  fyne.NewPos(x, y),
    //  fyne.NewPos(x+10, y+10),
    //  canvas.DurationStandard,
    //  func(p fyne.Position) {
    //      dog_img.Move(p)
    //      dog_img.Refresh()
    //  })
    up_btn2 := widget.NewButton("Up", func() {
        // x = float32(x - 10)
        n := float32(y - 10)
        //n := float32(y + 10)
        canvas.NewPositionAnimation(
            fyne.NewPos(n, y),
            fyne.NewPos(x, n),
            canvas.DurationStandard,
            func(p fyne.Position) {
                dog_img.Move(p)
                dog_img.Refresh()
            }).Start()
        //dog_img.Move(fyne.NewPos(x, y))
        dog_img.Refresh()
        //anim1.Start()
        y = n
    })
    right_btn3 := widget.NewButton("Right", func() {
        n := float32(x + 10)
        // y = float32(y - 10)
        //n := float32(y + 10)
        canvas.NewPositionAnimation(
            fyne.NewPos(x, n),
            fyne.NewPos(n, y),
            canvas.DurationStandard,
            func(p fyne.Position) {
                dog_img.Move(p)
                dog_img.Refresh()
            }).Start()
        //dog_img.Move(fyne.NewPos(x, y))
        dog_img.Refresh()
        x = n
    })
    left_btn4 := widget.NewButton("Left", func() {
        n := float32(x - 10)
        // y = float32(y - 10)
        //n := float32(y + 10)
        canvas.NewPositionAnimation(
            fyne.NewPos(x, n),
            fyne.NewPos(n, y),
            canvas.DurationStandard,
            func(p fyne.Position) {
                dog_img.Move(p)
                dog_img.Refresh()
            }).Start()
        dog_img.Move(fyne.NewPos(x, y))
        dog_img.Refresh()
        x = n
    })
    down_btn.Resize(fyne.NewSize(50, 30))
    down_btn.Move(fyne.NewPos(150, 60))
    up_btn2.Resize(fyne.NewSize(50, 30))
    up_btn2.Move(fyne.NewPos(150, 0))
    right_btn3.Resize(fyne.NewSize(50, 30))
    right_btn3.Move(fyne.NewPos(180, 30))
    left_btn4.Resize(fyne.NewSize(50, 30))
    left_btn4.Move(fyne.NewPos(130, 30))
    w.SetContent(container.NewWithoutLayout(
        dog_img, down_btn, up_btn2, right_btn3, left_btn4, cat_img,
    ))
    w.ShowAndRun()
}

 

[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main48.go” text=”Download Code” color=”red_darker”]

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: