apple.go 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "math/rand"
  4. "github.com/gdamore/tcell/v2"
  5. )
  6. // getFoodItem returns an emoji from a string used to represent food
  7. // Takes no arguments, but returns an emoji/rune from a predefined string
  8. // func getFoodItem() rune {
  9. // var foods string = "🐵🐱🐷🐁🐇🐿🦇🐓🐣🐸🦎🍎🍏"
  10. // foodsRune := []rune(foods)
  11. // randomIndex := rand.Intn(len(foodsRune))
  12. // return foodsRune[randomIndex]
  13. // }
  14. // func placeApple(snakex int, snakey int) Apple {
  15. func placeApple(snake *Snake) Apple {
  16. var x, y int
  17. var found bool
  18. for !found {
  19. x = rand.Intn(80)
  20. y = rand.Intn(24)
  21. if x != snake.head.x && y != snake.head.y {
  22. found = true
  23. }
  24. if x == 0 || x == 79 || y == 0 || y == 23 {
  25. found = false
  26. }
  27. if hitsTail(snake, x, y) {
  28. found = false
  29. }
  30. }
  31. apple := Apple{
  32. x: x,
  33. y: y,
  34. }
  35. return apple
  36. }
  37. func drawApple(screen tcell.Screen, style tcell.Style, apple *Apple) {
  38. screen.SetContent(apple.x, apple.y, 'o', nil, style)
  39. }