Kept editing until it was in a semi-playable.
This commit is contained in:
parent
51176b0aa3
commit
9aad81f7cd
31
game.go
31
game.go
@ -19,11 +19,16 @@ func gameinit() {
|
|||||||
// Quit the screen
|
// Quit the screen
|
||||||
quit(&game)
|
quit(&game)
|
||||||
|
|
||||||
|
fmt.Println("\nDebug time: Trash")
|
||||||
|
for i, t := range game.trash {
|
||||||
|
fmt.Println("Trash: ", i, t)
|
||||||
|
}
|
||||||
|
|
||||||
// Give closure
|
// Give closure
|
||||||
if game.gameover == 1 {
|
if game.gameover == 1 {
|
||||||
fmt.Println("You get rekt by a robot")
|
fmt.Println("\nYou 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("\nYou took the easy way out")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Score:", game.player.score)
|
fmt.Println("Score:", game.player.score)
|
||||||
@ -72,28 +77,38 @@ func initialize() (Game, error) {
|
|||||||
func (game *Game) gameDirector() {
|
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
|
// Draw starter conditions
|
||||||
game.drawBox()
|
game.drawBox()
|
||||||
|
game.drawScore()
|
||||||
game.drawPlayer()
|
game.drawPlayer()
|
||||||
game.drawCoords()
|
//game.drawCoords()
|
||||||
|
//game.drawDebug()
|
||||||
|
|
||||||
// Draw robots??
|
// Draw robots??
|
||||||
if len(game.robots) == 0 {
|
if len(game.robots) == 0 {
|
||||||
|
game.level++
|
||||||
|
game.cleanTrash()
|
||||||
game.initRobots()
|
game.initRobots()
|
||||||
}
|
}
|
||||||
game.drawRobots()
|
game.drawRobots()
|
||||||
|
|
||||||
|
// Draw my ex-wife
|
||||||
|
game.drawTrash()
|
||||||
|
|
||||||
// Draw the screen
|
// Draw the screen
|
||||||
game.screen.Show()
|
game.screen.Show()
|
||||||
|
|
||||||
// Process input
|
// Process input
|
||||||
game.movePlayer(game, <-game.keypresses)
|
game.movePlayer(game, <-game.keypresses)
|
||||||
|
|
||||||
|
// Move robots
|
||||||
|
game.moveRobots()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -101,7 +116,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()
|
||||||
@ -154,7 +169,7 @@ func quit(game *Game) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) randPos() Position {
|
func (game *Game) randPos() Position {
|
||||||
var safe bool
|
var safe bool = false
|
||||||
var pos Position
|
var pos Position
|
||||||
|
|
||||||
for !safe {
|
for !safe {
|
||||||
@ -164,9 +179,11 @@ func (game *Game) randPos() Position {
|
|||||||
if x == 0 || x == 79 || y == 0 || y == 23 {
|
if x == 0 || x == 79 || y == 0 || y == 23 {
|
||||||
safe = false
|
safe = false
|
||||||
} else {
|
} else {
|
||||||
pos = Position{x: x, y: y}
|
pos.x = x
|
||||||
|
pos.y = y
|
||||||
safe = true
|
safe = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return pos
|
return pos
|
||||||
}
|
}
|
||||||
|
24
player.go
24
player.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "math/rand"
|
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)
|
||||||
@ -59,8 +61,24 @@ func (g *Game) movePlayer(game *Game, press int) {
|
|||||||
|
|
||||||
func (game *Game) teleport() {
|
func (game *Game) teleport() {
|
||||||
// Draw something nice
|
// Draw something nice
|
||||||
game.player.position.x = rand.Intn(80)
|
// Use 1+rand.Intn(77) instead this
|
||||||
game.player.position.y = rand.Intn(24)
|
|
||||||
|
var safe bool = false
|
||||||
|
var x, y int
|
||||||
|
|
||||||
|
for !safe {
|
||||||
|
x = rand.Intn(80)
|
||||||
|
y = rand.Intn(24)
|
||||||
|
|
||||||
|
if x == 0 || x == 79 || y == 0 || y == 23 {
|
||||||
|
safe = false
|
||||||
|
} else {
|
||||||
|
safe = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
game.player.position.x = x
|
||||||
|
game.player.position.y = y
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) onPlayer(pos Position) bool {
|
func (game *Game) onPlayer(pos Position) bool {
|
||||||
|
23
render.go
23
render.go
@ -1,6 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
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 (game *Game) drawBox() {
|
func (game *Game) drawBox() {
|
||||||
@ -41,7 +44,23 @@ 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)
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Game) drawDebug() {
|
||||||
|
var x, y int = 40, 24
|
||||||
|
for _, r := range []rune("[ NumGoRoutine: " + strconv.FormatInt(int64(runtime.NumGoroutine()), 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)
|
game.screen.SetContent(x, y-1, r, nil, game.style)
|
||||||
x++
|
x++
|
||||||
}
|
}
|
||||||
|
77
robots.go
77
robots.go
@ -1,24 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func (game *Game) initRobots() {
|
func (game *Game) initRobots() {
|
||||||
var fabricate int = game.level * 16
|
var fabricate int = game.level * 16
|
||||||
for i := 0; i < fabricate; i++ {
|
for i := 0; i < fabricate; i++ {
|
||||||
var found bool
|
var found bool
|
||||||
var rndPos Position
|
var rndPos Position
|
||||||
for !found {
|
for !found {
|
||||||
rndPos := game.randPos()
|
rndPos = game.randPos()
|
||||||
if !game.onPlayer(rndPos) {
|
if !game.onPlayer(rndPos) {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.robots = append(game.robots, rndPos)
|
game.robots = append(game.robots, rndPos)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range game.robots {
|
|
||||||
fmt.Println("xy", r.x, r.y)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) drawRobots() {
|
func (game *Game) drawRobots() {
|
||||||
@ -27,8 +21,71 @@ func (game *Game) drawRobots() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) drawTrash() {
|
func (game *Game) moveRobots() {
|
||||||
for _, t := range game.trash {
|
// Iterate through the robots
|
||||||
game.screen.SetContent(t.x, t.y, '*', nil, game.style)
|
for i, r := range game.robots {
|
||||||
|
|
||||||
|
// Determine in which direction to go
|
||||||
|
if game.player.position.x < r.x {
|
||||||
|
game.robots[i].x--
|
||||||
|
} else if game.player.position.x > r.x {
|
||||||
|
game.robots[i].x++
|
||||||
|
}
|
||||||
|
|
||||||
|
if game.player.position.y < r.y {
|
||||||
|
game.robots[i].y--
|
||||||
|
} else if game.player.position.y > r.y {
|
||||||
|
game.robots[i].y++
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// After all the moves, lets check if any of the rowboz collided with anything
|
||||||
|
for i, r := range game.robots {
|
||||||
|
|
||||||
|
// Check if it collides with anything.
|
||||||
|
if game.onPlayer(r) {
|
||||||
|
game.gameover = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if game.onRobot(i, r) || game.onTrash(r) {
|
||||||
|
|
||||||
|
trash := r
|
||||||
|
|
||||||
|
// delete robot
|
||||||
|
game.deleteRobot(i)
|
||||||
|
|
||||||
|
// create trash
|
||||||
|
game.addTrash(trash)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (game *Game) onRobot(index int, pos Position) bool {
|
||||||
|
var found bool
|
||||||
|
for i, r := range game.robots {
|
||||||
|
if index != i && pos.x == r.x && pos.y == r.y {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: improve this
|
||||||
|
func (game *Game) deleteRobot(robot int) {
|
||||||
|
|
||||||
|
var rowboz []Position
|
||||||
|
|
||||||
|
for i, r := range game.robots {
|
||||||
|
if robot != i {
|
||||||
|
rowboz = append(rowboz, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
game.robots = nil
|
||||||
|
game.robots = rowboz
|
||||||
|
|
||||||
|
}
|
||||||
|
25
trash.go
Normal file
25
trash.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
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.x == pos.x && t.y == pos.y {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Game) addTrash(pos Position) {
|
||||||
|
game.trash = append(game.trash, pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (game *Game) cleanTrash() {
|
||||||
|
game.trash = nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user