I'm new to python and created this small program. It's basically a skeleton for a simple text-based RPG. The program is one menu that allows you to:
- Create a Character
- View Character Stats/Attributes
- Load Character
- Battle
- Save
- Quit
I'm looking for constructive criticism from any angle, be that if I comment too much, if I'm not comment to specific, if my variables are to implicit, or any other flaws in my programming. Perhaps a better way for a menu but this time using tuples or dictionaries. Perhaps you noticed a redundancy in my code.
I'd also appreciate if someone can give me some direction if my goal is to make a top-side RPG. Should I use tkinter? PyGame? Tmxlib? Or should I just focus on the programming aspect for now until I'm a bit more experienced before tackling a library?
# Pickle for object serialization, OS to check if character data file exist
import pickle
import os
class Character(object):
def __init__(self, name, exp):
self.name = name
self.exp = exp
self.lvl = self.exp/100
# Simple character statistic display using the new format technique instead of %
def stats(self):
print ("Character stats: {}, you are level {} with {} experience points!".format(self.name,self.lvl,self.exp))
# Simple battle program, has one paramter for the amount of EXP
# self.lvl is necessary to update the self.lvl variable with the actual level
def battle(self,exp):
self.exp = self.exp + exp
self.lvl = self.exp/100
print ("Successful battle! {} experienced gained!".format(exp))
self.stats()
# Not sure what to comment for starting variables? "Global variables?" "Initial variables"?
# sets menu on loop and player sees the main menu
loop_menu = True
menu_choice = "main"
# error prevention
new_character = None
fileCheck = "charData"
# menu_display
# 1 = menu with input
# "new" = input
menu_display = 1
# menu_error
# None = no error yet
# 1 = invalid input received
menu_error = None
while loop_menu == True:
if menu_display == 1:
print("New Character, Stats, Load, Battle, Save or Quit?")
menu_choice = (input("Input your choice: ")).lower()
menu_display = 0
if menu_display == "new" and menu_error == None:
menu_choice = (input("What would you like to do now?: ")).lower()
if menu_display == "new" and menu_error == 1:
menu_choice = (input("Please type a real answer, what now?: ")).lower()
menu_error = None
# Text Formatting
print ("\n", end="")
if menu_choice == "new":
chardata = ["Foo", 300]
new_character = Character(*chardata)
print ("New character created!")
new_character.stats()
elif menu_choice == "stats":
if new_character == None:
print ("Sorry, you can't check your stats, you haven't made a new/loaded a character yet!")
else:
new_character.stats()
elif menu_choice == "load":
if os.path.exists(fileCheck):
print("Load successful!")
inFile = open("charData", "rb")
new_character = pickle.load(inFile)
inFile.close()
else:
print("You can't load yet! Data file not created!")
print("You can use the \"save\" feature after you've created a character to automatically create a save file!")
elif menu_choice == "battle":
if new_character == None:
print("You can't battle, you haven't made a character")
else:
new_character.battle(100)
# Saving character to data file
elif menu_choice == "save":
print ("Character saved!")
outFile = open("charData", "wb")
pickle.dump(new_character, outFile)
outFile.close()
elif menu_choice == "quit":
print ("Quitting program...")
break
else:
menu_error = 1
# Text Formatting
print ("\n", end="")
# Skips the instructions and goes straight to the input
menu_display = "new"