From 5e303ad753d6aa369e1a419b167b2bfe33763e8f Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Wed, 3 Jun 2020 22:32:35 -0400 Subject: [PATCH 1/7] update files to create basic game. Cannot currently get items --- src/adv.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/item.py | 10 +++++++++ src/player.py | 18 +++++++++++++++++ src/room.py | 15 +++++++++++++- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/item.py diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..05639d5776 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from item import Item # Declare all the rooms @@ -33,9 +35,25 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] + +items = { + 'chocolate': Item('chocolate', 'eat to regain strength to code'), + 'peanut butter': Item('peanut butter', 'the power food that never spoils'), + 'book': Item('book', 'to strengthen the mind and the soul'), + 'highlighter': Item('highlighter', 'to highlight the pertinent parts of your book'), + 'computer': Item('computer', 'to code until you cannot code anymore') +} + +room['outside'].add_item(items['highlighter']) +room['foyer'].add_item(items['book']) +room['foyer'].add_item(items['computer']) +room['narrow'].add_item(items['chocolate']) +room['treasure'].add_item(items['peanut butter']) + # # Main # +player1 = Player('player1', room['outside']) # Make a new player object that is currently in the 'outside' room. @@ -49,3 +67,41 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. +while True: + command = input( + 'Enter a direction using the keys n,s,e,w to go to different rooms, q to quit the game, i to see items, g to get items, or d to drop items. ').lower() + + if command == 'n': + if player1.current_room.n_to != None: + player1.current_room = player1.current_room.n_to + print( + f'room:{player1.current_room.name} \n description: {player1.current_room.description}') + else: + print('You cannot move north') + elif command == 's': + if player1.current_room.s_to != None: + player1.current_room = player1.current_room.s_to + print( + f'room:{player1.current_room.name} \n description: {player1.current_room.description}') + else: + print('You cannot move south') + elif command == 'e': + if player1.current_room.e_to != None: + player1.current_room = player1.current_room.e_to + print( + f'room:{player1.current_room.name} \n description: {player1.current_room.description}') + else: + print('You cannot move east') + elif command == 'w': + if player1.current_room.w_to != None: + player1.current_room = player1.current_room.w_to + print( + f'room:{player1.current_room.name} \n description: {player1.current_room.description}') + else: + print('You cannot move west') + elif command == 'i': + if len(player1.current_room.items) > 0: + for i in player1.current_room.items: + print(f'name: {i.name} \n description: {i.description}') + elif command == 'q': + quit() diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..599afb1121 --- /dev/null +++ b/src/item.py @@ -0,0 +1,10 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + + def on_take(self): + print(f'you selected {self.name}') + + def on_drop(self): + print(f'You dropped {self.name}') diff --git a/src/player.py b/src/player.py index d79a175029..9512c5e7a6 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,20 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +from room import Room + + +class Player: + + def __init__(self, name, current_room, items=None): + self.name = name + self.current_room = current_room + self.items = [] if items is None else items + + def print_items(self): + if len(self.current_room.items) > 0: + for i in self.items: + print(i) + + else: + print('no items for you in this room') diff --git a/src/room.py b/src/room.py index 24c07ad4c8..2de080929a 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,15 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. + +class Room: + def __init__(self, name, description, n_to=None, s_to=None, e_to=None, w_to=None, items=None): + self.name = name + self.description = description + self.n_to = n_to + self.s_to = s_to + self.e_to = e_to + self.w_to = w_to + self.items = [] if items is None else items + + def add_item(self, item): + self.items.append(item) From 38728f0969032fd30239bc3f2b50a63377ac717f Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Wed, 3 Jun 2020 22:34:06 -0400 Subject: [PATCH 2/7] small update to push code on new branch --- src/adv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adv.py b/src/adv.py index 05639d5776..baf82749f7 100644 --- a/src/adv.py +++ b/src/adv.py @@ -62,10 +62,10 @@ # * Prints the current room name # * Prints the current description (the textwrap module might be useful here). # * Waits for user input and decides what to do. -# + # If the user enters a cardinal direction, attempt to move to the room there. # Print an error message if the movement isn't allowed. -# + # If the user enters "q", quit the game. while True: command = input( From 691805ddc2d51c430e937958a0b9828a591a9252 Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Thu, 4 Jun 2020 11:42:13 -0400 Subject: [PATCH 3/7] update room file --- src/room.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/room.py b/src/room.py index 2de080929a..af3c0cb9f6 100644 --- a/src/room.py +++ b/src/room.py @@ -2,13 +2,13 @@ # description attributes. class Room: - def __init__(self, name, description, n_to=None, s_to=None, e_to=None, w_to=None, items=None): + def __init__(self, name, description, items=None): self.name = name self.description = description - self.n_to = n_to - self.s_to = s_to - self.e_to = e_to - self.w_to = w_to + self.n_to = None + self.s_to = None + self.e_to = None + self.w_to = None self.items = [] if items is None else items def add_item(self, item): From ce48af91b30962ef4dc52e2a380c4a3cc2f019fc Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Thu, 4 Jun 2020 18:30:41 -0400 Subject: [PATCH 4/7] try new logic --- src/adv.py | 38 -------------------------------------- src/item.py | 4 ++-- src/player.py | 2 +- 3 files changed, 3 insertions(+), 41 deletions(-) diff --git a/src/adv.py b/src/adv.py index baf82749f7..5d16f36b38 100644 --- a/src/adv.py +++ b/src/adv.py @@ -67,41 +67,3 @@ # Print an error message if the movement isn't allowed. # If the user enters "q", quit the game. -while True: - command = input( - 'Enter a direction using the keys n,s,e,w to go to different rooms, q to quit the game, i to see items, g to get items, or d to drop items. ').lower() - - if command == 'n': - if player1.current_room.n_to != None: - player1.current_room = player1.current_room.n_to - print( - f'room:{player1.current_room.name} \n description: {player1.current_room.description}') - else: - print('You cannot move north') - elif command == 's': - if player1.current_room.s_to != None: - player1.current_room = player1.current_room.s_to - print( - f'room:{player1.current_room.name} \n description: {player1.current_room.description}') - else: - print('You cannot move south') - elif command == 'e': - if player1.current_room.e_to != None: - player1.current_room = player1.current_room.e_to - print( - f'room:{player1.current_room.name} \n description: {player1.current_room.description}') - else: - print('You cannot move east') - elif command == 'w': - if player1.current_room.w_to != None: - player1.current_room = player1.current_room.w_to - print( - f'room:{player1.current_room.name} \n description: {player1.current_room.description}') - else: - print('You cannot move west') - elif command == 'i': - if len(player1.current_room.items) > 0: - for i in player1.current_room.items: - print(f'name: {i.name} \n description: {i.description}') - elif command == 'q': - quit() diff --git a/src/item.py b/src/item.py index 599afb1121..3b080495dd 100644 --- a/src/item.py +++ b/src/item.py @@ -4,7 +4,7 @@ def __init__(self, name, description): self.description = description def on_take(self): - print(f'you selected {self.name}') + print(f'You just pickec up the {self.name}!') def on_drop(self): - print(f'You dropped {self.name}') + print(f'You just dropped the {self.name}!') diff --git a/src/player.py b/src/player.py index 9512c5e7a6..1540e5b920 100644 --- a/src/player.py +++ b/src/player.py @@ -17,4 +17,4 @@ def print_items(self): print(i) else: - print('no items for you in this room') + print('nothing for you here') From ea14769a7291bee905ceb12f58e56c57adf0c1ab Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Thu, 4 Jun 2020 22:08:44 -0400 Subject: [PATCH 5/7] update files but having issues with direction functionality --- src/adv.py | 29 +++++++++++++++++++++++------ src/item.py | 6 +++--- src/player.py | 40 +++++++++++++++++++++++++++++++--------- src/room.py | 27 ++++++++++++++++++--------- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/src/adv.py b/src/adv.py index 5d16f36b38..c597029501 100644 --- a/src/adv.py +++ b/src/adv.py @@ -2,6 +2,7 @@ from player import Player from item import Item + # Declare all the rooms room = { @@ -44,16 +45,16 @@ 'computer': Item('computer', 'to code until you cannot code anymore') } -room['outside'].add_item(items['highlighter']) -room['foyer'].add_item(items['book']) -room['foyer'].add_item(items['computer']) -room['narrow'].add_item(items['chocolate']) -room['treasure'].add_item(items['peanut butter']) +room['outside'].items.append(items['highlighter']) +room['foyer'].items.append(items['book']) +room['foyer'].items.append(items['computer']) +room['narrow'].items.append(items['chocolate']) +room['treasure'].items.append(items['peanut butter']) # # Main # -player1 = Player('player1', room['outside']) +user = Player('Totoro', room['outside'], inventory=None) # Make a new player object that is currently in the 'outside' room. @@ -67,3 +68,19 @@ # Print an error message if the movement isn't allowed. # If the user enters "q", quit the game. +while True: + print( + f"\n\nWelcome {user.name}!\n You are currently standing in the {user.current_room}") + + direction = input( + "Choose a direction (n, s, e, w) and enter q to end the game: ").lower() + + if direction in ['n', 's', 'e', 'w']: + user.current_room = user.move_to(direction, user.current_room) + continue + elif direction == 'q': + print('Have a great day!') + break + else: + direction != 'n' or 's' or 'w' or 'e' or 'q' + print("Please enter a valid command") diff --git a/src/item.py b/src/item.py index 3b080495dd..0979674dda 100644 --- a/src/item.py +++ b/src/item.py @@ -3,8 +3,8 @@ def __init__(self, name, description): self.name = name self.description = description - def on_take(self): - print(f'You just pickec up the {self.name}!') + def pick_up(self): + print(f'You just picked up the {self.name}!') - def on_drop(self): + def drop(self): print(f'You just dropped the {self.name}!') diff --git a/src/player.py b/src/player.py index 1540e5b920..eb5cfaddc9 100644 --- a/src/player.py +++ b/src/player.py @@ -1,20 +1,42 @@ # Write a class to hold player information, e.g. what room they are in # currently. -from room import Room - class Player: - def __init__(self, name, current_room, items=None): + def __init__(self, name, current_room, inventory: []): self.name = name self.current_room = current_room - self.items = [] if items is None else items + self.inventory = inventory - def print_items(self): - if len(self.current_room.items) > 0: - for i in self.items: - print(i) + def move_to(self, movement, current_location): + attribute = movement + '_to' + + if hasattr(current_location, attribute): + return getattr(current_location, attribute) + + print('You cannot go this direction - choose another direction') + + return current_location + def take(self, item): + if self.current_room.items.count(item) > 0: + self.inventory.append(item) + self.current_room.items.remove(item) + else: + print(f'The {item.name} is not here') + + def drop(self, item): + if self.items.count(item) > 0: + self.current_room.items.append(item) + self.inventory.items.remove(item) + else: + print(f"You do not have the item {item.name} to drop") + + def print_items(self): + if len(self.inventory) < 1: + print("you are emptyhanded") else: - print('nothing for you here') + print('Here is what you have: ') + for x in self.inventory: + print(x.name) diff --git a/src/room.py b/src/room.py index af3c0cb9f6..16124a8d26 100644 --- a/src/room.py +++ b/src/room.py @@ -2,14 +2,23 @@ # description attributes. class Room: - def __init__(self, name, description, items=None): + def __init__(self, name, description, items=[]): self.name = name self.description = description - self.n_to = None - self.s_to = None - self.e_to = None - self.w_to = None - self.items = [] if items is None else items - - def add_item(self, item): - self.items.append(item) + self.items = items + + def __str__(self): + return f'{self.name}' + + def print_description(self): + return f'{self.description}' + + def list_items(self): + if not self.items: + print("there are no items to be found in this room") + else: + print("This room contains some items: ") + for item in self.items: + print(item.name) + print(item.description) + From cdc1cedd593366011d91bc1644ae12fe687f5ad1 Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Fri, 5 Jun 2020 07:38:46 -0400 Subject: [PATCH 6/7] getting error tht Player takes no arguments. Will keep working on this --- src/adv.py | 61 +++++++++++++++++++++++++++++++++------------------ src/item.py | 3 +++ src/player.py | 37 ++----------------------------- src/room.py | 24 +++++--------------- 4 files changed, 51 insertions(+), 74 deletions(-) diff --git a/src/adv.py b/src/adv.py index c597029501..07d67448c6 100644 --- a/src/adv.py +++ b/src/adv.py @@ -7,21 +7,21 @@ room = { 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), + "North of you, the cave mount beckons", []), 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), +passages run north and east.""", []), 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +the distance, but there is no way across the chasm.""", []), 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), +to north. The smell of gold permeates the air.""", []), 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), +earlier adventurers. The only exit is to the south.""", []), } @@ -53,8 +53,11 @@ # # Main -# -user = Player('Totoro', room['outside'], inventory=None) +# make a new player +name = input("Hello! What is your name? ") + + +user = Player(name, room['outside']) # Make a new player object that is currently in the 'outside' room. @@ -70,17 +73,33 @@ # If the user enters "q", quit the game. while True: print( - f"\n\nWelcome {user.name}!\n You are currently standing in the {user.current_room}") - - direction = input( - "Choose a direction (n, s, e, w) and enter q to end the game: ").lower() - - if direction in ['n', 's', 'e', 'w']: - user.current_room = user.move_to(direction, user.current_room) - continue - elif direction == 'q': - print('Have a great day!') - break - else: - direction != 'n' or 's' or 'w' or 'e' or 'q' - print("Please enter a valid command") + f"\n\nWelcome{user}!\n You are currently standing in the: " + user.current_room.name) + print("\n Description of room: " + user.current_room.description) + print("\nItems in the room: ") + for item in user.current_room.items: + print(item) + print("\n Choose the direction you want to go. Use n,s,e,w to select direction, i to get inventory, and t to get item, d to drop an item, and q to quit.") + command = input().split().lower() + currentRoom = user.current_room.name + + if len(command) == 1: + if command[0] == 'n': + if user.current_room.n_to: + user.current_room = user.current_room.n_to + elif command[0] == 's': + if user.current_room.s_to: + user.current_room = user.current_room.s_to + elif command[0] == 'e': + if user.current_room.e_to: + user.current_room = user.current_room.e_to + elif command[0] == 'w': + if user.current_room.w_to: + user.current_room = user.current_room.w_to + elif command[0] == 'i': + print("\n Here are your items: ") + for item in user.items: + print(item) + elif command[0] == 'q': + break + else: + print("Error entering command. Use n,s,e,w for directions, i for inventory, t to get an item, d to drop an item") diff --git a/src/item.py b/src/item.py index 0979674dda..d658d37d21 100644 --- a/src/item.py +++ b/src/item.py @@ -3,6 +3,9 @@ def __init__(self, name, description): self.name = name self.description = description + def __str__(self): + return self.name + "\n" + self.description + def pick_up(self): print(f'You just picked up the {self.name}!') diff --git a/src/player.py b/src/player.py index eb5cfaddc9..5a58996129 100644 --- a/src/player.py +++ b/src/player.py @@ -3,40 +3,7 @@ class Player: - - def __init__(self, name, current_room, inventory: []): + def __init_(self, name, current_room): self.name = name self.current_room = current_room - self.inventory = inventory - - def move_to(self, movement, current_location): - attribute = movement + '_to' - - if hasattr(current_location, attribute): - return getattr(current_location, attribute) - - print('You cannot go this direction - choose another direction') - - return current_location - - def take(self, item): - if self.current_room.items.count(item) > 0: - self.inventory.append(item) - self.current_room.items.remove(item) - else: - print(f'The {item.name} is not here') - - def drop(self, item): - if self.items.count(item) > 0: - self.current_room.items.append(item) - self.inventory.items.remove(item) - else: - print(f"You do not have the item {item.name} to drop") - - def print_items(self): - if len(self.inventory) < 1: - print("you are emptyhanded") - else: - print('Here is what you have: ') - for x in self.inventory: - print(x.name) + self.items = items diff --git a/src/room.py b/src/room.py index 16124a8d26..666f60c288 100644 --- a/src/room.py +++ b/src/room.py @@ -2,23 +2,11 @@ # description attributes. class Room: - def __init__(self, name, description, items=[]): + def __init__(self, name, description, n_to=None, s_to=None, e_to=None, w_to=None): self.name = name self.description = description - self.items = items - - def __str__(self): - return f'{self.name}' - - def print_description(self): - return f'{self.description}' - - def list_items(self): - if not self.items: - print("there are no items to be found in this room") - else: - print("This room contains some items: ") - for item in self.items: - print(item.name) - print(item.description) - + self.items = [] + self.n_to = n_to + self.s_to = s_to + self.e_to = e_to + self.w_to = w_to From 395900724aa54b3ccf10e88ebed654800dd7ba89 Mon Sep 17 00:00:00 2001 From: Jon Haase Date: Sat, 6 Jun 2020 21:08:00 -0400 Subject: [PATCH 7/7] update files again as experiment with different options --- examples/history.txt | 2 +- examples/rock_paper_scissors.py | 50 +++++++------- src/adv.py | 118 ++++++-------------------------- src/item.py | 11 +-- src/logo.py | 51 ++++++++++++++ src/player.py | 29 ++++++-- src/room.py | 20 +++--- src/room_info.py | 36 ++++++++++ 8 files changed, 172 insertions(+), 145 deletions(-) create mode 100644 src/logo.py create mode 100644 src/room_info.py diff --git a/examples/history.txt b/examples/history.txt index aa178b017c..f2109fcd47 100644 --- a/examples/history.txt +++ b/examples/history.txt @@ -1 +1 @@ -3,3,0 \ No newline at end of file +7,5,3 \ No newline at end of file diff --git a/examples/rock_paper_scissors.py b/examples/rock_paper_scissors.py index 4a1899f900..e05b41caf9 100644 --- a/examples/rock_paper_scissors.py +++ b/examples/rock_paper_scissors.py @@ -1,35 +1,35 @@ -#import module we need +# import module we need import random -#file i/o functions for historical results -def load_results(): - text_file = open("history.txt", "r") - history = text_file.read().split(",") - text_file.close() - return history +# file i/o functions for historical results +# def load_results(): +# text_file = open("history.txt", "r") +# history = text_file.read().split(",") +# text_file.close() +# return history -def save_results( w, t, l): - text_file = open("history.txt", "w") - text_file.write( str(w) + "," + str(t) + "," + str(l)) - text_file.close() +# def save_results( w, t, l): +# text_file = open("history.txt", "w") +# text_file.write( str(w) + "," + str(t) + "," + str(l)) +# text_file.close() -#welcome message +# welcome message results = load_results() wins = int(results[0]) -ties = int( results[1]) +ties = int(results[1]) losses = int(results[2]) print("Welcome to Rock, Paper, Scissors!") print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses)) print("Please choose to continue...") -#initialize user, computer choices -computer = random.randint(1,3) +# initialize user, computer choices +computer = random.randint(1, 3) user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n")) -#gamplay loop +# gamplay loop while not user == 9: - #user chooses ROCK + # user chooses ROCK if user == 1: if computer == 1: print("Computer chose rock...tie!") @@ -41,7 +41,7 @@ def save_results( w, t, l): print("Computer chose scissors...you wins :)") wins += 1 - #user chooses PAPER + # user chooses PAPER elif user == 2: if computer == 1: print("Computer chose rock...you win :)") @@ -52,8 +52,8 @@ def save_results( w, t, l): else: print("Computer chose scissors...computer wins :(") losses += 1 - - #user chooses SCISSORS + + # user chooses SCISSORS elif user == 3: if computer == 1: print("Computer chose rock...computer wins :(") @@ -66,14 +66,14 @@ def save_results( w, t, l): ties += 1 else: print("Invalid selection. Please try again.") - #print updated stats + # print updated stats print("Wins: %s, Ties: %s, Losses: %s" % (wins, ties, losses)) - #prompt user to make another selection + # prompt user to make another selection print("Please choose to continue...") - #initialize user, computer choices - computer = random.randint(1,3) + # initialize user, computer choices + computer = random.randint(1, 3) user = int(input("[1] Rock [2] Paper [3] Scissors [9] Quit\n")) # #game over, save results -save_results(wins, ties, losses) \ No newline at end of file +save_results(wins, ties, losses) diff --git a/src/adv.py b/src/adv.py index 07d67448c6..d417975b42 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,105 +1,31 @@ from room import Room from player import Player -from item import Item +from room_info import room_info +room_map = room_info() -# Declare all the rooms +player = Player("Harry Potter", room_map["outside"]) -room = { - 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons", []), - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east.""", []), - - 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling -into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm.""", []), - - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air.""", []), - - 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure -chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south.""", []), -} - - -# Link rooms together - -room['outside'].n_to = room['foyer'] -room['foyer'].s_to = room['outside'] -room['foyer'].n_to = room['overlook'] -room['foyer'].e_to = room['narrow'] -room['overlook'].s_to = room['foyer'] -room['narrow'].w_to = room['foyer'] -room['narrow'].n_to = room['treasure'] -room['treasure'].s_to = room['narrow'] - - -items = { - 'chocolate': Item('chocolate', 'eat to regain strength to code'), - 'peanut butter': Item('peanut butter', 'the power food that never spoils'), - 'book': Item('book', 'to strengthen the mind and the soul'), - 'highlighter': Item('highlighter', 'to highlight the pertinent parts of your book'), - 'computer': Item('computer', 'to code until you cannot code anymore') -} - -room['outside'].items.append(items['highlighter']) -room['foyer'].items.append(items['book']) -room['foyer'].items.append(items['computer']) -room['narrow'].items.append(items['chocolate']) -room['treasure'].items.append(items['peanut butter']) - -# -# Main -# make a new player -name = input("Hello! What is your name? ") - - -user = Player(name, room['outside']) - -# Make a new player object that is currently in the 'outside' room. - -# Write a loop that: -# -# * Prints the current room name -# * Prints the current description (the textwrap module might be useful here). -# * Waits for user input and decides what to do. - -# If the user enters a cardinal direction, attempt to move to the room there. -# Print an error message if the movement isn't allowed. - -# If the user enters "q", quit the game. while True: - print( - f"\n\nWelcome{user}!\n You are currently standing in the: " + user.current_room.name) - print("\n Description of room: " + user.current_room.description) - print("\nItems in the room: ") - for item in user.current_room.items: - print(item) - print("\n Choose the direction you want to go. Use n,s,e,w to select direction, i to get inventory, and t to get item, d to drop an item, and q to quit.") - command = input().split().lower() - currentRoom = user.current_room.name + print(player.room.name) + + user_input = input( + "Please choose from the following to play: 1) move in directions n,s,e,w 2) p for pickup 3) q for quit ") - if len(command) == 1: - if command[0] == 'n': - if user.current_room.n_to: - user.current_room = user.current_room.n_to - elif command[0] == 's': - if user.current_room.s_to: - user.current_room = user.current_room.s_to - elif command[0] == 'e': - if user.current_room.e_to: - user.current_room = user.current_room.e_to - elif command[0] == 'w': - if user.current_room.w_to: - user.current_room = user.current_room.w_to - elif command[0] == 'i': - print("\n Here are your items: ") - for item in user.items: - print(item) - elif command[0] == 'q': - break + if user_input.lower() in ['n', 's', 'e', 'w']: + if player.room.connections is not None: + player.move(user_input) + print("You are now in ", player.room.name) + print("Items in this room are: ", player.room.items) else: - print("Error entering command. Use n,s,e,w for directions, i for inventory, t to get an item, d to drop an item") + print("You have reached a dead end") + elif user_input.lower() == 'p': + item_check = input("what item will you choose? ") + player.pickup(item_check) + elif user_input.lower() == 'q': + print("Thank you for playing!") + break + + else: + print("Do not understand your input. Please enter a valid command or press q to quit. ") diff --git a/src/item.py b/src/item.py index d658d37d21..cbd098809f 100644 --- a/src/item.py +++ b/src/item.py @@ -1,13 +1,6 @@ + + class Item: def __init__(self, name, description): self.name = name self.description = description - - def __str__(self): - return self.name + "\n" + self.description - - def pick_up(self): - print(f'You just picked up the {self.name}!') - - def drop(self): - print(f'You just dropped the {self.name}!') diff --git a/src/logo.py b/src/logo.py new file mode 100644 index 0000000000..0d1829b30f --- /dev/null +++ b/src/logo.py @@ -0,0 +1,51 @@ +import turtle +import random +myPen = turtle.Turtle() +myPen.shape("turtle") +myPen.speed(10) + +window = turtle.Screen() +window.bgcolor("#00B2C0") + + +def corner(turtle): + for x in range(0, 6): + turtle.left(15) + turtle.forward(2) + + +def cornered_box(turtle, x1, y1, x2, y2, letter): + turtle.penup() + turtle.goto(x1, y1) + turtle.pendown() + turtle.color("black") + turtle.fillcolor("black") + turtle.begin_fill() + for x in range(0, 4): + turtle.forward(40) + corner(turtle) + turtle.end_fill() + turtle.penup() + turtle.goto(x2, y2) + turtle.color("white") + turtle.write(letter, None, None, "28pt bold") + + +cornered_box(myPen, -40, 20, -35, 35, "C") +cornered_box(myPen, 25, 20, 30, 35, "O") +cornered_box(myPen, -40, -45, -35, -30, "D") +cornered_box(myPen, 25, -45, 30, -30, "E") + +myPen.color("white") +myPen.goto(-115, -92) +myPen.write("Anyone Can Learn", None, None, "24pt bold") +myPen.goto(-115, -127) +myPen.write("Anyone Can Teach", None, None, "24pt bold") +myPen.left(90) + + +myPen.goto(10, 105) + +for x in range(0, 100): + myPen.color("#%06x" % random.randint(0, 2**24 - 1)) +myPen.color("white") diff --git a/src/player.py b/src/player.py index 5a58996129..470093a116 100644 --- a/src/player.py +++ b/src/player.py @@ -1,9 +1,30 @@ # Write a class to hold player information, e.g. what room they are in # currently. +from room import Room -class Player: - def __init_(self, name, current_room): +class Player(): + def __init__(self, name, room, inventory=[]): self.name = name - self.current_room = current_room - self.items = items + self.room = room + self.inventory = inventory + + def __str__(self): + return "You are currently in: " + self.room + + def move(self, direction): + if self.room.connections[direction] is not None: + self.room = self.room.connections[direction] + else: + print("You can not go this way. Choose another direction.") + + def pickup(self, item): + if item in self.room.items: + self.inventory.append(item) + print(f"You have obtained {item.name}") + else: + print("The item is not in the room") + + def print_inventory(self): + for item in self.inventory: + print(item.name + item.description) diff --git a/src/room.py b/src/room.py index 666f60c288..75276b9eaf 100644 --- a/src/room.py +++ b/src/room.py @@ -1,12 +1,12 @@ -# Implement a class to hold room information. This should have name and -# description attributes. - -class Room: - def __init__(self, name, description, n_to=None, s_to=None, e_to=None, w_to=None): +class Room(): + def __init__(self, name, description, n_to=None, e_to=None, s_to=None, w_to=None, items=[]): self.name = name self.description = description - self.items = [] - self.n_to = n_to - self.s_to = s_to - self.e_to = e_to - self.w_to = w_to + self.connections = { + 'n': n_to, + 's': s_to, + 'e': e_to, + 'w': w_to + } + + self.items = items diff --git a/src/room_info.py b/src/room_info.py new file mode 100644 index 0000000000..9b637c807e --- /dev/null +++ b/src/room_info.py @@ -0,0 +1,36 @@ +from room import Room + + +def room_info(): + room_map = { + 'outside': Room("Outside Cave Entrance", + "North of you, the cave mount beckons"), + + 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty +passages run north and east."""), + + 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling +into the darkness. Ahead to the north, a light flickers in +the distance, but there is no way across the chasm."""), + + 'narrow': Room("Narrow Passage", """The narrow passage bends here from west +to north. The smell of gold permeates the air."""), + + 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure +chamber! Sadly, it has already been completely emptied by +earlier adventurers. The only exit is to the south."""), + } + + +# Link rooms together + + room_map['outside'].connections["n"] = room_map['foyer'] + room_map['foyer'].connections["s"] = room_map['outside'] + room_map['foyer'].connections["n"] = room_map['overlook'] + room_map['foyer'].connections["e"] = room_map['narrow'] + room_map['overlook'].connections["s"] = room_map['foyer'] + room_map['narrow'].connections["w"] = room_map['foyer'] + room_map['narrow'].connections["n"] = room_map['treasure'] + room_map['treasure'].connections["s"] = room_map['narrow'] + + return room_map