snake/apple.go

42 lines
566 B
Go

package main
import (
"math/rand"
"github.com/gdamore/tcell/v2"
)
func placeApple(snake *Snake) Apple {
var x, y int
var found bool
for !found {
x = rand.Intn(80)
y = rand.Intn(24)
if x != snake.head.x && y != snake.head.y {
found = true
}
if x == 0 || x == 79 || y == 0 || y == 23 {
found = false
}
if hitsTail(snake, x, y) {
found = false
}
}
apple := Apple{
x: x,
y: y,
}
return apple
}
func drawApple(screen tcell.Screen, style tcell.Style, apple *Apple) {
screen.SetContent(apple.x, apple.y, 'o', nil, style)
}