簡體   English   中英

在 Python 中的基於文本的 RPG 中創建一個簡單的經驗點獲取和升級系統

[英]Creating a simple experience points gaining and leveling up system in a text based RPG in Python

如果需要,請隨時編輯這篇文章。

嗨,我已經學習python兩個月了,我剛剛完成了函數和藍豆邏輯的學習。 我決定在我不在課堂上的時候嘗試做一個文本冒險,看看在學期結束時結果如何。 我的導師本周或下周沒有時間,所以我在這里提出我的問題。 我覺得我的問題與我還沒有完全理解全局變量的工作原理有關,因為我剛剛開始使用它們,或者我是否應該為此目的使用它們來存儲數據。 我對這個游戲的想法本質上是運行調用一堆函數,當我需要它們時,我會調用它們,因為我覺得它對我所知道的來說非常有效。 如果您要運行此代碼,則會出現錯誤“位置參數丟失”。 我在這里可以做些什么更好? 我也嘗試集成一個函數來測試它,以便更容易為我提供幫助。

tldr; 我怎樣才能使我試圖設想的經驗值系統工作,以便它節省獲得的經驗值數量,然后將該數字添加到大多數 RPG 游戲中對下一個級別的新的更高的經驗要求中? 例如,添加五個經驗后,程序會出錯。 提前感謝您閱讀本文! 我希望我充分解釋了我的問題。

這是我到目前為止提出的代碼

global playername
playername = "Name Jeff 21 Bruh"
global playerlvl
playerlvl = 0
global xpcurrent
xpcurrent = 0
global xpnext
xpnext = int(((playerlvl * 4) / 3) + 4)
global xpgained
xpgained = 0

def show_stats():
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

def lvlup(xpgained):
    global xpcurrent
    xpgainedhere = xpgained
    xpcurrent += xpgained
    xpgained = 0
    if xpcurrent >= xpnext:
        playerlvl + 1
        xpcurrent -= xpnext
        print("~ ~", playername, "is Lvl", playerlvl, "! ~ ~")
        if xpcurrent >= xpnext:
            lvlup()
        else:
            show_stats()
    else:
        print("~ ~", playername, "gained", xpgainedhere, "Exp! ~ ~")
        show_stats()

def game_main():
        print("*" * 50)
        print("This is where the game begins.")
        # ------just before this \n (the '.' )is the character limit
        # ---------------------------------------------------------V
        print("The player after lines of text can enter commands.\n"
              "use this to enter commands to test them.")
        print("*" * 50, "\n")

        def testexp():
            addexp = int(input("type the integer for how much exp "
                               "that you would like to add\n"))
            lvlup(addexp)

        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()
        testexp()

game_main()

全局變量通常是一個壞主意。 嘗試將您的函數轉換為使用參數而不是全局變量。

這部分

def show_stats():
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

會成為

def show_stats(playername, playerlvl, xpcurrent, xpnext):
        print("*" * 20, "\n")
        print(playername, "the Adventurer")
        print("Lvl", playerlvl)
        print("EXP =", xpcurrent, '/', xpnext)
        print("-_" * 16 + '\n')

使用這個新函數而不是將所有變量設為全局變量

playername = "Name Jeff 21 Bruh"
playerlvl = 0
xpcurrent = 0
xpnext = int(((playerlvl * 4) / 3) + 4)
show_stats(playername, playerlvl, xpcurrent, xpnext)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM