Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14331

I am creating a text based game for a project. My issue I'm having is when I run my program it doesn't change the name of the room

$
0
0

This is my current code in full.# print welcome messageprint('Welcome to the game.')print('You must collect all six items and exit the house to bring the Villain to justice.')print('Move Commands: {direction} North, South, East, West, exit')print('get {item} (add nearby item to inventory)')

import osdef clear():    os.system('cls' if os.name == 'nt' else 'clear')#Maprooms = {'Start': {'East': 'Living Room', 'Item': 'Key'},'Living Room': {'South': 'Bathroom', 'Item': 'Towel'},'Bathroom': {'East': 'Guest Room', 'Item': 'Journal'},'Guest Room': {'North': 'Green Room'},'Green Room': {'North': 'Master Bedroom', 'Boss': 'Villain'},'Master Bedroom': {'North': 'Mud Room', 'Item': 'Shoes'},'Mudroom': {'West': 'Kitchen', 'Item': 'Knife'}}# List of vowelsvowels = ['a', 'e', 'i', 'o', 'u']# List of inventoryInventory= []# Track current roomcurrent_room = 'Start Room'#Lastmovemsg = ''clear()while True:    clear()    print(f'You are in the {current_room}')    print(f'Inventory :[]')    move = input(f'Enter your move: ').split()[0].capitalize()    print('___________________________')    # Item indicator    if 'Item' in rooms[current_room].keys():        nearby_item = rooms[current_room]['Item']        if nearby_item not in Inventory:            if nearby_item[-1] == 's':                print(f'You see {nearby_item}')            elif nearby_item[0] in vowels:                print(f'You see an {nearby_item}')            else:                print(f'You see a {nearby_item}')    # Boss encounter    if "Boss" in rooms[current_room].keys():    # Lose    if len(inventory) <6:         print(f"You lost a fight with {rooms[current_room]['Boss']}.")         break    # Win    else:        print(f"You beat {rooms[current_room]['Boss']}!")        break# Accept player inputuser_input = ('Enter your move:')# splits into wordsnext_move = user_input.split('')# First word is actionaction = next_move[0].title()if len(next_move) > 1:    item = next_move[1:]    direction = next_move[1].title()# Moving between roomsif action == 'Go':    try:        current_room = rooms[current_room][direction]        print('You travel{direction}.')    except:        print("You can't go that way!")# Picking up itemselif action == 'Get':    try:        if item == rooms[current_room]['item']:            if item not in inventory:                inventory.append(rooms[current_room]['item'])                msg = f'{item} retrieved!'            else:                msg = f'You already have the {item}.'        else:            msg = f"Can't find {item}."    except:        msg = f"can't find {item}."# Exit gameelif action == 'Exit':    break# Invalid commandselse:    msg = 'Invalid command.'

This is not giving any error messages it just doesn't go to the next room. This is the output.

Welcome to the game.You must collect all six items and exit the house to bring the Villain to justice.Move Commands: {direction} North, South, East, West, exitget {item} (add nearby item to inventory)You are in the StartInventory :[]Enter your move: east__________________________You see a KeyYou are in the StartInventory :[]Enter your move: north___________________________You see a KeyYou are in the StartInventory :[]

I have tried to change the current_room and putting 'go east' as my input when prompted but it then gives a KeyError.your text


Viewing all articles
Browse latest Browse all 14331

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>