Compare commits

31 Commits

Author SHA1 Message Date
06aa9b54c7 Attempt to start using Go Routines 2024-01-08 11:12:13 +01:00
76e745801a Bring players closer to the borders. 2023-12-15 00:21:26 +01:00
e1ce42614c Improve build script, ignore the builds/ folder. 2023-12-14 23:42:15 +01:00
5680e2a204 Update the README.md! 🥳 2023-12-11 21:54:50 +01:00
8933597780 Split game into a different module. Closes #1 2023-12-11 21:51:50 +01:00
64e20f36f2 Remove (outdated) comments 2023-12-11 20:49:07 +01:00
1542837738 Resets player position at the start of a new level. Closes #12 2023-12-11 01:04:38 +01:00
5e587bc348 Added a "screenshot". 2023-12-11 00:36:54 +01:00
ec8c3fb8a3 Checks of players are blocked by heaps of trash. Closes #11 2023-12-11 00:33:02 +01:00
d1af011acb Update README.md 2023-12-07 01:25:43 +01:00
134c5edef9 Update the readme (1/2) 2023-12-07 01:18:39 +01:00
d554bf73ef Prevent from TPing into a wall. 2023-12-06 22:30:22 +01:00
227364f79e Prevent users from TPing into trash. 2023-12-06 22:23:47 +01:00
4690e5b1f1 Enable bragging rights. 2023-12-06 20:35:31 +01:00
84a9ee5c52 Refactored how robots are handled. Closes #3 2023-12-06 20:32:08 +01:00
fa37a4740b Cleaning up, followed by start of a rewrite of the deleteRobot() function. 2023-12-06 19:26:06 +01:00
85ca0b0f86 Better method comparing Position's. 2023-12-06 19:25:16 +01:00
adefe7bd20 Disable trash debug for now. 2023-12-06 19:24:46 +01:00
2c7c17e727 Change game-logic a bit. Reduces trash. Addresses #3 and #11. 2023-12-06 19:23:47 +01:00
f0ad3157f3 Move printing around, gives a more accurate result. 2023-12-05 23:49:57 +01:00
063cab6521 Initalizes robots during initialization(), prevents weird behaviour. 2023-12-05 23:29:45 +01:00
a349d221f1 Bring movePlayer() in to line with the rest. 2023-12-05 23:20:58 +01:00
6a5db168e9 Pre-sleep note-to-self 2023-12-04 01:32:04 +01:00
0d7a141830 Clean up of code. 2023-12-04 01:27:19 +01:00
371de84218 Score whenever a robot gets destroyed. Closes #7. 2023-12-04 01:11:33 +01:00
d77d84a891 Simplified the teleport function. Closes #5 2023-12-04 01:04:51 +01:00
2e13d93a53 Fixes #4, additional debug 2023-12-04 01:01:45 +01:00
9aad81f7cd Kept editing until it was in a semi-playable. 2023-12-03 22:20:44 +01:00
51176b0aa3 Lots of changes. Need to check if robots are being made, and why they aren't being printed. 2023-12-02 00:19:33 +01:00
579dcbd41c Added score; Keep track of movement; 2023-12-01 23:08:17 +01:00
ee47329732 Clean up of redundant code. 2023-12-01 23:07:57 +01:00
15 changed files with 474 additions and 169 deletions

24
.gitignore vendored
View File

@ -1,23 +1 @@
# ---> Go builds
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work

View File

