Gra tekstowa polegająca na poszukiwaniu skarbu na dwuwymiarowej planszy

0

Napisz grę tekstową polegającą na poszukiwaniu skarbu na dwuwymiarowej planszy o
rozmiarach 10 na 10.
Program na początku losuje pozycję skarbu oraz pozycję gracza.
Następnie użytkownik może wprowadzać komendy zmieniające położenie postaci o jedną
pozycję w górę/dół/lewo/prawo (np zgodnie z konwencją WSAD) – normalnie za pomocą input.
Gdy gracz wejdzie na pole, na którym kryje się skarb – wygrywa.
Gdy wyjdzie poza planszę – przegrywa.
Po każdym ruchu użytkownik powinien otrzymywać informację o tym, czy zmierza w dobrym
kierunku. Po znalezieniu skarbu wypisz liczbę ruchów wykorzystanych przez użytkownika na
dojście do celu.

5

Ok, co już masz. Z czym problem? Bo jeśli chcesz gotowca to zły dział i nie podałeś stawki

3

Inicjalizacja planszy: Stwórz dwuwymiarową tablicę o wymiarach 10x10. Możesz to zrobić za pomocą listy list w Pythonie.

plansza = [[0]*10 for _ in range(10)]

Losowanie pozycji skarbu i gracza: Użyj funkcji randint z modułu random do losowania pozycji skarbu i gracza. Upewnij się, że te dwie pozycje nie są takie same.

import random
pozycja_gracza = [random.randint(0, 9), random.randint(0, 9)]
pozycja_skarbu = [random.randint(0, 9), random.randint(0, 9)]

Pętla gry: Stwórz pętlę while, która będzie trwać dopóki gracz nie znajdzie skarbu lub nie wyjdzie poza planszę.

while True:
    # kod gry

Starczy, dalej pokaż, czy w ogóle coś umiesz. Jeżeli jesteś leniwym studentem, który w ogóle się na tym nie zna, to szukaj kogoś za kasę, jak już napisano powyżej.

0
frz napisał(a):

Inicjalizacja planszy: Stwórz dwuwymiarową tablicę o wymiarach 10x10. Możesz to zrobić za pomocą listy list w Pythonie.

plansza = [[0]*10 for _ in range(10)]

Losowanie pozycji skarbu i gracza: Użyj funkcji randint z modułu random do losowania pozycji skarbu i gracza. Upewnij się, że te dwie pozycje nie są takie same.

import random
pozycja_gracza = [random.randint(0, 9), random.randint(0, 9)]
pozycja_skarbu = [random.randint(0, 9), random.randint(0, 9)]

Pętla gry: Stwórz pętlę while, która będzie trwać dopóki gracz nie znajdzie skarbu lub nie wyjdzie poza planszę.

while True:
    # kod gry

Starczy, dalej pokaż, czy w ogóle coś umiesz. Jeżeli jesteś leniwym studentem, który w ogóle się na tym nie zna, to szukaj kogoś za kasę, jak już napisano powyżej.

Dzięki popróbuję podziałać dalej. Jestem tu nowy i jeszcze nie za wiele potrafię. Jak to rozwiążę to tu zamieszczę swoje rozwiązanie. Jeszcze raz dziękuje za podpowiedz

0
import random
import math

# Define the size of the map
MAP_SIZE = 10
moves = 0;

# Initialize the map
map_grid = [['.' for _ in range(MAP_SIZE)] for _ in range(MAP_SIZE)]

# Generate random starting position for the player and prevent to start on the edge
player_x, player_y = random.randint(1, MAP_SIZE - 2), random.randint(1, MAP_SIZE - 2)

# Generate random position for the hidden treasury
treasury_x, treasury_y = random.randint(0, MAP_SIZE - 1), random.randint(0, MAP_SIZE - 1)

# Ensure player and treasury positions are not the same
while (player_x, player_y) == (treasury_x, treasury_y):
    treasury_x, treasury_y = random.randint(0, MAP_SIZE - 1), random.randint(0, MAP_SIZE - 1)

# Main game loop
while True:

222.png

0
while True:
    if pozycja_gracza == pozycja_skarbu:
        print('Brawo znalazłeś skarb!')
    elif pozycja_gracza != pozycja_skarbu:
        print('Zmień pozycję gracza')
        zmiana_pozycji= input('Zmiana pozycji:''góra''dół''prawo' 'lewo')
        pozycja_gracza== 'prawo'
        pozycja_gracza[0]= pozycja_gracza [+ 1]
        print(pozycja_gracza) 

Czy ja idę w dobrym kierunku?

1

Można tak, sprawdź if-em którą pozycję wpisał gracz.
Zamiast pozycja_gracza[0]= pozycja_gracza [+ 1] powinno być pozycja_gracza[0]=pozycja_gracza[0] + 1 lub w skrócie pozycja_gracza[0] + = 1.

Wykorzystaj ChatGPT (lub podobny), będzie Ci łatwiej i szybciej

0
Telefoniak napisał(a):

Wykorzystaj ChatGPT (lub podobny), będzie Ci łatwiej i szybciej

Tylko trzeba mieć na uwadze że ChatGPT tak żeby nie miał błędów pisze tylko kod który jest bardzo, bardzo prosty. Z chociaż trochę skomplikowanymi problemami zaczyna pisać głupoty i zmyśla.

0

Popracowałem z generatorem (jeszcze jak będę miał czas i chęci to dodam coś ciekawego) moja wersja tej prostej gry jest taka:

import random
import math

# Define the size of the map
MAP_SIZE = 10
EDGE_PADDING = 1

moves = 0
magic_scroll = 3

# Initialize the map
map_grid = [['.' for _ in range(MAP_SIZE)] for _ in range(MAP_SIZE)]

# Generate random starting position for the player and prevent it from starting on the edge
player_x, player_y = random.randint(1, MAP_SIZE - EDGE_PADDING - 1), random.randint(1, MAP_SIZE - EDGE_PADDING - 1)

# Generate random position for the hidden treasury
treasury_x, treasury_y = random.randint(0, MAP_SIZE - 1), random.randint(0, MAP_SIZE - 1)

# Ensure player and treasury positions are not the same
while (player_x, player_y) == (treasury_x, treasury_y):
    treasury_x, treasury_y = random.randint(0, MAP_SIZE - 1), random.randint(0, MAP_SIZE - 1)

# Function to print the map
def print_map():
    for row in range(MAP_SIZE):
        for col in range(MAP_SIZE):
            if row == player_y and col == player_x:
                print('☺', end=' ')  # Print player symbol: ☺
            elif row == treasury_y and col == treasury_x:
                print('$', end=' ')  # Print treasury symbol: $
            else:
                print(map_grid[row][col], end=' ')
        print()

# Hello player
print('Welcome to the Treasure Hunt Game!\nBe careful not to cross the edge of the map!')

# Main game loop
while True:
    # Update the map with the player's position
    map_grid[player_y][player_x] = 'x'
    
    # Check if the player has reached the treasury
    if player_x == treasury_x and player_y == treasury_y:
        print('Congratulations! You found the hidden treasury. You win! \nNumber of moves made:', moves)
        if magic_scroll < 3:
            print('You used magic scroll at last once :(')
        print_map()
        break

    # Calculate the distance between the player and the treasury
    distance_before_move = math.sqrt((player_x - treasury_x) ** 2 + (player_y - treasury_y) ** 2)
    
    # Get the player's move input
    move = input('Enter direction (W/A/S/D) or Q to quit: ').upper()

    # Update the player's position based on the input
    if move == 'W' and player_y > 0:
        player_y -= 1
        moves += 1
    elif move == 'A' and player_x > 0:
        player_x -= 1
        moves += 1
    elif move == 'S' and player_y < MAP_SIZE - 1:
        player_y += 1
        moves += 1
    elif move == 'D' and player_x < MAP_SIZE - 1:
        player_x += 1
        moves += 1
    elif move == 'M':
        if magic_scroll <= 0:
            print('Unfortunately, your scroll has already burned out.')
        else:
            print_map()
            magic_scroll -= 1
    elif move == 'Q':
        print('Exiting the game. Goodbye!')
        break
    else:
        print('Invalid move. Please enter W, A, S, or D.')

    # Check if the player is at the edge of the map
    if player_x == 0 or player_x == MAP_SIZE - 1 or player_y == 0 or player_y == MAP_SIZE - 1:
        print('You are standing on the edge of the map.')

    # Check if the player has crossed the edge of the map
    if player_x < 0 or player_x >= MAP_SIZE or player_y < 0 or player_y >= MAP_SIZE:
        print('You have crossed the edge of the map. Game over!')
        break

    # Calculate the distance between the player's new position and the treasury
    distance_after_move = math.sqrt((player_x - treasury_x) ** 2 + (player_y - treasury_y) ** 2)

    # Check if the distance decreases after the player moves
    if distance_after_move < distance_before_move:
        print('You are getting closer to the treasury!')
    elif distance_after_move > distance_before_move:
        print('You are moving away from the treasury!')

1 użytkowników online, w tym zalogowanych: 0, gości: 1