adventure-game #3

Open
Kenwood wants to merge 80 commits from adventure-game into master
19 changed files with 621 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
*.pyc
.idea
Adventure Game/adventure_game/logs/AdventureGame.log

0
4/4.10/4.10.1/main.py Normal file
View File

2
Adventure Game/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv
inspectionProfiles

7
Adventure Game/Makefile Normal file
View File

@ -0,0 +1,7 @@
init:
pip install -r requirements.txt
test:
py.test tests
.PHONY: init test

View File

@ -0,0 +1,138 @@
import npyscreen
import sys
from Handler import Handler
class QuitButton(npyscreen.ButtonPress):
def whenPressed(self):
sys.exit(0)
class GameNavigator(npyscreen.FormBaseNew):
"""
This class handles all the drawing and 'graphics' of our game.
only basic logic like initial loading should happen here. re-drawing
and game logic should be done in Handler.py
TODO: Find a fix for initial room startup
TODO: Find a way to reset the cursor after a user hits sendButton
"""
def update_log(self, newline):
self.logList.append('> ' + newline) # Append the newline
self.logList = self.logList[-7:] # Truncate to only the last 5 lines
res = '' # Convert the list to a string
for element in self.logList:
res = res + str(element) + '\n'
res = res.upper() # Log is always uppercase
self.logBox.value = res # Set the logbox to that value
def update_location(self, location, art=None):
"""
This may not be needed in the future, dynamic
handling of location is something the navigator should do and should inherit from player.
"""
self.artBox.footer = location
if art != None:
self.artContent.value = art
else:
self.artContent.value = self.parentApp.gamelib['menu']['graphics']['not_found']
def update_inventory(self, items):
res = ''
for element in items:
res = res + str(element) + '\n'
res = res.upper() # Inventory is always uppercase
self.inventoryContent.value = res # Set the logbox to that value
def create(self):
top_division_height = 20
inventory_width = 20
art_width = 100
self.logList = []
self.artBox = self.add(npyscreen.BoxBasic,
name='ArtBox',
max_width=art_width,
max_height=top_division_height,
rely=2,
relx=inventory_width + 1,
editable=False)
self.artContent = self.add(npyscreen.MultiLineEdit,
rely=3,
relx=inventory_width + 2,
max_width=art_width - 2,
max_height=top_division_height - 2,
value=self.parentApp.gamelib['menu']['graphics']['start_to_continue'],
editable=False)
self.artBox.footer = 'Unknown Location'
self.inventoryBox = self.add(npyscreen.BoxBasic,
name='Inventory',
max_width=inventory_width,
max_height=top_division_height,
relx=1,
rely=2,
editable=False)
self.inventoryContent = self.add(npyscreen.MultiLineEdit,
max_width=inventory_width,
max_height=top_division_height,
relx=2,
rely=3,
value='------------------',
editable=False)
self.logBoxOutline = self.add(npyscreen.BoxBasic,
max_width=inventory_width + art_width,
max_height=9,
relx=1,
rely=top_division_height + 2,
editable=False)
self.logBox = self.add(npyscreen.MultiLineEdit,
max_width=inventory_width + art_width - 7,
max_height=7,
relx=2,
rely=top_division_height + 3,
editable=False)
self.dialogueBoxOutline = self.add(npyscreen.BoxBasic,
max_width=inventory_width + art_width,
max_height=3,
relx=1,
rely=top_division_height + 2 + 9,
editable=False)
self.dialogueBox = self.add(npyscreen.Textfield,
name='Type Here',
max_width=inventory_width + art_width - 14,
max_height=1,
relx=2,
rely=top_division_height + 3 + 9)
self.sendButton = self.add(Handler,
name="Send",
relx=inventory_width + art_width - 7,
rely=top_division_height + 3 + 9)
self.quitButton = self.add(QuitButton,
name="Quit",
relx=1,
rely=1)
"""
We've reached end of __init__ basicly by this point
its up to Handler.py to actually play the game, but we should
do some basic initalization here
"""
# TODO: load art from the last place the player was in
# TODO: load up inventory
# TODO: Expand this by loding the text from the game
# WARN: THIS MAY REQUIRE REWRITING HANDLER.PY TO INTALIZE THE ROOM OBJECT OUTSIDE OF HANDLER.PY
self.update_log('Welcome back! Try "LOOK AROUND" to get started.')
self.update_log(
'>>Note from joe: Welcome! you\'re playing the demo! Please dont mind text issues like |this| and *this*\ni have yet to implement color!')

