From c46122eb704bc32c8689cde926b051ff7a9b4b2c Mon Sep 17 00:00:00 2001 From: Okocha76 Date: Thu, 7 May 2020 16:50:08 +0300 Subject: [PATCH] Day 1 MVP --- src/adv.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/player.py | 9 +++++++++ src/room.py | 15 ++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..b151c15173 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from os import system, name # Declare all the rooms @@ -33,10 +35,53 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] + +def clear_screen(): + _ = system('cls' if name == 'nt' else 'clear') + + # # Main # +def adventure_game(): + clear_screen() + + print(f'\nWelcome!') + + player = Player(input(f'\nWhat is your name?'), room['outside']) + print(f'\nWelcome {player.name}!\n') + print(f'You are currently at the {player.current_room.name}\n') + + cmd = '' + + while cmd != 'q': + # Dict for movement + room_movement = { + 'n': player.current_room.n_to, + 's': player.current_room.s_to, + 'e': player.current_room.e_to, + 'w': player.current_room.w_to + } + + cmd = input('\nWhat would you like to do?' + '\nType n, s, e or w to move and q to quit\n') + + if cmd in room_movement.keys(): + clear_screen() + if room_movement[cmd]: + player.move(room_movement[cmd]) + player.current_room.room_description() + else: + print(f'\nYou can not go there!') + elif cmd != 'q': + clear_screen() + print(f'\nThat is not a valid command!') + + clear_screen() + print('Bye bye!') + + # Make a new player object that is currently in the 'outside' room. # Write a loop that: @@ -49,3 +94,6 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +if __name__ == '__main__': + adventure_game() diff --git a/src/player.py b/src/player.py index d79a175029..c8fbd984f0 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,11 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +class Player: + def __init__(self, name, current_room): + self.name = name + self.current_room = current_room + + def move(self, direction): + self.current_room = direction + print(f'\nMoving to the {self.current_room.name}\n') \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..30d3aff75a 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): + self.name = name + self.description = description + self.n_to = None + self.s_to = None + self.e_to = None + self.w_to = None + + def room_description(self): + print(f'\nYou are in the ' + self.name + + f"\n{'*'*30}\n" + self.description) \ No newline at end of file