@ -2,6 +2,33 @@
A clone of the video game Chase/Robots written in Go. Primary goal is to deepen my knowledge and understanding of the Go programming language. A clone of the video game Chase/Robots written in Go. Primary goal is to deepen my knowledge and understanding of the Go programming language.
```
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ ║
║ + ║
║ ║
║ + ║
║ + + ║
║ + ║
║ + ║
║ ║
║ + ║
║ + ║
║ + @ ║
║ + ║
║ ║
║ ║
║ + ║
║ + ║
║ + ║
║ + ║
║ + ║
║ + ║
║ ║
╚════[ Score: 0 Level: 1 ]═════════════════════════════════════════════════════╝
```
## Gameplay ## Gameplay
* As described here: https://en.wikipedia.org/wiki/Chase_(video_game) * As described here: https://en.wikipedia.org/wiki/Chase_(video_game)
@ -12,3 +39,13 @@ The tool will be written in Go exploring the following:
* Localities * Localities
* Interfaces * Interfaces
* Generics (if possible) * Generics (if possible)
## Phases
### Step 1: Finish the game
* ~~Make the game in a functioning state~~
### Step 2: Split into modules
* ~~Split the game in separate module~~
### Step 3: Go routines
* Put robots in separate go routines and experiment with localities
### Step 4: Beautify the game
* Work on [#8](https://gitea.ligthert.net/golang/rowboz/issues/8), [#9](https://gitea.ligthert.net/golang/rowboz/issues/9), [#10](https://gitea.ligthert.net/golang/rowboz/issues/10) and make it something nice to look at.

10
build.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
mkdir -p builds
rm builds/*
go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o builds/rowboz.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
cd builds
for i in `ls *windows*`; do mv -v $i $i.exe; done

View File

@ -1,26 +0,0 @@
# Design
Gameplay: See https://en.wikipedia.org/wiki/Chase_%28video_game%29
controls: wasd + qezc
Teleport key: t
`@`: player
`+`: drone
`*`: wrecks
Playfield: 80x24 - Display message when windows is too small
Interfaces:
Game
- Screen
- Gamestate
- Styles?
- Player
- Score
- Position
- Moves
- []Robots
- Channel name
- Go routine reference?

12
go.mod
View File

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

21
go.sum
View File

@ -1,11 +1,11 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= 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/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.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y= github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= 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/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.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 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 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@ -13,28 +13,35 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 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/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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 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-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 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/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/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-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-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-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-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.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/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 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.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/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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.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.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/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 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.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/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -1,5 +1,7 @@
package main package main
import "gitea.ligthert.net/golang/rowboz/robots"
func main() { func main() {
gameinit() robots.GameInit()
} }

View File

@ -1,55 +0,0 @@
package main
import "math/rand"
func (g *Game) drawPlayer(game *Game) {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
}
func (g *Game) 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++
}
} else if press == upleft {
if game.player.position.x != 2 && game.player.position.y != 1 {
game.player.position.x--
game.player.position.y--
}
} else if press == upright {
if game.player.position.x != 78 && game.player.position.y != 1 {
game.player.position.x++
game.player.position.y--
}
} else if press == downright {
if game.player.position.x != 78 && game.player.position.y != 22 {
game.player.position.x++
game.player.position.y++
}
} else if press == downleft {
if game.player.position.x != 2 && game.player.position.y != 22 {
game.player.position.x--
game.player.position.y++
}
} else if press == teleport {
game.teleport(game)
}
}
func (g *Game) teleport(game *Game) {
// Draw something nice
game.player.position.x = rand.Intn(80)
game.player.position.y = rand.Intn(24)
}

View File

@ -1,9 +0,0 @@
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)
}

View File

@ -1,25 +1,35 @@
package main package robots
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
func gameinit() { func GameInit() {
game, err := initialize() game, err := initialize()
if err != nil { if err != nil {
log.Fatalln("Initilization error: ", err) log.Fatalln("Initilization error: ", err)
} }
go gameDirector(&game) go game.gameDirector()
keyboardProcessor(&game) game.keyboardProcessor()
// Quit the screen // Quit the screen
quit(&game) quit(&game)
// Give closure // Give closure
fmt.Println("You get rekt lol.") if game.gameover == 1 {
fmt.Println("You got rekt by a robot")
} else if game.gameover == 2 {
fmt.Println("You took the easy way out")
}
fmt.Println("Score:", game.player.score)
fmt.Println("Level: ", game.level)
fmt.Println("Moves:", game.player.moves)
fmt.Println("Teleports:", game.player.teleports)
} }
@ -47,46 +57,66 @@ func initialize() (Game, error) {
x: 40, x: 40,
y: 12, y: 12,
}, },
score: 0,
moves: 0, moves: 0,
teleports: 0, teleports: 0,
}, },
// robots: []Robot, // I need this maybe level: 1,
// trash: []Position, // I will need this
gameover: 0, gameover: 0,
} }
game.initRobots()
return game, err return game, err
} }
func gameDirector(game *Game) { func (game *Game) gameDirector() {
defer quit(game) defer quit(game)
for { for game.gameover == 0 {
// Clean the screen // Clean the screen
game.screen.Clear() game.screen.Clear()
// Draw starter conditions
game.drawBox(game)
game.drawPlayer(game)
game.drawCoords(game)
// Draw robots?? // Draw robots??
if len(game.robots) == 0 {
game.level++
game.cleanTrash()
game.resetPlayer()
game.initRobots()
}
// Draw starter conditions
game.drawBox()
game.drawPlayer()
//game.drawCoords()
game.drawRobots()
// Draw my ex-wife
game.drawTrash()
// Draw stuff last
game.drawScore()
game.drawDebug()
// Draw the screen // Draw the screen
game.screen.Show() game.screen.Show()
// Process input // Process input
game.movePlayer(game, <-game.keypresses) game.movePlayer(<-game.keypresses)
// Move robots
game.moveRobots()
} }
} }
func keyboardProcessor(game *Game) { func (game *Game) keyboardProcessor() {
defer close(game.keypresses) defer close(game.keypresses)
for { for game.gameover == 0 {
// Poll for an event // Poll for an event
ev := game.screen.PollEvent() ev := game.screen.PollEvent()
@ -96,6 +126,7 @@ func keyboardProcessor(game *Game) {
case *tcell.EventKey: case *tcell.EventKey:
// Keys to bug out // Keys to bug out
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC { if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
game.gameover = 2
return return
// Screen management // Screen management
@ -125,11 +156,6 @@ func keyboardProcessor(game *Game) {
game.keypresses <- teleport game.keypresses <- teleport
} }
} }
if game.gameover != 0 {
return
}
} }
} }
@ -140,3 +166,15 @@ func quit(game *Game) {
panic(maybePanic) panic(maybePanic)
} }
} }
func (game *Game) randPos() Position {
var pos Position
x := 1 + rand.Intn(77)
y := 1 + rand.Intn(22)
pos.x = x
pos.y = y
return pos
}

126
robots/player.go Normal file
View File

@ -0,0 +1,126 @@
package robots
func (game *Game) drawPlayer() {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
}
func (game *Game) movePlayer(press int) {
if game.blockedByTrash(press) {
game.player.moves++
return
}
if press == Left {
if game.player.position.x != 1 {
game.player.position.x--
game.player.moves++
}
} else if press == Right {
if game.player.position.x != 78 {
game.player.position.x++
game.player.moves++
}
} else if press == Up {
if game.player.position.y != 1 {
game.player.position.y--
game.player.moves++
}
} else if press == Down {
if game.player.position.y != 22 {
game.player.position.y++
game.player.moves++
}
} else if press == upleft {
if game.player.position.x != 1 && game.player.position.y != 1 {
game.player.position.x--
game.player.position.y--
game.player.moves++
}
} else if press == upright {
if game.player.position.x != 78 && game.player.position.y != 1 {
game.player.position.x++
game.player.position.y--
game.player.moves++
}
} else if press == downright {
if game.player.position.x != 78 && game.player.position.y != 22 {
game.player.position.x++
game.player.position.y++
game.player.moves++
}
} else if press == downleft {
if game.player.position.x != 2 && game.player.position.y != 22 {
game.player.position.x--
game.player.position.y++
game.player.moves++
}
} else if press == teleport {
game.teleport()
game.player.teleports++
}
}
// blockedByTrash() Checks if player can move into a direction and if trash isn't blocking
func (game *Game) blockedByTrash(press int) bool {
var checkPos Position
if press == Left {
checkPos.x = game.player.position.x - 1
checkPos.y = game.player.position.y
} else if press == Right {
checkPos.x = game.player.position.x + 1
checkPos.y = game.player.position.y
} else if press == Up {
checkPos.x = game.player.position.x
checkPos.y = game.player.position.y - 1
} else if press == Down {
checkPos.x = game.player.position.x
checkPos.y = game.player.position.y + 1
} else if press == upleft {
checkPos.x = game.player.position.x - 1
checkPos.y = game.player.position.y - 1
} else if press == upright {
checkPos.x = game.player.position.x + 1
checkPos.y = game.player.position.y - 1
} else if press == downright {
checkPos.x = game.player.position.x + 1
checkPos.y = game.player.position.y + 1
} else if press == downleft {
checkPos.x = game.player.position.x - 1
checkPos.y = game.player.position.y + 1
}
return game.onTrash(checkPos)
}
func (game *Game) teleport() {
var pos Position
var safe bool = false
for !safe {
pos = game.randPos()
if !game.onTrash(pos) {
safe = true
}
}
game.player.position.x = pos.x
game.player.position.y = pos.y
}
func (game *Game) onPlayer(pos Position) bool {
var onPlayer bool
if pos.x == game.player.position.x && pos.y == game.player.position.y {
onPlayer = true
} else {
onPlayer = false
}
return onPlayer
}
func (game *Game) resetPlayer() {
game.player.position.x = 40
game.player.position.y = 12
}

View File

@ -1,22 +1,19 @@
package main package robots
import "strconv" import (
"runtime"
"strconv"
)
// drawBox Draw the outline of the box the snake can move in // drawBox Draw the outline of the box the snake can move in
func (g *Game) drawBox(game *Game) { func (game *Game) drawBox() {
// Assuming we will always have this
x1 := 0 x1 := 0
y1 := 0 y1 := 0
x2 := 79 x2 := 79
y2 := 23 y2 := 23
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
// Fill background // Fill background
for row := y1; row <= y2; row++ { for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ { for col := x1; col <= x2; col++ {
@ -44,11 +41,27 @@ func (g *Game) drawBox(game *Game) {
} }
// drawCoords Print the coordinates of the head of the snake // drawCoords Print the coordinates of the player
func (g *Game) drawCoords(game *Game) { func (game *Game) drawCoords() {
var x, y int = 25, 24 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) + " ]") { 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) game.screen.SetContent(x, y-1, r, nil, game.style)
x++ x++
} }
} }
func (game *Game) drawDebug() {
var x, y int = 40, 24
for _, r := range []rune("[ NumGoRoutine: " + strconv.FormatInt(int64(runtime.NumGoroutine()), 10) + " Bots: " + strconv.FormatInt(int64(len(game.robots)), 10) + " ]") {
game.screen.SetContent(x, y-1, r, nil, game.style)
x++
}
}
func (game *Game) drawScore() {
var x, y int = 5, 24
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(game.player.score), 10) + " Level: " + strconv.FormatInt(int64(game.level), 10) + " ]") {
game.screen.SetContent(x, y-1, r, nil, game.style)
x++
}
}

153
robots/robots.go Normal file
View File

@ -0,0 +1,153 @@
package robots
func (game *Game) initRobots() {
var fabricate int = game.level * 16
for i := 0; i < fabricate; i++ {
var robot Robot
robot.id = i
robot.comms = make(chan Position)
var found bool
var rndPos Position
for !found {
rndPos = game.randPos()
if !game.onPlayer(rndPos) {
found = true
}
}
robot.position = rndPos
game.robots = append(game.robots, robot)
go game.Robot(robot)
}
}
func (game *Game) drawRobots() {
for _, r := range game.robots {
game.screen.SetContent(r.position.x, r.position.y, '+', nil, game.style)
}
}
func (game *Game) moveRobots() {
var robotPos Position
// Iterate through the robots
for i, _ := range game.robots {
game.robots[i].comms <- game.player.position
robotPos = <-game.robots[i].comms
game.robots[i].position = robotPos
}
// After all the moves, lets check if any of the rowboz collided with anything
for _, r := range game.robots {
// Hit a player? Game over!
if game.onPlayer(r.position) {
game.gameover = 1
return
}
// Robots mingling? Trash!
if game.onRobot(r) {
// Delete robot
game.deleteRobot(r)
// Create trash
game.addTrash(r)
}
// Hugging Trash? More trash!
if game.onTrash(r.position) {
// Delete robot
game.deleteRobot(r)
}
}
}
func (game *Game) onRobot(robot Robot) bool {
var found bool = false
for _, r := range game.robots {
if robot.id != r.id && robot.position == r.position {
found = true
}
}
return found
}
func (game *Game) deleteRobot(robot Robot) {
var rowboz []Robot
// Delete from Array
for _, r := range game.robots {
if robot.id != r.id {
rowboz = append(rowboz, r)
}
}
game.robots = nil
game.robots = rowboz
// Close the channel
close(robot.comms)
game.player.score++
}
func (game *Game) Robot(robot Robot) {
var player Position
var robotPos Position = robot.position
var emptyPos Position
for {
// Wait for input from the channel
player = <-robot.comms
if player == emptyPos {
close(robot.comms)
return
}
//fmt.Println(player, robotPos)
// Determine direction to move in
robotPos = game.moveRobot(player, robot.position)
robot.position = robotPos
// Return new position of robot
robot.comms <- robotPos
}
}
func (game *Game) moveRobot(player Position, robot Position) Position {
var newPosition Position
// Determine in which direction to go
if player.x < robot.x {
newPosition.x = robot.x - 1
} else if player.x > robot.x {
newPosition.x = robot.x + 1
}
if player.y < robot.y {
newPosition.y = robot.y - 1
} else if player.y > robot.y {
newPosition.y = robot.y + 1
}
//fmt.Println(player, robot, newPosition)
return newPosition
}

25
robots/trash.go Normal file
View File

@ -0,0 +1,25 @@
package robots
func (game *Game) drawTrash() {
for _, t := range game.trash {
game.screen.SetContent(t.x, t.y, '*', nil, game.style)
}
}
func (game *Game) onTrash(pos Position) bool {
var found bool
for _, t := range game.trash {
if t == pos {
found = true
}
}
return found
}
func (game *Game) addTrash(robot Robot) {
game.trash = append(game.trash, robot.position)
}
func (game *Game) cleanTrash() {
game.trash = nil
}

View File

@ -1,6 +1,8 @@
package main package robots
import "github.com/gdamore/tcell/v2" import (
"github.com/gdamore/tcell/v2"
)
const ( const (
Up = iota Up = iota
@ -21,20 +23,24 @@ type Position struct {
type Player struct { type Player struct {
position Position position Position
score int
moves int moves int
teleports int teleports int
} }
type Robot struct {
position Position
}
type Game struct { type Game struct {
screen tcell.Screen screen tcell.Screen
style tcell.Style style tcell.Style
keypresses chan int keypresses chan int
player Player player Player
level int
robots []Robot robots []Robot
trash []Position trash []Position
gameover int gameover int
} }
type Robot struct {
id int
comms chan Position
position Position
}