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 (
"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)
}

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.
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()

View File

@ -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

View File

@ -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