Added wasd-game exercise
I am most definitely getting the hang of the Gocui library, this is fun
This commit is contained in:
parent
7d33ba18fc
commit
3eacb27315
|
@ -0,0 +1,11 @@
|
|||
module wasd-game
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require github.com/jroimartin/gocui v0.5.0
|
||||
|
||||
require (
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/nsf/termbox-go v1.1.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
)
|
|
@ -0,0 +1,10 @@
|
|||
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
|
||||
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
||||
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
|
@ -0,0 +1,87 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jroimartin/gocui"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
defer g.Close()
|
||||
|
||||
g.SetManagerFunc(layout)
|
||||
g.Mouse = true
|
||||
g.Cursor = true
|
||||
initKeybindings(g)
|
||||
|
||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||
log.Panicln(err)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
playerX int = 0
|
||||
playerY int = 0
|
||||
)
|
||||
|
||||
func layout(g *gocui.Gui) error {
|
||||
|
||||
maxX, maxY := g.Size()
|
||||
if player, err := g.SetView("player", maxX/2+7+playerX, maxY/2+2-playerY, maxX/2+9+playerX, maxY/2+4-playerY); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
player.Frame = false
|
||||
player.BgColor = gocui.ColorRed
|
||||
fmt.Fprintln(player, "@")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func quit(*gocui.Gui, *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
func initKeybindings(g *gocui.Gui) error {
|
||||
|
||||
var movementStep int = 1
|
||||
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'a', gocui.ModNone,
|
||||
func(g *gocui.Gui, v *gocui.View) error {
|
||||
playerX -= movementStep
|
||||
return nil
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'd', gocui.ModNone,
|
||||
func(g *gocui.Gui, v *gocui.View) error {
|
||||
playerX += movementStep
|
||||
return nil
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 'w', gocui.ModNone,
|
||||
func(g *gocui.Gui, v *gocui.View) error {
|
||||
playerY += movementStep
|
||||
return nil
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("", 's', gocui.ModNone,
|
||||
func(g *gocui.Gui, v *gocui.View) error {
|
||||
playerY -= movementStep
|
||||
return nil
|
||||
}); err != nil {
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue