-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAn_RPG.py
80 lines (68 loc) · 2.81 KB
/
An_RPG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# # This program is going to merge the three programs and generate a single game
# # Step 1: The registration process
# # Made a register file by the name of "register.txt"
# # # # # # Step 1 - 1: Defining a function/class, each for login and registeration.
import json
def login():
print("Login...")
print("###########################################################")
name = input("Username : ")
password = input("Password : ")
print("###########################################################")
saveFile = name + ".txt"
with open("register.json", "r") as j:
check = json.load(j)
if name in check.keys():
for key, value in check.items():
if key == name and value == password:
try:
with open(saveFile, "r") as f:
status = f.read()
print(status)
points = int(status)
except FileNotFoundError:
print("SaveFile not found") # we can repeat a new savefile making process here if the file is deleted somehow
def register():
x = True
while x == True:
z = 0
print("###########################################################")
print("Enter new user name")
name = input("Username : ")
password = input("Password : ")
print("###########################################################")
saveFile = name + ".txt"
with open("register.json") as f:
check = json.load(f)
print(check)
for a in check: # check for name availability
if a == name:
print("Name has already been taken")
z = 1
break
if z == 1:
continue
else:
with open("register.json", "r+") as f:
data = json.load(f)
dict1 = {name : password}
data.update(dict1)
f.seek(0)
json.dump(data, f)
with open(saveFile, "w") as f:
f.write("0")
print("Savefile made")
break
loginIn = False
while loginIn == False:
print("""Enter 'r' to register a new user
Enter 'l' to login an existing user""")
choice = input()
if choice == "l":
login()
loginIn = True
elif choice == "r":
register()
print("Registration is complete, continue to login")
print("###########################################################")
login()