player.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. func (game *Game) drawPlayer() {
  3. game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
  4. }
  5. func (game *Game) movePlayer(press int) {
  6. if press == Left {
  7. if game.player.position.x != 2 {
  8. game.player.position.x--
  9. game.player.moves++
  10. }
  11. } else if press == Right {
  12. if game.player.position.x != 78 {
  13. game.player.position.x++
  14. game.player.moves++
  15. }
  16. } else if press == Up {
  17. if game.player.position.y != 1 {
  18. game.player.position.y--
  19. game.player.moves++
  20. }
  21. } else if press == Down {
  22. if game.player.position.y != 22 {
  23. game.player.position.y++
  24. game.player.moves++
  25. }
  26. } else if press == upleft {
  27. if game.player.position.x != 2 && game.player.position.y != 1 {
  28. game.player.position.x--
  29. game.player.position.y--
  30. game.player.moves++
  31. }
  32. } else if press == upright {
  33. if game.player.position.x != 78 && game.player.position.y != 1 {
  34. game.player.position.x++
  35. game.player.position.y--
  36. game.player.moves++
  37. }
  38. } else if press == downright {
  39. if game.player.position.x != 78 && game.player.position.y != 22 {
  40. game.player.position.x++
  41. game.player.position.y++
  42. game.player.moves++
  43. }
  44. } else if press == downleft {
  45. if game.player.position.x != 2 && game.player.position.y != 22 {
  46. game.player.position.x--
  47. game.player.position.y++
  48. game.player.moves++
  49. }
  50. } else if press == teleport {
  51. game.teleport()
  52. game.player.teleports++
  53. }
  54. }
  55. func (game *Game) teleport() {
  56. pos := game.randPos()
  57. game.player.position.x = pos.x
  58. game.player.position.y = pos.y
  59. }
  60. func (game *Game) onPlayer(pos Position) bool {
  61. var onPlayer bool
  62. if pos.x == game.player.position.x && pos.y == game.player.position.y {
  63. onPlayer = true
  64. } else {
  65. onPlayer = false
  66. }
  67. return onPlayer
  68. }