Added scoring + apple mechanics

This commit is contained in:
Sacha Ligthert 2023-11-12 16:57:52 +01:00
parent 3fb13a165f
commit b17918f052
4 changed files with 75 additions and 18 deletions

View File

@ -2,13 +2,46 @@ package main
import ( import (
"math/rand" "math/rand"
"github.com/gdamore/tcell/v2"
) )
// getFoodItem returns an emoji from a string used to represent food // getFoodItem returns an emoji from a string used to represent food
// Takes no arguments, but returns an emoji/rune from a predefined string // Takes no arguments, but returns an emoji/rune from a predefined string
func getFoodItem() rune { // func getFoodItem() rune {
var foods string = "🐵🐱🐷🐁🐇🐿🦇🐓🐣🐸🦎🍎🍏" // var foods string = "🐵🐱🐷🐁🐇🐿🦇🐓🐣🐸🦎🍎🍏"
foodsRune := []rune(foods) // foodsRune := []rune(foods)
randomIndex := rand.Intn(len(foodsRune)) // randomIndex := rand.Intn(len(foodsRune))
return foodsRune[randomIndex] // 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)
} }

19
game.go
View File

@ -43,8 +43,8 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
// Destined to die, even before I was born. // Destined to die, even before I was born.
defer quit(screen) defer quit(screen)
//var score int = 0 var score int = 0
//var scoreCounter int = 1 var scoreCounter int = 1
//var field [80][24]int //var field [80][24]int
// fill up the field: // fill up the field:
// 0: empty // 0: empty
@ -52,12 +52,19 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
// 2: apple // 2: apple
// 3: border // 3: border
// Board size
var screenx = 80 var screenx = 80
var screeny = 24 var screeny = 24
// Have snake start at the center of the board
var snakex int = (screenx / 2) - 1 var snakex int = (screenx / 2) - 1
var snakey int = (screeny / 2) - 1 var snakey int = (screeny / 2) - 1
// Last direction we went in
var lastpress int = 1 var lastpress int = 1
var apple Apple = placeApple(&snakex, &snakey)
for { for {
// Take input or sleep // Take input or sleep
@ -69,6 +76,12 @@ func gameDirector(screen tcell.Screen, style tcell.Style, keypresses chan int, g
snakeDirection(lastpress, &snakex, &snakey) 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. // Call it quits once the snake hits the border.
if snakex == 0 || snakex == screenx-1 { if snakex == 0 || snakex == screenx-1 {
close(gamestate) 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) drawBox(screen, 0, 0, screenx-1, screeny-1, style)
drawCoords(screen, style, &snakex, &snakey) drawCoords(screen, style, &snakex, &snakey)
drawSnake(screen, style, snakex, snakey) drawSnake(screen, style, snakex, snakey)
drawApple(screen, style, apple.x, apple.y)
drawScore(screen, style, score)
// Draw the screen // Draw the screen
screen.Show() screen.Show()

View File

@ -4,6 +4,15 @@ import (
"log" "log"
) )
type Position struct {
x int
y int
}
type Apple Position
//type Snake Position
func main() { func main() {
// Create channles to manage keypresses and the gamestate // Create channles to manage keypresses and the gamestate

View File

@ -6,17 +6,17 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
func checkSize(screen tcell.Screen) bool { // func checkSize(screen tcell.Screen) bool {
var retval bool = true // var retval bool = true
x, y := screen.Size() // x, y := screen.Size()
if x < 80 { // if x < 80 {
retval = false // retval = false
} // }
if y < 24 { // if y < 24 {
retval = false // retval = false
} // }
return retval // return retval
} // }
func drawScore(screen tcell.Screen, style tcell.Style, score int) { func drawScore(screen tcell.Screen, style tcell.Style, score int) {
var x, y int = 5, 24 var x, y int = 5, 24