Skip to content

Commit

Permalink
Merge pull request #5 from zenandreas/character-menu
Browse files Browse the repository at this point in the history
Optimised menu and changed character storing location to a json file
  • Loading branch information
Mircea Dumitrescu committed Aug 23, 2021
2 parents f6c2d3d + 22f8a36 commit 7e042a2
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ cython_debug/

#Custom
test.py
data.json
data
*.json
Binary file removed __pycache__/app.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/help.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/help_info.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/inventory.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/methods.cpython-38.pyc
Binary file not shown.
Binary file removed __pycache__/methods.cpython-39.pyc
Binary file not shown.
Binary file removed __pycache__/text_input.cpython-38.pyc
Binary file not shown.
10 changes: 0 additions & 10 deletions data.json

This file was deleted.

162 changes: 97 additions & 65 deletions methods.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import sys
import time
import os
import json
import math
from text_input import text_input
from inventory import get_inventory
from inventory import add_inventory
from map import player_movement
from character import Player

myPlayer = Player()

def cls():
os.system('cls' if os.name=='nt' else 'clear')

def read(file: str):
return json.load(open(file))
def write(file: str, data: dict):
with open(file, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)

##### Defined Function to type words slowly
def type_sys_text(txt):
i = 0
speed = 30 #Ronan's prefrence is 30
#This to write {speed} chars at once since the limitation of float number is 10^-9 aka,
# the time.sleep is not fast enought
while i < len(txt):
delta = len(txt) - i - speed
if delta <= 0:
speed = speed - abs(delta) #Prevent out of range
for b in range(0, speed):
sys.stdout.write(txt[i + b])
sys.stdout.flush()

time.sleep(math.pow(10, -9))
i += speed

starting_kits = {
"MEDIC": {
"POTIONS": 3,
Expand All @@ -24,6 +51,45 @@
"KEYS": 3 }
}

player = {}

if not os.path.exists('data/character.json'):
write('data/character.json', {})
else:
player = read('data/character.json')


def welcome():
welcome = """
#####################################################
_____ __ __
/ ___/ / /_ ____ _ ____/ /____ _ __
\__ \ / __ \ / __ `// __ // __ \| | /| / /
___/ // / / // /_/ // /_/ // /_/ /| |/ |/ /
/____//_/ /_/ \__,_/ \__,_/ \____/ |__/|__/
__ ___ ____ ____ ______
/ |/ /____ ____ ____ / __ \ / __ \ / ____/
/ /|_/ // __ \ / __ \ / __ \ / /_/ // /_/ // / __
/ / / // /_/ // /_/ // / / / / _, _// ____// /_/ /
/_/ /_/ \____/ \____//_/ /_/ /_/ |_|/_/ \____/
######################################################
Copyright Python Task Force 2021
[1] Play <
[2] Quit <
"""

##### Calling out the function from methods.py to type the welcome message when program run

type_sys_text(welcome)
menu = {"1": setup_game,
"2": sys.exit
}
text_input("> ", menu)
game_menu()

def setup_game():
name = input("""
Expand All @@ -33,9 +99,7 @@ def setup_game():
How should we call you?
> """).capitalize()
#print(f"Your name is {name}")
myPlayer.name = name

selected_kit = input("""
_________________________________________
| WHAT IS YOUR JOB? |
Expand All @@ -53,39 +117,38 @@ def setup_game():
Make sure you type the correct job name as written above!
> """).upper()
else:
#once the player chooses the kit we will store it in selected_kit
myPlayer.starting_kit = selected_kit

#to print their inventories
starting_potions = starting_kits[myPlayer.starting_kit]["POTIONS"]
starting_bullets = starting_kits[myPlayer.starting_kit]["BULLETS"]
starting_keys = starting_kits[myPlayer.starting_kit]["KEYS"]
#once the player chooses the kit we will store it in data/character.json
player[selected_kit] = {
"potions": starting_kits[selected_kit]["POTIONS"],
"bullet": starting_kits[selected_kit]["BULLETS"],
"keys": starting_kits[selected_kit]["KEYS"]
}
write('data/character.json', player)
#to print their inventories
starting_potions = starting_kits[selected_kit]["POTIONS"]
starting_bullets = starting_kits[selected_kit]["BULLETS"]
starting_keys = starting_kits[selected_kit]["KEYS"]

print(f"""
_________________________________________
| YOU ARE NOW A {selected_kit} |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
You will start with {starting_potions} potions,
{starting_bullets} bullets and {starting_keys} keys
in your inventory!
""")
time.sleep(5)


print(f"""
_________________________________________
| YOU ARE NOW A {selected_kit} |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
You will start with {starting_potions} potions,
{starting_bullets} bullets and {starting_keys} keys
in your inventory!
""")
time.sleep(5)



##### Defined Function to type words slowly
def type_sys_text(i):
for char in i:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.003)
# player_input = input("> ")
# os.system("clear")

##### Defined Function to call inventory
### this will have more complex features in the future
def display_inventory():
cls()
inventory = f"""
===============================================================
Expand All @@ -104,48 +167,14 @@ def display_inventory():
[3] {get_inventory("slot3")} Keys [6] {get_inventory("slot6")} Apples
"""
for char in inventory:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.001)
# player_input = input("> ")
# os.system("clear")
type_sys_text(inventory)
time.sleep(5)
game_menu()

def welcome():
welcome = """

#####################################################
_____ __ __
/ ___/ / /_ ____ _ ____/ /____ _ __
\__ \ / __ \ / __ `// __ // __ \| | /| / /
___/ // / / // /_/ // /_/ // /_/ /| |/ |/ /
/____//_/ /_/ \__,_/ \__,_/ \____/ |__/|__/
__ ___ ____ ____ ______
/ |/ /____ ____ ____ / __ \ / __ \ / ____/
/ /|_/ // __ \ / __ \ / __ \ / /_/ // /_/ // / __
/ / / // /_/ // /_/ // / / / / _, _// ____// /_/ /
/_/ /_/ \____/ \____//_/ /_/ /_/ |_|/_/ \____/
######################################################
Copyright Python Task Force 2021
[1] Play <
[2] Quit <
"""

##### Calling out the function from methods.py to type the welcome message when program run

type_sys_text(welcome)
menu = {"1": setup_game,
"2": sys.exit
}
text_input("> ", menu)
game_menu()

def help_game():
cls()
help_info = """
____ ____ ________ _____ _______
Expand Down Expand Up @@ -201,13 +230,16 @@ def game_menu():
|________________________________________________________|
"""
type_sys_text(game_options)
menu = {"m": get_direction,
menu = {
"m": get_direction,
"h": help_game,
"i": display_inventory,}
"i": display_inventory
}
text_input("> ", menu)


def get_direction():
cls()
directions = """
_________________________________________________________
| |
Expand Down
28 changes: 0 additions & 28 deletions test.py

This file was deleted.

0 comments on commit 7e042a2

Please sign in to comment.