View File

@ -0,0 +1,171 @@
import npyscreen
from yaml_parser import parse_datafile as parse
class Handler(npyscreen.ButtonPress):
"""
Very important, called when the player hits send, there are several things we need to do here:
1: handle the player's input, and run logic, this is done in handler.py
2: prepare new items to display on the screen
3: re-render the screen
"""
def localize_room(self, roomlocation):
"""
This method can re-localize the room.
"""
try:
room = parse(self.parent.parentApp.mainPath / 'gamedata/world' / roomlocation)['room']
# If the file could not be found
except FileNotFoundError:
# Log a critical error!
self.parent.parentApp.log.critical(
'Handler could not load the current room! Is the player file corrupt or was there a typo? Path was {0}'.format(
self.parent.parentApp.mainPath / 'gamedata/world' / roomlocation))
# Put the player in a blank room i forgot to finish
room = parse(self.parent.parentApp.mainPath / 'gamedata/world/blank_room.yaml')
return room
def whenPressed(self):
self.parent.parentApp.log.debug('Send button pressed!')
# This is the raw command from the user
raw_command = self.parent.dialogueBox.value.upper()
# Clear the dialogue box, TODO: This may become unneeded if issue #8 is fixed
self.parent.dialogueBox.value = ''
# This is the raw command from the user
parsed_command = raw_command.split()
try:
command = parsed_command.pop(0)
except IndexError:
self.parent.parentApp.log.warn(
'Command "{0}" could not be split, was it malformed or incomplete?'.format(raw_command))
command = ''
arguments = parsed_command # Whatever is left in the list, are arguments.
# Handle an empty command
if len(command) <= 2:
self.parent.update_log('Command was too short, try something like "MOVE", "LOOK AROUND" or "USE".')
else:
# Concatenate everything back together (just to show the user the program understood them correctly
self.parent.update_log(command + ' ' + ' '.join(str(s) for s in arguments))
"""
This is where real logic can happen!
"""
# Localize the player
player = self.parent.parentApp.player
room = self.localize_room(player.playerData['player']['location']) # Localize the room
# By now we should be situated in our room, and with our player.
# self.parent.parentApp.log.debug(room) # We dont need to log this, its too verbose!
# TODO: Should upgrade these to use fuzzy words library! and not direct comparisons!
if command == 'LOOK':
try:
if arguments[0] == 'AROUND':
try:
self.parent.update_log(room['look_around'])
except KeyError:
self.parent.update_log('There is nothing to look at?.. This might be a bug.')
if arguments[0] == 'AT':
try:
# Argument[1] is the "thing" you want to look at, yaml is lowercase so we lowercase it.
self.parent.update_log(room[arguments[1].lower()]['look_at'])
except KeyError:
self.parent.update_log("Im not sure what you're trying to look at.")
except IndexError:
self.parent.parentApp.log.error('Could not handle {0}, {1}'.format(command, arguments))
self.parent.update_log("You must specify something to look at.")
elif command in ['PICK', 'TAKE']:
try:
if arguments[0] == 'UP' or command in ['TAKE']:
if command in ['TAKE']:
arguments = ['UP'] + arguments # This is a messy way to fix take logic vs pick up logic but it works.
if len(arguments) <= 2: # If there are only 2 args ex:up, item then we dont need to merge that last arg
try:
# Argument[1] is the "thing" you want to pick up, yaml is lowercase so we lowercase it.
self.parent.parentApp.log.info('Player tried to pick up {0}'.format(arguments[1]))
self.parent.update_log(room[arguments[1].lower()]['pick_up'])
player.add_inventory(room[arguments[1].lower()]['item_name'])
except KeyError:
self.parent.update_log("You cant pick that up.")
else: # if its a longer list of args, the player prolly wants to pick up an item with multiple words, like hand_axe, or log_viewer
try:
long_arg = '_'.join(
arguments[1:]) # Joins whatever comes after 1 in our args with '_' between
self.parent.parentApp.log.info(
'Player tried to pick up long object {0}'.format(long_arg))
self.parent.update_log(room[long_arg.lower()]['pick_up'])
player.add_inventory(room[long_arg.lower()]['item_name'])
except KeyError:
self.parent.update_log("You cant pick that up.")
except IndexError:
self.parent.parentApp.log.error('Could not handle {0}, {1}'.format(command, arguments))
self.parent.update_log("You must specify something to pick up.")
elif command == 'OPEN':
try:
self.parent.parentApp.log.info('Player tried to open door: {0}'.format(arguments[0]))
new_room = room[arguments[0].lower()]['leads_to']
self.parent.parentApp.log.debug('New room is: {0}'.format(new_room))
upon_enter = player.change_room(new_room) # Change the player to that new room.
self.parent.update_log(upon_enter) # Print the new room upon enter text.
room = self.localize_room(player.playerData['player']['location']) # Re-Localize!
except KeyError:
self.parent.update_log("You cant open that.")
except IndexError:
self.parent.update_log("You must specify something to open")
elif command == 'INSPECT':
try:
self.parent.parentApp.log.info('Player inspecting: {0}'.format(arguments[0]))
self.parent.update_log(
room[arguments[0].lower()]['inspect']) # Prints the inspect text, if it exists
except KeyError:
self.parent.update_log("Nothing more to inspect here.")
except IndexError:
self.parent.update_log("You must specify something to inspect.")
elif command == 'START':
self.parent.update_log('Welcome to the game! Try to |LOOK AROUND|.')
else:
self.parent.parentApp.log.info('Player\'s command was not understood: {0}'.format(command))
self.parent.update_log('I didn\'t understand {0}'.format(command))
if command == 'WHERE':
# TODO: this should take the human readable room name, not the code-name
self.parent.update_log('You are in {0}.'.format(room))
# Log the command that we parsed
self.parent.parentApp.log.info('Parsed command "{0}" with arguments "{1}"'.format(command, arguments))
"""
Do a little bit of final setup, change the art if we
need to, change the text at the bottom and update the inventory.
"""
try:
self.parent.update_location(
room['name'],
room['art'])
except KeyError:
self.parent.update_location('Unknown Location')
self.parent.update_inventory(player.playerData['player']['inventory'])
# Make sure to re-draw the art box when we're all done (in case we updated it in logic above)
self.parent.artContent.display()
# Switch back to the game menu.
# TODO: possibly deprecate this statement?
self.parent.parentApp.switchForm('GAME')

