Made it able to move

This commit is contained in:
Sacha Ligthert 2023-12-01 22:41:35 +01:00
parent 86e96a1062
commit 2543cd8e55
4 changed files with 64 additions and 20 deletions

43
game.go
View File

@ -67,9 +67,9 @@ func gameDirector(game *Game) {
game.screen.Clear()
// Draw starter conditions
drawBox(game)
drawPlayer(game)
drawCoords(game)
game.drawBox(game)
game.drawPlayer(game)
game.drawCoords(game)
// Draw robots??
@ -77,7 +77,7 @@ func gameDirector(game *Game) {
game.screen.Show()
// Process input
movePlayer(game, <-game.keypresses)
game.movePlayer(game, <-game.keypresses)
}
@ -94,22 +94,35 @@ func keyboardProcessor(game *Game) {
// Fetch the type, and check if any other actions are required.
switch ev := ev.(type) {
case *tcell.EventKey:
// Keys to bug out
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
return
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
return
// Screen management
} else if ev.Key() == tcell.KeyCtrlL {
game.screen.Sync()
} else if ev.Rune() == 'C' || ev.Rune() == 'c' {
game.screen.Clear()
} else if ev.Key() == tcell.KeyLeft {
game.keypresses <- Left
} else if ev.Key() == tcell.KeyRight {
game.keypresses <- Right
} else if ev.Key() == tcell.KeyDown {
game.keypresses <- Down
} else if ev.Key() == tcell.KeyUp {
// Player Movement
} else if ev.Rune() == 'q' {
game.keypresses <- upleft
} else if ev.Rune() == 'e' {
game.keypresses <- upright
} else if ev.Rune() == 'z' {
game.keypresses <- downleft
} else if ev.Rune() == 'c' {
game.keypresses <- downright
} else if ev.Key() == tcell.KeyUp || ev.Rune() == 'w' {
game.keypresses <- Up
} else if ev.Key() == tcell.KeyRight || ev.Rune() == 'd' {
game.keypresses <- Right
} else if ev.Key() == tcell.KeyDown || ev.Rune() == 's' || ev.Rune() == 'x' {
game.keypresses <- Down
} else if ev.Key() == tcell.KeyLeft || ev.Rune() == 'a' {
game.keypresses <- Left
// Teleport the player
} else if ev.Rune() == 't' {
game.keypresses <- teleport
}
}

View File

@ -1,10 +1,12 @@
package main
func drawPlayer(game *Game) {
import "math/rand"
func (g *Game) drawPlayer(game *Game) {
game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
}
func movePlayer(game *Game, press int) {
func (g *Game) movePlayer(game *Game, press int) {
if press == Left {
if game.player.position.x != 2 {
game.player.position.x--
@ -21,9 +23,33 @@ func movePlayer(game *Game, press int) {
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 telePort(game *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

@ -3,7 +3,7 @@ package main
import "strconv"
// drawBox Draw the outline of the box the snake can move in
func drawBox(game *Game) {
func (g *Game) drawBox(game *Game) {
x1 := 0
y1 := 0
@ -45,7 +45,7 @@ func drawBox(game *Game) {
}
// drawCoords Print the coordinates of the head of the snake
func drawCoords(game *Game) {
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)

View File

@ -7,6 +7,11 @@ const (
Left
Right
Down
upleft
upright
downleft
downright
teleport
)
type Position struct {