Advertisement
Guest User

Untitled

a guest
May 1st, 2025
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.22 KB | Source Code | 0 0
  1. package
  2.  main
  3.  
  4. import
  5.  (
  6.     "fmt"
  7.     "math/rand"
  8. )
  9.  
  10. var
  11.  playerPos = Point{rand.
  12. Intn
  13. (10), rand.
  14. Intn
  15. (10)}
  16. var
  17.  goldPos = Point{rand.
  18. Intn
  19. (10), rand.
  20. Intn
  21. (20) + 20}
  22. var
  23.  snakePos = Point{rand.
  24. Intn
  25. (10), rand.
  26. Intn
  27. (10) + 10}
  28. var
  29.  doorPos = Point{rand.
  30. Intn
  31. (5) + 5, rand.
  32. Intn
  33. (10) + 30}
  34.  
  35. func
  36.  
  37. main
  38. () {
  39.     collectGold := false
  40.     count := 0
  41.  
  42.     fmt.
  43. Println
  44. ("Dein Ziehl ist das Gold (G) einzusameln und dann durch die Tür (T) zu entkommen (Du bist P). Die Schlange (S) versucht dich zu jagen, wenn sie die berüht hast du verloren. Du bewegst dich mit w/a/s/d.Drücke Enter zum starten vom Spiel. Viel Spaß.")
  45. //Englisch: Your goal is to collect the gold (G) and then escape through the door (T) (You are P). The snake (S) tries to chase you, and if it touches you, you lose. You move with w/a/s/d. Press Enter to start the game. Have fun!
  46.  
  47.     fmt.
  48. Scanln
  49. ()
  50.  
  51.    
  52. for
  53.  {
  54.        
  55. print_gamefield
  56. (collectGold)
  57.        
  58. playerMovement
  59. ()
  60.  
  61.        
  62. if
  63.  count%2 == 0 {
  64.            
  65. snakeMovment
  66. ()
  67.         }
  68.  
  69.        
  70. if
  71.  playerPos == snakePos {
  72.             fmt.
  73. Println
  74. ("Du hast verloren ):")
  75. //Englisch: You lose
  76.            
  77. break
  78.         }
  79.  
  80.        
  81. if
  82.  playerPos == goldPos {
  83.             collectGold = true
  84.         }
  85.  
  86.        
  87. if
  88.  playerPos == doorPos && collectGold {
  89.            
  90. print_gamefield
  91. (collectGold)
  92.             fmt.
  93. Println
  94. ("Du hast Gewonnen (:")
  95. //Englisch: You win
  96.            
  97. break
  98.         }
  99.  
  100.         count++
  101.     }
  102. }
  103.  
  104. func
  105.  
  106. print_gamefield
  107. (collectGold bool) {
  108.    
  109. for
  110.  x := 0; x < 10; x++ {
  111.        
  112. for
  113.  y := 0; y < 40; y++ {
  114.  
  115.            
  116. if
  117.  x == playerPos.x && y == playerPos.y {
  118.                 fmt.
  119. Print
  120. (" P ")
  121.             }
  122. else
  123.  
  124. if
  125.  x == goldPos.x && y == goldPos.y {
  126.                
  127. if
  128.  collectGold {
  129.                     fmt.
  130. Print
  131. (" . ")
  132.                 }
  133. else
  134.  {
  135.                     fmt.
  136. Print
  137. (" G ")
  138.                 }
  139.             }
  140. else
  141.  
  142. if
  143.  x == snakePos.x && y == snakePos.y {
  144.                 fmt.
  145. Print
  146. (" S ")
  147.             }
  148. else
  149.  
  150. if
  151.  x == doorPos.x && y == doorPos.y {
  152.                 fmt.
  153. Print
  154. (" D ")
  155.             }
  156. else
  157.  {
  158.                 fmt.
  159. Print
  160. (" . ")
  161.             }
  162.         }
  163.         fmt.
  164. Println
  165. ()
  166.     }
  167. }
  168.  
  169. func
  170.  
  171. playerMovement
  172. () {
  173.    
  174. var
  175.  playerInput string
  176.     fmt.
  177. Scanln
  178. (&playerInput)
  179.  
  180.    
  181. if
  182.  playerInput == "w" {
  183.        
  184. if
  185.  playerPos.x != 0 {
  186.             playerPos.x--
  187.         }
  188.     }
  189. else
  190.  
  191. if
  192.  playerInput == "s" {
  193.        
  194. if
  195.  playerPos.x != 9 {
  196.             playerPos.x++
  197.         }
  198.     }
  199. else
  200.  
  201. if
  202.  playerInput == "a" {
  203.        
  204. if
  205.  playerPos.y != 0 {
  206.             playerPos.y--
  207.         }
  208.     }
  209. else
  210.  
  211. if
  212.  playerInput == "d" {
  213.        
  214. if
  215.  playerPos.y != 39 {
  216.             playerPos.y++
  217.         }
  218.     }
  219. }
  220.  
  221. func
  222.  
  223. snakeMovment
  224. () {
  225.    
  226. if
  227.  playerPos.y > snakePos.y {
  228.         snakePos.y++
  229.     }
  230. else
  231.  
  232. if
  233.  playerPos.y < snakePos.y {
  234.         snakePos.y--
  235.     }
  236. else
  237.  
  238. if
  239.  playerPos.x > snakePos.x {
  240.         snakePos.x++
  241.     }
  242. else
  243.  
  244. if
  245.  playerPos.x < snakePos.x {
  246.         snakePos.x--
  247.     }
  248. }
  249.  
  250. type
  251.  Point
  252. struct
  253.  {
  254.     x int
  255.     y int
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement