Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- main
- import
- (
- "fmt"
- "math/rand"
- )
- var
- playerPos = Point{rand.
- Intn
- (10), rand.
- Intn
- (10)}
- var
- goldPos = Point{rand.
- Intn
- (10), rand.
- Intn
- (20) + 20}
- var
- snakePos = Point{rand.
- Intn
- (10), rand.
- Intn
- (10) + 10}
- var
- doorPos = Point{rand.
- Intn
- (5) + 5, rand.
- Intn
- (10) + 30}
- func
- main
- () {
- collectGold := false
- count := 0
- fmt.
- Println
- ("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ß.")
- //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!
- fmt.
- Scanln
- ()
- for
- {
- print_gamefield
- (collectGold)
- playerMovement
- ()
- if
- count%2 == 0 {
- snakeMovment
- ()
- }
- if
- playerPos == snakePos {
- fmt.
- Println
- ("Du hast verloren ):")
- //Englisch: You lose
- break
- }
- if
- playerPos == goldPos {
- collectGold = true
- }
- if
- playerPos == doorPos && collectGold {
- print_gamefield
- (collectGold)
- fmt.
- Println
- ("Du hast Gewonnen (:")
- //Englisch: You win
- break
- }
- count++
- }
- }
- func
- print_gamefield
- (collectGold bool) {
- for
- x := 0; x < 10; x++ {
- for
- y := 0; y < 40; y++ {
- if
- x == playerPos.x && y == playerPos.y {
- fmt.
- Print
- (" P ")
- }
- else
- if
- x == goldPos.x && y == goldPos.y {
- if
- collectGold {
- fmt.
- Print
- (" . ")
- }
- else
- {
- fmt.
- Print
- (" G ")
- }
- }
- else
- if
- x == snakePos.x && y == snakePos.y {
- fmt.
- Print
- (" S ")
- }
- else
- if
- x == doorPos.x && y == doorPos.y {
- fmt.
- Print
- (" D ")
- }
- else
- {
- fmt.
- Print
- (" . ")
- }
- }
- fmt.
- Println
- ()
- }
- }
- func
- playerMovement
- () {
- var
- playerInput string
- fmt.
- Scanln
- (&playerInput)
- if
- playerInput == "w" {
- if
- playerPos.x != 0 {
- playerPos.x--
- }
- }
- else
- if
- playerInput == "s" {
- if
- playerPos.x != 9 {
- playerPos.x++
- }
- }
- else
- if
- playerInput == "a" {
- if
- playerPos.y != 0 {
- playerPos.y--
- }
- }
- else
- if
- playerInput == "d" {
- if
- playerPos.y != 39 {
- playerPos.y++
- }
- }
- }
- func
- snakeMovment
- () {
- if
- playerPos.y > snakePos.y {
- snakePos.y++
- }
- else
- if
- playerPos.y < snakePos.y {
- snakePos.y--
- }
- else
- if
- playerPos.x > snakePos.x {
- snakePos.x++
- }
- else
- if
- playerPos.x < snakePos.x {
- snakePos.x--
- }
- }
- type
- Point
- struct
- {
- x int
- y int
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement