Lots of changes. Need to check if robots are being made, and why they aren't being printed.
This commit is contained in:
parent
579dcbd41c
commit
51176b0aa3
55
game.go
55
game.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
)
|
)
|
||||||
@ -12,14 +13,22 @@ func gameinit() {
|
|||||||
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 get 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("Moves:", game.player.moves)
|
||||||
|
fmt.Println("Teleports:", game.player.teleports)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +60,7 @@ func initialize() (Game, error) {
|
|||||||
moves: 0,
|
moves: 0,
|
||||||
teleports: 0,
|
teleports: 0,
|
||||||
},
|
},
|
||||||
|
level: 1,
|
||||||
// robots: []Robot, // I need this maybe
|
// robots: []Robot, // I need this maybe
|
||||||
// trash: []Position, // I will need this
|
// trash: []Position, // I will need this
|
||||||
gameover: 0,
|
gameover: 0,
|
||||||
@ -59,7 +69,7 @@ func initialize() (Game, error) {
|
|||||||
return game, err
|
return game, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func gameDirector(game *Game) {
|
func (game *Game) gameDirector() {
|
||||||
defer quit(game)
|
defer quit(game)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -68,11 +78,15 @@ func gameDirector(game *Game) {
|
|||||||
game.screen.Clear()
|
game.screen.Clear()
|
||||||
|
|
||||||
// Draw starter conditions
|
// Draw starter conditions
|
||||||
game.drawBox(game)
|
game.drawBox()
|
||||||
game.drawPlayer(game)
|
game.drawPlayer()
|
||||||
game.drawCoords(game)
|
game.drawCoords()
|
||||||
|
|
||||||
// Draw robots??
|
// Draw robots??
|
||||||
|
if len(game.robots) == 0 {
|
||||||
|
game.initRobots()
|
||||||
|
}
|
||||||
|
game.drawRobots()
|
||||||
|
|
||||||
// Draw the screen
|
// Draw the screen
|
||||||
game.screen.Show()
|
game.screen.Show()
|
||||||
@ -84,7 +98,7 @@ func gameDirector(game *Game) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyboardProcessor(game *Game) {
|
func (game *Game) keyboardProcessor() {
|
||||||
defer close(game.keypresses)
|
defer close(game.keypresses)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -97,6 +111,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
|
||||||
@ -126,18 +141,32 @@ func keyboardProcessor(game *Game) {
|
|||||||
game.keypresses <- teleport
|
game.keypresses <- teleport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if game.gameover != 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
panic(maybePanic)
|
panic(maybePanic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (game *Game) randPos() Position {
|
||||||
|
var safe bool
|
||||||
|
var pos Position
|
||||||
|
|
||||||
|
for !safe {
|
||||||
|
x := rand.Intn(80)
|
||||||
|
y := rand.Intn(24)
|
||||||
|
|
||||||
|
if x == 0 || x == 79 || y == 0 || y == 23 {
|
||||||
|
safe = false
|
||||||
|
} else {
|
||||||
|
pos = Position{x: x, y: y}
|
||||||
|
safe = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pos
|
||||||
|
}
|
||||||
|
16
player.go
16
player.go
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import "math/rand"
|
import "math/rand"
|
||||||
|
|
||||||
func (g *Game) drawPlayer(game *Game) {
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,13 +52,23 @@ func (g *Game) movePlayer(game *Game, press int) {
|
|||||||
game.player.moves++
|
game.player.moves++
|
||||||
}
|
}
|
||||||
} else if press == teleport {
|
} else if press == teleport {
|
||||||
game.teleport(game)
|
game.teleport()
|
||||||
game.player.teleports++
|
game.player.teleports++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) teleport(game *Game) {
|
func (game *Game) teleport() {
|
||||||
// Draw something nice
|
// Draw something nice
|
||||||
game.player.position.x = rand.Intn(80)
|
game.player.position.x = rand.Intn(80)
|
||||||
game.player.position.y = rand.Intn(24)
|
game.player.position.y = rand.Intn(24)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ package main
|
|||||||
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 (g *Game) drawBox(game *Game) {
|
func (game *Game) drawBox() {
|
||||||
|
|
||||||
// Assuming we will always have this
|
// Assuming we will always have this
|
||||||
x1 := 0
|
x1 := 0
|
||||||
@ -38,8 +38,8 @@ 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)
|
||||||
|
33
robots.go
33
robots.go
@ -1,9 +1,34 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func drawRobot(game *Game) {
|
import "fmt"
|
||||||
game.screen.SetContent(game.player.position.x, game.player.position.y, '+', nil, game.style)
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTrash(game *Game) {
|
for _, r := range game.robots {
|
||||||
game.screen.SetContent(game.player.position.x, game.player.position.y, '*', nil, game.style)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user