player.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "math/rand"
  4. )
  5. func (game *Game) drawPlayer() {
  6. game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
  7. }
  8. func (g *Game) movePlayer(game *Game, press int) {
  9. if press == Left {
  10. if game.player.position.x != 2 {
  11. game.player.position.x--
  12. game.player.moves++
  13. }
  14. } else if press == Right {
  15. if game.player.position.x != 78 {
  16. game.player.position.x++
  17. game.player.moves++
  18. }
  19. } else if press == Up {
  20. if game.player.position.y != 1 {
  21. game.player.position.y--
  22. game.player.moves++
  23. }
  24. } else if press == Down {
  25. if game.player.position.y != 22 {
  26. game.player.position.y++
  27. game.player.moves++
  28. }
  29. } else if press == upleft {
  30. if game.player.position.x != 2 && game.player.position.y != 1 {
  31. game.player.position.x--
  32. game.player.position.y--
  33. game.player.moves++
  34. }
  35. } else if press == upright {
  36. if game.player.position.x != 78 && game.player.position.y != 1 {
  37. game.player.position.x++
  38. game.player.position.y--
  39. game.player.moves++
  40. }
  41. } else if press == downright {
  42. if game.player.position.x != 78 && game.player.position.y != 22 {
  43. game.player.position.x++
  44. game.player.position.y++
  45. game.player.moves++
  46. }
  47. } else if press == downleft {
  48. if game.player.position.x != 2 && game.player.position.y != 22 {
  49. game.player.position.x--
  50. game.player.position.y++
  51. game.player.moves++
  52. }
  53. } else if press == teleport {
  54. game.teleport()
  55. game.player.teleports++
  56. }
  57. }
  58. func (game *Game) teleport() {
  59. // Draw something nice
  60. // Use 1+rand.Intn(77) instead this
  61. var safe bool = false
  62. var x, y int
  63. for !safe {
  64. x = rand.Intn(80)
  65. y = rand.Intn(24)
  66. if x == 0 || x == 79 || y == 0 || y == 23 {
  67. safe = false
  68. } else {
  69. safe = true
  70. }
  71. }
  72. game.player.position.x = x
  73. game.player.position.y = y
  74. }
  75. func (game *Game) onPlayer(pos Position) bool {
  76. var onPlayer bool
  77. if pos.x == game.player.position.x && pos.y == game.player.position.y {
  78. onPlayer = true
  79. } else {
  80. onPlayer = false
  81. }
  82. return onPlayer
  83. }