Show at least something. It is a start.

This commit is contained in:
Sacha Ligthert 2023-12-01 21:57:52 +01:00
parent 1b81e0b9a5
commit 86e96a1062
12 changed files with 309 additions and 27 deletions

View File

@ -1,3 +0,0 @@
package bots
// This is the actor that controls the bots

129
game.go Normal file
View File

@ -0,0 +1,129 @@
package main
import (
"fmt"
"log"
"github.com/gdamore/tcell/v2"
)
func gameinit() {
game, err := initialize()
if err != nil {
log.Fatalln("Initilization error: ", err)
}
go gameDirector(&game)
keyboardProcessor(&game)
// Quit the screen
quit(&game)
// Give closure
fmt.Println("You get rekt lol.")
}
func initialize() (Game, error) {
var err error
screen, err := tcell.NewScreen()
if err != nil {
log.Println("Error creating screen: ", err)
}
err = screen.Init()
if err != nil {
log.Fatalln("Error initializing screen: ", err)
}
style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
game := Game{
screen: screen,
style: style,
keypresses: make(chan int),
player: Player{
position: Position{
x: 40,
y: 12,
},
moves: 0,
teleports: 0,
},
// robots: []Robot, // I need this maybe
// trash: []Position, // I will need this
gameover: 0,
}
return game, err
}
func gameDirector(game *Game) {
defer quit(game)
for {
// Clean the screen
game.screen.Clear()
// Draw starter conditions
drawBox(game)
drawPlayer(game)
drawCoords(game)
// Draw robots??
// Draw the screen
game.screen.Show()
// Process input
movePlayer(game, <-game.keypresses)
}
}
func keyboardProcessor(game *Game) {
defer close(game.keypresses)
for {
// Poll for an event
ev := game.screen.PollEvent()
// Fetch the type, and check if any other actions are required.
switch ev := ev.(type) {
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
return
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
return
} else if ev.Key() == tcell.KeyCtrlL {
game.screen.Sync()
} else if ev.Rune() == 'C' || ev.Rune() == 'c' {
game.screen.Clear()
} else if ev.Key() == tcell.KeyLeft {
game.keypresses <- Left
} else if ev.Key() == tcell.KeyRight {
game.keypresses <- Right
} else if ev.Key() == tcell.KeyDown {
game.keypresses <- Down
} else if ev.Key() == tcell.KeyUp {
game.keypresses <- Up
}
}
if game.gameover != 0 {
return
}
}
}
func quit(game *Game) {
maybePanic := recover()
game.screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
}

View File

@ -1,7 +0,0 @@
package game
import "fmt"
func init() {
fmt.Println("This is a thing!")
}

12
go.mod
View File

@ -1,3 +1,15 @@
module gitea.ligthert.net/golang/rowboz
go 1.21.4
require github.com/gdamore/tcell/v2 v2.6.0
require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)

40
go.sum
View File

@ -0,0 +1,40 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -1,10 +1,5 @@
package main
import (
"gitea.ligthert.net/golang/rowboz/game"
)
func main() {
game.init()
game.director()
gameinit()
}

29
player.go Normal file
View File

@ -0,0 +1,29 @@
package main
func drawPlayer(game *Game) {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
}
func movePlayer(game *Game, press int) {
if press == Left {
if game.player.position.x != 2 {
game.player.position.x--
}
} else if press == Right {
if game.player.position.x != 78 {
game.player.position.x++
}
} else if press == Up {
if game.player.position.y != 1 {
game.player.position.y--
}
} else if press == Down {
if game.player.position.y != 22 {
game.player.position.y++
}
}
}
func telePort(game *Game) {
// Draw something nice
}

54
render.go Normal file
View File

@ -0,0 +1,54 @@
package main
import "strconv"
// drawBox Draw the outline of the box the snake can move in
func drawBox(game *Game) {
x1 := 0
y1 := 0
x2 := 79
y2 := 23
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
// Fill background
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
game.screen.SetContent(col, row, ' ', nil, game.style)
}
}
// Draw borders
for col := x1; col <= x2; col++ {
game.screen.SetContent(col, y1, '═', nil, game.style)
game.screen.SetContent(col, y2, '═', nil, game.style)
}
for row := y1 + 1; row < y2; row++ {
game.screen.SetContent(x1, row, '║', nil, game.style)
game.screen.SetContent(x2, row, '║', nil, game.style)
}
// Only draw corners if necessary
if y1 != y2 && x1 != x2 {
game.screen.SetContent(x1, y1, '╔', nil, game.style)
game.screen.SetContent(x2, y1, '╗', nil, game.style)
game.screen.SetContent(x1, y2, '╚', nil, game.style)
game.screen.SetContent(x2, y2, '╝', nil, game.style)
}
}
// drawCoords Print the coordinates of the head of the snake
func drawCoords(game *Game) {
var x, y int = 25, 24
for _, r := range []rune("[ x:" + strconv.FormatInt(int64(game.player.position.x), 10) + " y: " + strconv.FormatInt(int64(game.player.position.y), 10) + " ]") {
game.screen.SetContent(x, y-1, r, nil, game.style)
x++
}
}

9
robots.go Normal file
View File

@ -0,0 +1,9 @@
package main
func drawRobot(game *Game) {
game.screen.SetContent(game.player.position.x, game.player.position.y, '+', nil, game.style)
}
func drawTrash(game *Game) {
game.screen.SetContent(game.player.position.x, game.player.position.y, '*', nil, game.style)
}

35
types.go Normal file
View File

@ -0,0 +1,35 @@
package main
import "github.com/gdamore/tcell/v2"
const (
Up = iota
Left
Right
Down
)
type Position struct {
x int
y int
}
type Player struct {
position Position
moves int
teleports int
}
type Robot struct {
position Position
}
type Game struct {
screen tcell.Screen
style tcell.Style
keypresses chan int
player Player
robots []Robot
trash []Position
gameover int
}

View File

@ -1,6 +0,0 @@
package types
type Position struct {
x int
y int
}

View File

@ -1,5 +0,0 @@
package types
type Robot struct {
position Position
}