Compare commits

32 Commits

Author SHA1 Message Date
e8d5ecc772 linting + solving linting 2025-03-05 11:44:10 +01:00
07f81679d8 More pre-commit magic. 2024-08-14 21:14:28 +02:00
1eb18e7aeb Disable printing of debug information 2024-08-14 17:17:11 +02:00
69a1c68fb6 Add start of a pre-commit config. 2024-08-14 17:13:38 +02:00
b08e0ea3e8 The (actual) Taskfile definition 2024-08-14 17:02:47 +02:00
34bfeaed0e Replace build script with a Taskfile 2024-08-14 17:01:52 +02: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
15 changed files with 341 additions and 144 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

7
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,7 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0 # this is optional, use `pre-commit autoupdate` to get the latest rev!
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

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.

15
Taskfile.dist.yaml Normal file
View File

@ -0,0 +1,15 @@
version: '3'
tasks:
default:
cmds:
- go run .
build:
cmds:
- mkdir -p builds
- rm builds/* || true
- 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
- for i in `ls builds/*windows*`; do mv -v $i $i.exe; done
lint:
cmds:
- golangci-lint run

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,34 +0,0 @@
package main
import "fmt"
func (game *Game) initRobots() {
var fabricate int = game.level * 16
for i := 0; i < fabricate; i++ {
var found bool
var rndPos Position
for !found {
rndPos := game.randPos()
if !game.onPlayer(rndPos) {
found = true
}
}
game.robots = append(game.robots, rndPos)
}
for _, r := range game.robots {
fmt.Println("xy", r.x, r.y)
}
}
func (game *Game) drawRobots() {
for _, r := range game.robots {
game.screen.SetContent(r.x, r.y, '+', nil, game.style)
}
}
func (game *Game) drawTrash() {
for _, t := range game.trash {
game.screen.SetContent(t.x, t.y, '*', nil, game.style)
}
}

View File

@ -1,4 +1,4 @@
package main package robots
import ( import (
"fmt" "fmt"
@ -8,7 +8,7 @@ import (
"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)
@ -21,12 +21,13 @@ func gameinit() {
// Give closure // Give closure
if game.gameover == 1 { if game.gameover == 1 {
fmt.Println("You get rekt by a robot") fmt.Println("You got rekt by a robot")
} else if game.gameover == 2 { } else if game.gameover == 2 {
fmt.Println("You took the easy way out") fmt.Println("You took the easy way out")
} }
fmt.Println("Score:", game.player.score) fmt.Println("Score:", game.player.score)
fmt.Println("Level: ", game.level)
fmt.Println("Moves:", game.player.moves) fmt.Println("Moves:", game.player.moves)
fmt.Println("Teleports:", game.player.teleports) fmt.Println("Teleports:", game.player.teleports)
@ -61,38 +62,53 @@ func initialize() (Game, error) {
teleports: 0, teleports: 0,
}, },
level: 1, level: 1,
// robots: []Robot, // I need this maybe
// trash: []Position, // I will need this
gameover: 0, gameover: 0,
} }
game.initRobots()
return game, err return game, err
} }
func (game *Game) gameDirector() { func (game *Game) gameDirector() {
defer quit(game) defer quit(game)
for { // TODO reorder this
for game.gameover == 0 {
// Clean the screen // Clean the screen
game.screen.Clear() game.screen.Clear()
// Draw robots??
if len(game.robots) == 0 {
game.level++
game.cleanTrash()
game.resetPlayer()
game.initRobots()
}
// Draw starter conditions // Draw starter conditions
game.drawBox() game.drawBox()
game.drawPlayer() game.drawPlayer()
game.drawCoords() //game.drawCoords()
// Draw robots??
if len(game.robots) == 0 {
game.initRobots()
}
game.drawRobots() 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()
} }
@ -101,7 +117,7 @@ func (game *Game) gameDirector() {
func (game *Game) keyboardProcessor() { 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()
@ -145,7 +161,6 @@ func (game *Game) keyboardProcessor() {
} }
func quit(game *Game) { func quit(game *Game) {
game.screen.Clear()
maybePanic := recover() maybePanic := recover()
game.screen.Fini() game.screen.Fini()
if maybePanic != nil { if maybePanic != nil {
@ -154,19 +169,13 @@ func quit(game *Game) {
} }
func (game *Game) randPos() Position { func (game *Game) randPos() Position {
var safe bool
var pos Position var pos Position
for !safe { x := 2 + rand.Intn(76)
x := rand.Intn(80) y := 2 + rand.Intn(21)
y := rand.Intn(24)
pos.x = x
pos.y = y
if x == 0 || x == 79 || y == 0 || y == 23 {
safe = false
} else {
pos = Position{x: x, y: y}
safe = true
}
}
return pos return pos
} }

View File

@ -1,12 +1,16 @@
package main package robots
import "math/rand"
func (game *Game) drawPlayer() { func (game *Game) drawPlayer() {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style) game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
} }
func (g *Game) movePlayer(game *Game, press int) { func (game *Game) movePlayer(press int) {
if game.blockedByTrash(press) {
game.player.moves++
return
}
if press == Left { if press == Left {
if game.player.position.x != 2 { if game.player.position.x != 2 {
game.player.position.x-- game.player.position.x--
@ -57,10 +61,53 @@ func (g *Game) movePlayer(game *Game, press int) {
} }
} }
// 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() { func (game *Game) teleport() {
// Draw something nice
game.player.position.x = rand.Intn(80) var pos Position
game.player.position.y = rand.Intn(24) 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 { func (game *Game) onPlayer(pos Position) bool {
@ -72,3 +119,8 @@ func (game *Game) onPlayer(pos Position) bool {
} }
return onPlayer return onPlayer
} }
func (game *Game) resetPlayer() {
game.player.position.x = 40
game.player.position.y = 12
}

View File

@ -1,6 +1,8 @@
package main package robots
import "strconv" import (
"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 (game *Game) drawBox() { func (game *Game) drawBox() {
@ -39,10 +41,28 @@ func (game *Game) drawBox() {
} }
// drawCoords Print the coordinates of the player // drawCoords Print the coordinates of the player
func (game *Game) drawCoords() { // 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++
// }
// }
// 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
content := "[ Score: " + strconv.FormatInt(int64(game.player.score), 10) + " Level: " + strconv.FormatInt(int64(game.level), 10) + " ]"
for i := 0; i < len(content); i++ {
game.screen.SetContent(x, y-1, rune(content[i]), nil, game.style)
x++ x++
} }
} }

102
robots/robots.go Normal file
View File

@ -0,0 +1,102 @@
package robots
func (game *Game) initRobots() {
var fabricate int = game.level * 16
for i := 0; i < fabricate; i++ {
var found bool
var rndPos Position
for !found {
rndPos = game.randPos()
if !game.onPlayer(rndPos) {
found = true
}
}
rndRobot := Robot{
id: i,
position: rndPos,
}
game.robots = append(game.robots, rndRobot)
}
}
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() {
// Iterate through the robots
for i, r := range game.robots {
// Determine in which direction to go
if game.player.position.x < r.position.x {
game.robots[i].position.x--
} else if game.player.position.x > r.position.x {
game.robots[i].position.x++
}
if game.player.position.y < r.position.y {
game.robots[i].position.y--
} else if game.player.position.y > r.position.y {
game.robots[i].position.y++
}
}
// 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
for _, r := range game.robots {
if robot != r {
rowboz = append(rowboz, r)
}
}
game.robots = nil
game.robots = rowboz
game.player.score++
}

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
@ -26,17 +28,18 @@ type Player struct {
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 level int
robots []Position robots []Robot
trash []Position trash []Position
gameover int gameover int
} }
type Robot struct {
id int
position Position
}