I’m working on making a basic text game.
I’m trying to allow a player to type what they want to do and what they want to do it to.
if the player wants to pickup shovel a shovel I want to check the action pickup then apply it to the item shovel.
So far I get AttributeError: ‘list’ object has no attribute ‘find’ when I run the game.
I want to get this code to print only the shovel item when I type pickup shovel.
I’m trying to make it more natural when trying to pick something up, instead of having to identify pickup and then ask what the player wants to pick up.
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.exits = {}
self.items = []
class Player(object):
def __init__(self, name, location):
self.name = name
self.location = location
self.inventory = []
self.gameOver = False
class Item(object):
def __init__(self, name, description, equipable, attackBonus, damage):
self.name = name
self.description = description
self.equipable = equipable
self.attackBonus = attackBonus
self.damage = damage
# Create Item
shovel = Item('Shovel', 'You find a sturdy shovel. It looks like it could do some good bashing.', True, 1, 2)
rockingChair = Item('Old Rocking Chair', "A rickety old rocking chair sits covered in grim. It doesn't look very appealing.", False, 0, 0)
# Create Locations
YourHouse = Room("Your House", "There isn't anything new here.",)
TownHall = Room("Town Hall", "")
TownHall.items.append(shovel)
TownHall.items.append(rockingChair)
# Start the Game
print('n' + player.location.name)
print(player.location.description)
room_exits = "n" + "The exits are: n"
for character in room_exits:
sys.stdout.write(character)
sys.stdout.flush()
for exit in player.location.exits:
print(exit)
while player.gameOver is False:
# Movement
if action.lower() in ['north', 'south', 'east', 'west']:
if action in player.location.exits:
player.location = player.location.exits[action]
print("n" "You went to " + player.location.name + '.')
if len(player.location.description) <1:
player.location.description = emptyDescription
# Action
if (action.find('pickup') != -1):
item = player.location.items
for item in player.location.items:
if (player.location.items.find(action)):
print(item.name)
else:
print("Sorry, you can't do that.")
Jake Carlile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.