大约一年前,我开始了一个项目,涉及一个使用 Python 3 的简单的基于终端的 RPG。没有真正考虑它,我就跳入了它。我开始为每个脚本组织多个脚本......好吧,功能。但是在项目进行到一半时,对于最终目标,我不确定只有一个非常大的脚本文件或多个文件是否更容易/更有效。
由于我将cmd
模块用于终端,我意识到让实际的应用程序运行成为一个循环游戏可能对所有这些外部文件具有挑战性,但同时我有一个__init__.py
文件来组合所有功能主运行脚本。这是文件结构。
澄清一下,我不是最伟大的程序员,而且我是 Python 的新手。我不确定该cmd
模块的兼容性问题。
所以我的问题是这样的;我应该保持这个结构并且它应该按预期工作吗?还是应该将所有这些assets
脚本合并到一个文件中?或者甚至将它们与使用的 start.py 分开cmd
?这是开始功能,以及各种脚本的一些片段。
开始.py
from assets import *
from cmd import Cmd
import pickle
from test import TestFunction
import time
import sys
import os.path
import base64
class Grimdawn(Cmd):
def do_start(self, args):
"""Start a new game with a brand new hero."""
#fill
def do_test(self, args):
"""Run a test script. Requires dev password."""
password = str(base64.b64decode("N0tRMjAxIEJSRU5ORU1BTg=="))
if len(args) == 0:
print("Please enter the password for accessing the test script.")
elif args == password:
test_args = input('> Enter test command.\n> ')
try:
TestFunction(test_args.upper())
except IndexError:
print('Enter a command.')
else:
print("Incorrect password.")
def do_quit(self, args):
"""Quits the program."""
print("Quitting.")
raise SystemExit
if __name__ == '__main__':
prompt = Grimdawn()
prompt.prompt = '> '
#ADD VERSION SCRIPT TO PULL VERSION FROM FOR PRINT
prompt.cmdloop('Joshua B - Grimdawn v0.0.3 |')
测试.py
from assets import *
def TestFunction(args):
player1 = BaseCharacter()
player2 = BerserkerCharacter('Jon', 'Snow')
player3 = WarriorCharacter('John', 'Smith')
player4 = ArcherCharacter('Alexandra', 'Bobampkins')
shop = BaseShop()
item = BaseItem()
#//fix this to look neater, maybe import switch case function
if args == "BASE_OFFENSE":
print('Base Character: Offensive\n-------------------------\n{}'.format(player1.show_player_stats("offensive")))
return
elif args == "BASE_DEFENSE":
print('Base Character: Defensive\n-------------------------\n{}'.format(player1.show_player_stats("defensive")))
return
* * *
播放器.py
#import functions used by script
#random is a math function used for creating random integers
import random
#pickle is for saving/loading/writing/reading files
import pickle
#sys is for system-related functions, such as quitting the program
import sys
#create a class called BaseCharacter, aka an Object()
class BaseCharacter:
#define what to do when the object is created, or when you call player = BaseCharacter()
def __init__(self):
#generate all the stats. these are the default stats, not necessarily used by the final class when player starts to play.
#round(random.randint(25,215) * 2.5) creates a random number between 25 and 215, multiplies it by 2.5, then roudns it to the nearest whole number
self.gold = round(random.randint(25, 215) * 2.5)
self.currentHealth = 100
self.maxHealth = 100
self.stamina = 10
self.resil = 2
self.armor = 20
self.strength = 15
self.agility = 10
self.criticalChance = 25
self.spellPower = 15
self.intellect = 5
self.speed = 5
self.first_name = 'New'
self.last_name = 'Player'
self.desc = "Base Description"
self.class_ = None
self.equipment = [None] * 6
#define the function to update stats when the class is set
def updateStats(self, attrs, factors):
#try to do a function
try:
#iterate, or go through data
for attr, fac in zip(attrs, factors):
val = getattr(self, attr)
setattr(self, attr, val * fac)
#except an error with a value given or not existing values
except:
raise("Error updating stats.")
#print out the stats when called
#adding the category line in between the ( ) makes it require a parameter when called
def show_player_stats(self, category):
* * *
笔记
脚本的目的是展示它们具有什么样的结构,因此它有助于支持我是否应该合并的问题