apple.go 566 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "math/rand"
  4. "github.com/gdamore/tcell/v2"
  5. )
  6. func placeApple(snake *Snake) Apple {
  7. var x, y int
  8. var found bool
  9. for !found {
  10. x = rand.Intn(80)
  11. y = rand.Intn(24)
  12. if x != snake.head.x && y != snake.head.y {
  13. found = true
  14. }
  15. if x == 0 || x == 79 || y == 0 || y == 23 {
  16. found = false
  17. }
  18. if hitsTail(snake, x, y) {
  19. found = false
  20. }
  21. }
  22. apple := Apple{
  23. x: x,
  24. y: y,
  25. }
  26. return apple
  27. }
  28. func drawApple(screen tcell.Screen, style tcell.Style, apple *Apple) {
  29. screen.SetContent(apple.x, apple.y, 'o', nil, style)
  30. }