View File

@ -0,0 +1,21 @@
import npyscreen
from Player import Player
class MainMenu(npyscreen.Form):
"""
This is the main menu, code here should only be for
initializing the player data and any settings they want to change
"""
def afterEditing(self):
# TODO: the game needs to happen after this inital main menu
self.parentApp.setNextForm('GAME')
def create(self):
self.add(npyscreen.FixedText, value='You cannot select a file yet! Just hit OK', editable=False)
self.playerSaveLocation = self.add(npyscreen.TitleFilenameCombo, name="Your save file:")
self.parentApp.player = Player(self.parentApp.mainPath / 'playerdata/defaults/default_player.yaml')
self.add(npyscreen.MultiLineEdit, value=self.parentApp.gamelib['menu']['graphics']['logo'], editable=False)

View File

@ -0,0 +1,34 @@
from yaml_parser import parse_datafile as parse
from errors import ItemAlreadyTaken
class Player:
"""
This class intended to abstract out the actual yaml data into a player.(item) that is more
friendly to handle in code.
"""
def __init__(self, save_location):
self.save_location = save_location
self.playerData = parse(save_location)
def change_room(self, new_room):
"""
Should move the player to a new room
TODO: Put a check here that checks if the room we're moving to exists!
"""
self.playerData['player']['location'] = new_room
room = self.playerData['player']['location']
return parse('adventure_game/gamedata/world/' + room)['room']['upon_enter']
def add_inventory(self, item):
if item in self.playerData['player']['inventory']:
raise ItemAlreadyTaken
else:
self.playerData['player']['inventory'].append(item)
def remove_inventory(self, item):
pass

View File

@ -0,0 +1,83 @@
import pathlib
import npyscreen
import logging
from npyscreen import NotEnoughSpaceForWidget
from os import system
from yaml_parser import parse_datafile as parse
from GameNavigator import GameNavigator
from MainMenu import MainMenu
class AlphaWarning(npyscreen.Popup):
def afterEditing(self):
self.parentApp.setNextForm('MENU')
def create(self):
self.add(npyscreen.MultiLineEdit, value="""Welcome to Unnamed Adventure game!
This game is still in ALPHA! And a TON
of features are not implemented.
Please check out the git project for
details and updates! and please report
any bugs! Thank you and enjoy!
https://kitsunehosting.net/gitea/Kenwood/SNHU-IT-140/issues""",
editable=False)
class AdventureGame(npyscreen.NPSAppManaged):
"""
This is the 'root' of the entire game!
"""
# Do on creation
def onStart(self):
# Setup some important 'global' values we'll need later
# Set the path all other files will follow
self.mainPath = pathlib.Path(__file__).parent
# Setup logging
self.log = logging
self.log.basicConfig(filename=self.mainPath / 'logs/AdventureGame.log',
filemode='w',
level=logging.DEBUG)
self.log.info('Logging started!')
# parse this data first (since it includes graphics for the main menu
self.gamelib = parse(self.mainPath / 'gamedata/gamelib.yaml')
self.log.debug('Gamelib at {0}'.format(self.mainPath / 'gamedata/gamelib.yaml'))
# Intalize the player as none, the player will be created in the main menu.
self.player = None
# Set screen size before drawing windows
dimensions = self.gamelib['menu']['graphics']['dimensions']
#system('mode con: cols={0} lines={1}'.format(
# dimensions['inventory_width']+dimensions['art_width'],
# 30)) # TODO: Finish setting this up.
# Set theme
#TODO: modify custom theme?
npyscreen.setTheme(npyscreen.Themes.ElegantTheme)
# Draw game windows
self.addForm('GAME', GameNavigator, name='Unnamed Adventure Game') # This window should draw the actual game
self.addForm('MENU', MainMenu, name='Welcome to the main menu') # This window should draw the main menu
self.addForm('MAIN', AlphaWarning, name='Welcome to the alpha!') # This window is only needed for the ALPHA
# TODO: Create a 'splash screen' or, traditional "main menu"
if __name__ == '__main__':
# Set the screen size bigger
system('mode con: cols={0} lines={1}'.format(124, 36))
# Make a new adventure game if not imported
adventure_game = AdventureGame()
# Run the game!
try:
adventure_game.run()
except NotEnoughSpaceForWidget:
# This exception should catch if a player tries to play in a screen that is too small.
print('Your screen is too small!\nOr, Joe has not fixed https://kitsunehosting.net/gitea/Kenwood/SNHU-IT-140/issues/7')

View File

@ -0,0 +1,6 @@
class ItemAlreadyTaken(Exception):
pass
class ItemRequired(Exception):
pass

View File

@ -0,0 +1,62 @@
menu:
graphics:
logo: |
. __ __ __ ___ __ __
/ / / /___ ____ ____ _____ ___ ___ ____/ / / | ____/ / _____ ____ / /___ __________
/ / / / __ \/ __ \/ __ `/ __ `__ \/ _ \/ __ / / /| |/ __ / | / / _ \/ __ \/ __/ / / / ___/ _ \
/ /_/ / / / / / / / /_/ / / / / / / __/ /_/ / / ___ / /_/ /| |/ / __/ / / / /_/ /_/ / / / __/
\____/_/ /_/_/ /_/\__,_/_/ /_/ /_/\___/\__,_/ /_/ |_\__,_/ |___/\___/_/ /_/\__/\__,_/_/ \___/
_________ __ _________
/ ____/ | / |/ / ____/
/ / __/ /| | / /|_/ / __/
/ /_/ / ___ |/ / / / /___
\____/_/ |_/_/ /_/_____/
not_found: |
+--------------------------------------------------------------------------------NNN0ddk0000kkX--+
|--------------------------------------------------------------------------------N0OOkkkO0OkxON--|
|--------------------------------------------------------------------------------KkOkkkxkOxloON--|
|--------------------------------------------------------------------------------0xOO00000kdokX--|
|---------------------------------------------+/ \+-----------------------------XOxdddxdl:;cdX--|
|--------------------------------------------+/ !! \+----------------------------XOxoldxllc:oON--|
|-------------------------------------------+/ \+---------------------------0xkddkxdOxod0W--|
|------------------------------------+No Art for this location+------------------XkddkxodkxkxxX--|
|--------------------------------|Consider making a pull request?|---------NXXXKOOkxxxddodxkkOX--|
|--------------------------------------------------------------------------NXNNXxclddxkkxkOOO0N--|
|--------------------------------------------------------------------------XkxxxdddoxkkxkOkxkKW--|
|--------------------------------------------------------------------------KxxxxdollloodO0kkk0N--|
|--------------------------------------------------------------------------XkxxxdlclddkOOkk0OON--|
|--------------------------------------------------------------------------N0dooxdlcdkOkkkkxcoX--|
|--------------------------------------------------------------------------WXKKKKOOO0K0kxxxdodX--|
|------------------------------------------------------------------------------------------------|
|------------------------------------------------------------------------------------------------|
+------------------------------------------------------------------------------------------------+
start_to_continue: |
+ +
| |
| TYPE |
| START |
| TO BEGIN! |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+ +
dimensions:
inventory_width: 23
inventory_height: 20
art_width: 101
art_height: 20
dialogue_width: 122
dialogue_height: 20
entry_box_width: 122
entry_box_height: 3

View File

@ -0,0 +1,4 @@
blank_room:
grid: [-1, -1]
upon_enter: "You're in a blank room. It looks unfinished, like joe forgot to put something here"
look_around: "There is nothing to look at, you should tell joe you're here."

View File

@ -0,0 +1,20 @@
room:
name: "The Hallway"
grid: [0, 1]
upon_enter: "You are standing just outside your office door."
look_around: "You look up, and down the hallway, you see the receptionists |desk|. A |closet| at the other end of the hall and the |Office Door| Behind you."
closet:
look_at: "Its a closet door."
inspect: "Its still a closet door."
desk:
look_at: "Its your receptionists desk, she's not sitting behind it."
inspect: "She's still not there."
office_door:
leads_to: office.yaml
look_at: "Its a door, it leads to your office. You should try to |OPEN| it."
inspect: "Its ornate but basic, this side of the door has your name on a |plaque|."
plaque:
item_name: "Office Door Plaque"
look_at: "Its a small brass plate, it has your name on it."
inspect: "Its still a small brass plate, and it has your name on it. Beyond that its nothing special."
pick_up: "You tear off the *PLAQUE*, and now you're stuck with it i guess."

View File

@ -0,0 +1,39 @@
room:
name: "Your Office"
grid: [0, 0]
upon_enter: "You are standing behind your desk, you see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|"
look_around: "You look around the room, you see a |DESK|, a |BOOKSHELF| and the |DOOR|"
desk:
look_at: "You move to stand behind your desk. You see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|"
inspect: "The desk is large and ornate with one of those silly lamps hovering over it."
bookshelf:
look_at: "The bookshelf is a bookshelf."
inspect: "Its still a bookshelf."
log_viewer:
item_name: "Log Viewer"
look_at: "The log viewer is a small piece of ornate code, allowing you to examine |entities| more closely."
inspect: "The dials are wiggly and the viewer makes Beep Boop sounds sometimes."
pick_up: "You pick up the *LOG VIEWER*."
door:
leads_to: hallway.yaml
look_at: "Its a door, it leads to the Hallway. You should try to |OPEN| it."
inspect: "Its ornate but basic, the other side of the door has your name on a plaque."
art: |
+ +
| |
| OFFICE STUFF? |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+ +

