diff --git a/apple.go b/apple.go index 911f4e4..21d0963 100644 --- a/apple.go +++ b/apple.go @@ -2,13 +2,46 @@ package main import ( "math/rand" + + "github.com/gdamore/tcell/v2" ) // getFoodItem returns an emoji from a string used to represent food // Takes no arguments, but returns an emoji/rune from a predefined string -func getFoodItem() rune { - var foods string = "πŸ΅πŸ±πŸ·πŸπŸ‡πŸΏπŸ¦‡πŸ“πŸ£πŸΈπŸ¦ŽπŸŽπŸ" - foodsRune := []rune(foods) - randomIndex := rand.Intn(len(foodsRune)) - return foodsRune[randomIndex] +// func getFoodItem() rune { +// var foods string = "πŸ΅πŸ±πŸ·πŸπŸ‡πŸΏπŸ¦‡πŸ“πŸ£πŸΈπŸ¦ŽπŸŽπŸ" +// foodsRune := []rune(foods) +// randomIndex := rand.Intn(len(foodsRune)) +// return foodsRune[randomIndex] +// } + +func placeApple(snakex *int, snakey *int) Apple { + var x, y int + var found bool + + for !found { + + x = rand.Intn(80) + y = rand.Intn(24) + + if x != *snakex && y != *snakey { + found = true + } + if x == 0 || x == 79 || y == 0 || y == 23 { + found = false + } + + } + + apple := Apple{ + x: x, + y: y, + } + + return apple + +} + +func drawApple(screen tcell.Screen, style tcell.Style, x int, y int) { + screen.SetContent(x, y, 'ο£Ώ', nil, style) } diff --git a/game.go b/game.go index 7e9d8b4..f473a26 100644 --- a/game.go +++ b/game.go @@ -43,8 +43,8 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g // Destined to die, even before I was born. defer quit(screen) - //var score int = 0 - //var scoreCounter int = 1 + var score int = 0 + var scoreCounter int = 1 //var field [80][24]int // fill up the field: // 0: empty @@ -52,12 +52,19 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g // 2: apple // 3: border + // Board size var screenx = 80 var screeny = 24 + + // Have snake start at the center of the board var snakex int = (screenx / 2) - 1 var snakey int = (screeny / 2) - 1 + + // Last direction we went in var lastpress int = 1 + var apple Apple = placeApple(&snakex, &snakey) + for { // Take input or sleep @@ -69,6 +76,12 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g snakeDirection(lastpress, &snakex, &snakey) } + if snakex == apple.x && snakey == apple.y { + apple = placeApple(&snakex, &snakey) + score += scoreCounter + scoreCounter++ + } + // Call it quits once the snake hits the border. if snakex == 0 || snakex == screenx-1 { close(gamestate) @@ -85,6 +98,8 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g drawBox(screen, 0, 0, screenx-1, screeny-1, style) drawCoords(screen, style, &snakex, &snakey) drawSnake(screen, style, snakex, snakey) + drawApple(screen, style, apple.x, apple.y) + drawScore(screen, style, score) // Draw the screen screen.Show() diff --git a/main.go b/main.go index e5273d0..39d10c6 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,15 @@ import ( "log" ) +type Position struct { + x int + y int +} + +type Apple Position + +//type Snake Position + func main() { // Create channles to manage keypresses and the gamestate diff --git a/render.go b/render.go index f74083b..358b51f 100644 --- a/render.go +++ b/render.go @@ -6,17 +6,17 @@ import ( "github.com/gdamore/tcell/v2" ) -func checkSize(screen tcell.Screen) bool { - var retval bool = true - x, y := screen.Size() - if x < 80 { - retval = false - } - if y < 24 { - retval = false - } - return retval -} +// func checkSize(screen tcell.Screen) bool { +// var retval bool = true +// x, y := screen.Size() +// if x < 80 { +// retval = false +// } +// if y < 24 { +// retval = false +// } +// return retval +// } func drawScore(screen tcell.Screen, style tcell.Style, score int) { var x, y int = 5, 24