player.go 1.8 KB

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