View File

@ -0,0 +1,14 @@
INFO:root:Logging started!
DEBUG:root:Gamelib at adventure_game\gamedata\gamelib.yaml
DEBUG:root:Send button pressed!
DEBUG:root:{'grid': [0, 0], 'upon_enter': 'You are standing behind your desk, you see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'look_around': 'You look around the room, you see a |DESK|, a |BOOKSHELF| and the |DOOR|', 'pick_up_logviewer': 'You pick the *LOG VIEWER* up.', 'desk': {'look_at': 'You move to stand behind your desk. You see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'inspect': 'Desk, ornate, stuff'}, 'log_viewer': {'look_at': 'logviewer looks like garbo', 'inspect': 'beep boop', 'pick_up': 'You pick up the *LOG VIEWER*.'}}
INFO:root:Parsed command "LOOK" with arguments "['AROUND']"
DEBUG:root:Send button pressed!
DEBUG:root:{'grid': [0, 0], 'upon_enter': 'You are standing behind your desk, you see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'look_around': 'You look around the room, you see a |DESK|, a |BOOKSHELF| and the |DOOR|', 'pick_up_logviewer': 'You pick the *LOG VIEWER* up.', 'desk': {'look_at': 'You move to stand behind your desk. You see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'inspect': 'Desk, ornate, stuff'}, 'log_viewer': {'look_at': 'logviewer looks like garbo', 'inspect': 'beep boop', 'pick_up': 'You pick up the *LOG VIEWER*.'}}
INFO:root:Parsed command "LOOK" with arguments "['AT', 'DESK']"
DEBUG:root:Send button pressed!
DEBUG:root:{'grid': [0, 0], 'upon_enter': 'You are standing behind your desk, you see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'look_around': 'You look around the room, you see a |DESK|, a |BOOKSHELF| and the |DOOR|', 'pick_up_logviewer': 'You pick the *LOG VIEWER* up.', 'desk': {'look_at': 'You move to stand behind your desk. You see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'inspect': 'Desk, ornate, stuff'}, 'log_viewer': {'look_at': 'logviewer looks like garbo', 'inspect': 'beep boop', 'pick_up': 'You pick up the *LOG VIEWER*.'}}
INFO:root:Parsed command "LOOK" with arguments "['AT', 'DOOR']"
DEBUG:root:Send button pressed!
DEBUG:root:{'grid': [0, 0], 'upon_enter': 'You are standing behind your desk, you see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'look_around': 'You look around the room, you see a |DESK|, a |BOOKSHELF| and the |DOOR|', 'pick_up_logviewer': 'You pick the *LOG VIEWER* up.', 'desk': {'look_at': 'You move to stand behind your desk. You see a |NAMEPLATE|, a |TAPE RECORDER| and your trusty |LOG VIEWER|', 'inspect': 'Desk, ornate, stuff'}, 'log_viewer': {'look_at': 'logviewer looks like garbo', 'inspect': 'beep boop', 'pick_up': 'You pick up the *LOG VIEWER*.'}}
INFO:root:Parsed command "LOOK" with arguments "['AT', 'BOOKSHELF']"

View File

@ -0,0 +1,4 @@
player:
name: 'Default'
location: 'office.yaml'
inventory: ['Detective Hat']

View File

@ -0,0 +1,12 @@
import yaml
def parse_datafile(file):
# With the file open
with open(file, 'r') as stream:
# Try to read it and return it
try:
content = yaml.safe_load(stream)
return content
except yaml.YAMLError as exc:
return exc

0
Adventure Game/readme.md Normal file
View File

View File

@ -0,0 +1,2 @@
npyscreen~=4.10.5
PyYAML~=5.1.2