apple.go 874 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. var x, y int
  16. var found bool
  17. for !found {
  18. x = rand.Intn(80)
  19. y = rand.Intn(24)
  20. if x != *snakex && y != *snakey {
  21. found = true
  22. }
  23. if x == 0 || x == 79 || y == 0 || y == 23 {
  24. found = false
  25. }
  26. }
  27. apple := Apple{
  28. x: x,
  29. y: y,
  30. }
  31. return apple
  32. }
  33. func drawApple(screen tcell.Screen, style tcell.Style, x int, y int) {
  34. screen.SetContent(x, y, '', nil, style)
  35. }