不确定如何让我的调平系统工作

问题描述 投票:0回答:1

我一直在用 python 开发一个基于文本的游戏,我已经到了一个地步,我正在尝试创建一个健身房类型的升级系统,每次你选择该选项时,它都会将级别提高一个,我遇到的问题是我必须在函数内定义,但如果我在函数内定义它,它将是我定义的,所以我应该怎么做才能让它起作用?

我期待第一次 1 第二次 2 第三次 3 等等的输出。我尝试了很多不同的方法,但我将包括当前的方法,以便您理解代码。

import time
import sys


def add(P, Q):    
      return P + Q   
def subtract(P, Q):   
      return P - Q   
def multiply(P, Q):    
      return P * Q   
def divide(P, Q): 
      return P / Q
def mainmenu():
  ks = 0
  ms = 0
  vs = 0
  es = 0
  print("\033[1;37;40m1 = to become the greatest knight ever!")
  print("2 = To become the greatest mage ever!")
  print("3 = To become the greatest viking ever!")
  print("4 = To become the greatest engineer ever!")
  print()
  charselect = input()
  print() 
  if charselect == ("1"):
      chartype = ("knight")
  if charselect == ("2"):
      chartype = ("mage")
  if charselect == ("3"):
      chartype = ("viking")
  if charselect == ("4"):
      chartype = ("engineer")
  delprint(f"\033[1;34;40mAh so you wish to become the greatest {chartype} a noble dream indeed, I wish you luck on your travels. oh and before you goyou'll need this.")
  if charselect == ("1"):
      print()
      delprint("\033[1;30;40m*godfrey hands you a rusty but well made sword*.")
      print()
      print()
      delprint("\033[1;37;40mthank you")
      print()
      print()
      delprint("\033[1;34;40maie")
      print()
  if charselect == ("2"):
      print()
      delprint("\033[1;30;40m*godfrey hands you a mossy but well made staff*.")
      print()
      print()
      delprint("\033[1;37;40mthank you")
      print()
      print()
      delprint("\033[1;34;40maie")
      print()
  if charselect == ("3"):
      print()
      delprint("\033[1;30;40m*godfrey hands you a rusty but well made axe with intricate engravings*.")
      print()
      print()
      delprint("\033[1;37;40mthank you")
      print()
      print()
      delprint("\033[1;34;40maie")
      print()
  if charselect == ("4"):
      print()
      delprint("\033[1;30;40m*godfrey hands you a rusty but well made wrench with a copper finish*.")
      print()
      print()
      delprint("\033[1;37;40mthank you")
      print()
      print()
      delprint("\033[1;34;40maie")
      print()
  gamebeginning = ("true")
  print()
  def mainmenu1():
        print(f"\033[1;37;40m1 = {chartype} training")
        print("2 = rest")
        print("3 = go to the ring of battle")
        print("4 = look around town for memorys")
        mmc = input()
        if mmc == ("1"):
          print()
          delprint(f"You decide to work on becomeing a better {chartype}")
          print()
          
          if chartype == ("knight"):
            ks 
            ks += 1
            print(ks)
            delprint(f"Level as a {chartype} has increased by one to {ks}!") 
          if chartype == ("mage"):
            ms + 1
            delprint(f"Level as a {chartype} has increased by one to {mainmenus}!") 
          if chartype == ("viking"):
            vs + 1
            delprint(f"Level as a {chartype} has increased by one to {vs}!") 
          if chartype == ("engineer"):
            es + 1
            delprint(f"Level as a {chartype} has increased by one to {es}!") 
            
          
          
      
          print()
          mainmenu1()
  mainmenu1()
python logic
1个回答
0
投票

所以一些想法:

你不需要在

mainmenu1
中定义
mainmenu
,你可以这样做:

def mainmenu():
  # some stuff
  mainmenu1(chartype, **kwargs) # kwargs here are other variables that mainmenu1 needs

def mainmenu1(chartype):
  # somestuff
  print(f"{chartype}")  # as an example

mainmenu()  # when this is called, it will run mainmenu which in turn runs mainmenu1, you dont' need to define mainmenu1 within mainmenu for this to work

我还建议开始学习面向对象的编程。例如,您可能有一个角色类,其中包含骑士、法师、维京人等拥有角色属性的子类。像这样的东西:

class Character:
  def __init__(self, character_name):
    self.character_name = character_name
    self.stats = {
      'strength':0,
      'intellect':0
    }
  def raise_main_stat(self, stat, amount):
    self.stats[stat]+=amount

class Knight(Character):
  def __init__(self, character_name):
    super().__init__(character_name)  # calls init of the base class
    self.stats = {
      'strength':5, # base properties all knights start with
      'intellect':1
    }
    self.main_stat = "strength"

当玩家选择骑士时,您实例化骑士类:

def mainmenu():
  choice = input()
  if choice == '1':
    player = Knight('my_name')
  mainmenu1(player)

def mainmenu1(player):
  choice = input()
  if choice == '1':
    player.raise_main_stat(stat=player.main_stat, amount=1)
    print(f"player {player.character_name} trained and raised {player.main_stat} by 1 to {player.stats[player.main_stat]}")

只需调用

mainmenu()
并输入
1
两次即可尝试上述操作。

© www.soinside.com 2019 - 2024. All rights reserved.