From 2543cd8e55da77841e17c2af736148762f50d75a Mon Sep 17 00:00:00 2001 From: Sacha Ligthert Date: Fri, 1 Dec 2023 22:41:35 +0100 Subject: [PATCH] Made it able to move --- game.go | 43 ++++++++++++++++++++++++++++--------------- player.go | 32 +++++++++++++++++++++++++++++--- render.go | 4 ++-- types.go | 5 +++++ 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/game.go b/game.go index 5403be7..41ef0e4 100644 --- a/game.go +++ b/game.go @@ -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 } } diff --git a/player.go b/player.go index cac9930..d65db39 100644 --- a/player.go +++ b/player.go @@ -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) } diff --git a/render.go b/render.go index 4995505..0344d32 100644 --- a/render.go +++ b/render.go @@ -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) diff --git a/types.go b/types.go index b6ed8bd..21e57f1 100644 --- a/types.go +++ b/types.go @@ -7,6 +7,11 @@ const ( Left Right Down + upleft + upright + downleft + downright + teleport ) type Position struct {