snake/apple.go

42 lines
566 B
Go
Raw Permalink Normal View History

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