Compare commits
No commits in common. "51176b0aa31e461d289ae4c075fb0a04461c86d1" and "2543cd8e55da77841e17c2af736148762f50d75a" have entirely different histories.
51176b0aa3
...
2543cd8e55
56
game.go
56
game.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
@ -13,22 +12,14 @@ func gameinit() {
|
||||
if err != nil {
|
||||
log.Fatalln("Initilization error: ", err)
|
||||
}
|
||||
go game.gameDirector()
|
||||
game.keyboardProcessor()
|
||||
go gameDirector(&game)
|
||||
keyboardProcessor(&game)
|
||||
|
||||
// Quit the screen
|
||||
quit(&game)
|
||||
|
||||
// Give closure
|
||||
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)
|
||||
fmt.Println("You get rekt lol.")
|
||||
|
||||
}
|
||||
|
||||
@ -56,11 +47,9 @@ func initialize() (Game, error) {
|
||||
x: 40,
|
||||
y: 12,
|
||||
},
|
||||
score: 0,
|
||||
moves: 0,
|
||||
teleports: 0,
|
||||
},
|
||||
level: 1,
|
||||
// robots: []Robot, // I need this maybe
|
||||
// trash: []Position, // I will need this
|
||||
gameover: 0,
|
||||
@ -69,7 +58,7 @@ func initialize() (Game, error) {
|
||||
return game, err
|
||||
}
|
||||
|
||||
func (game *Game) gameDirector() {
|
||||
func gameDirector(game *Game) {
|
||||
defer quit(game)
|
||||
|
||||
for {
|
||||
@ -78,15 +67,11 @@ func (game *Game) gameDirector() {
|
||||
game.screen.Clear()
|
||||
|
||||
// Draw starter conditions
|
||||
game.drawBox()
|
||||
game.drawPlayer()
|
||||
game.drawCoords()
|
||||
game.drawBox(game)
|
||||
game.drawPlayer(game)
|
||||
game.drawCoords(game)
|
||||
|
||||
// Draw robots??
|
||||
if len(game.robots) == 0 {
|
||||
game.initRobots()
|
||||
}
|
||||
game.drawRobots()
|
||||
|
||||
// Draw the screen
|
||||
game.screen.Show()
|
||||
@ -98,7 +83,7 @@ func (game *Game) gameDirector() {
|
||||
|
||||
}
|
||||
|
||||
func (game *Game) keyboardProcessor() {
|
||||
func keyboardProcessor(game *Game) {
|
||||
defer close(game.keypresses)
|
||||
|
||||
for {
|
||||
@ -111,7 +96,6 @@ func (game *Game) keyboardProcessor() {
|
||||
case *tcell.EventKey:
|
||||
// Keys to bug out
|
||||
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
||||
game.gameover = 2
|
||||
return
|
||||
|
||||
// Screen management
|
||||
@ -141,32 +125,18 @@ func (game *Game) keyboardProcessor() {
|
||||
game.keypresses <- teleport
|
||||
}
|
||||
}
|
||||
|
||||
if game.gameover != 0 {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func quit(game *Game) {
|
||||
game.screen.Clear()
|
||||
maybePanic := recover()
|
||||
game.screen.Fini()
|
||||
if maybePanic != nil {
|
||||
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
|
||||
}
|
||||
|
25
player.go
25
player.go
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import "math/rand"
|
||||
|
||||
func (game *Game) drawPlayer() {
|
||||
func (g *Game) drawPlayer(game *Game) {
|
||||
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
|
||||
}
|
||||
|
||||
@ -10,65 +10,46 @@ func (g *Game) movePlayer(game *Game, press int) {
|
||||
if press == Left {
|
||||
if game.player.position.x != 2 {
|
||||
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 != 2 && 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++
|
||||
game.teleport(game)
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Game) teleport() {
|
||||
func (g *Game) teleport(game *Game) {
|
||||
// Draw something nice
|
||||
game.player.position.x = rand.Intn(80)
|
||||
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
|
||||
}
|
||||
|
14
render.go
14
render.go
@ -3,14 +3,20 @@ package main
|
||||
import "strconv"
|
||||
|
||||
// drawBox Draw the outline of the box the snake can move in
|
||||
func (game *Game) drawBox() {
|
||||
func (g *Game) drawBox(game *Game) {
|
||||
|
||||
// Assuming we will always have this
|
||||
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++ {
|
||||
@ -38,8 +44,8 @@ func (game *Game) drawBox() {
|
||||
|
||||
}
|
||||
|
||||
// drawCoords Print the coordinates of the player
|
||||
func (game *Game) drawCoords() {
|
||||
// drawCoords Print the coordinates of the head of the snake
|
||||
func (g *Game) 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)
|
||||
|
33
robots.go
33
robots.go
@ -1,34 +1,9 @@
|
||||
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 drawRobot(game *Game) {
|
||||
game.screen.SetContent(game.player.position.x, game.player.position.y, '+', nil, game.style)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
func drawTrash(game *Game) {
|
||||
game.screen.SetContent(game.player.position.x, game.player.position.y, '*', nil, game.style)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user