首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python3.0中基于文本的战斗游戏

Python3.0中基于文本的战斗游戏
EN

Code Review用户
提问于 2019-07-11 02:56:12
回答 1查看 2.5K关注 0票数 5

几年后,我开始练习Python,在我的第一个项目中,我决定用不同的角色制作一个类似于战斗/rpg的游戏。到目前为止,我只熟悉循环和函数,不熟悉类或OOP。请给我任何反馈,说明我如何通过调试、优化或添加更多的内容来改进我的代码(如果游戏由于没有highscore.txt而没有开始,请创建一个名为highscore的文本文件,在第一行中插入一个插槽,在第二个输入任何名称。这种情况不应该发生,只是以防万一:P)。

代码语言:javascript
运行
复制
import random as r

######Getting High Score#####

try:
    hs = open("highscore.txt","r+")
except:

    hs = open("highscore.txt","x")
    hs = open("highscore.txt","r+")

try:
    score = hs.readlines(1)
    score = int(score[0])

    leader = hs.readlines(2)
    leader = str(leader[0])
except:
    hs = open("highscore.txt","w")
    hs.write("0\n")
    hs.write("null")
    hs = open("highscore.txt","r")
    score = hs.readlines(1)
    score = int(score[0])
    leader = hs.readlines(2)
    leader = str(leader[0])

#####Introduction, Initializing player#####

print("\nWELCOME TO WONDERLANDS RPG!")
print("The High Score is:",score,"by",leader)
points = 0
player_name = input("\nEnter your hero's name: ")

#####Classes [health, attack 1 (only does set damage), attack 2 min, attack 2 max, attack 3 min, attack 3 max, heal min, heal max], Getting player's Class#####

knight = [100,10,5,15,0,25,5,10] #health: 100, attack 1: 10, attack 2: 5-15, attack 3: 5-25, heal: 5-10
mage = [50,15,10,20,-5,25,10,15] #health: 50, attack 1: 15, attack 2: 10-20, attack 3: -5-25, heal: 10-15
healer = [150,5,5,10,5,15,10,20] #health: 150, attack 1: 5, attack 2: 5-10, attack 3: 5-15, heal: 10-20

while True:
    print("\n1. Knight: Health: ",knight[0],"; Attack 1:",knight[1],"; Attack 2:",knight[2],"-",knight[3],"; Attack 3:",knight[4],"-",knight[5],"; Heal:",knight[6],"-",knight[7])
    print("2. Mage: Health: ",mage[0],"; Attack 1:",mage[1],"; Attack 2:",mage[2],"-",mage[3],"; Attack 3:",mage[4],"-",mage[5],"; Heal:",mage[6],"-",mage[7])
    print("3. Healer: Health: ",healer[0],"; Attack 1:",healer[1],"; Attack 2:",healer[2],"-",healer[3],"; Attack 3:",healer[4],"-",healer[5],"; Heal:",healer[6],"-",healer[7])
    player_class = input("\nSelect your class: 1, 2, or 3: ")
    if player_class == "1":
        player_class = knight
        print("You have selected the Knight")
        break
    if player_class == "2":
        player_class = mage
        print("You have selected the Mage")
        break
    if player_class == "3":
        player_class = healer
        print("You have selected the Healer")
        break
    else:
        print("Please select a valid class.")
        continue

player_heal_max = player_class[0]

#####Difficulty/Upgrade Functions#####

def level_up(player,health_max):
    while True:
        lv_choice = input("\nWould you like to:\n Increase max health by 20 (1)\n Increase Healing Factor by 5 (2)\n increase your damage by 5 (3) ")
        if lv_choice == "1":
            health_max += 20
            player[0] = health_max
            return player,health_max
        elif lv_choice == "2":
            player[6] +=5
            player[7] +=5
            player[0] = health_max
            return player, health_max
        elif lv_choice == "3":
            player[1] +=5
            player[2] +=5
            player[3] +=5
            player[4] +=5
            player[5] +=5
            player[0] = health_max
            return player, health_max
        else:
            print("Please enter in a valid number")
            continue

def difficulty(ai,health_max,level):
    if level == 1:
        return ai
    else:
        ai[0] = health_max+15*round(0.5*level+0.5)
        ai[1] +=5*round(0.5*level+0.5)
        ai[2] +=5*round(0.5*level+0.5)
        ai[3] +=5*round(0.5*level+0.5)
        ai[4] +=5*round(0.5*level+0.5)
        ai[5] +=5*round(0.5*level+0.5)
        ai[6] +=5*round(0.5*level+0.5)
        ai[7] +=5*round(0.5*level+0.5)
        return ai

def ai_stats(s):
    s[0] += r.randint(-20,20)
    s[1] += r.randint(-3,3)
    s[2] += r.randint(-3,3)
    s[3] += r.randint(-3,3)
    s[4] += r.randint(-3,3)
    s[5] += r.randint(-3,3)
    s[6] += r.randint(-3,3)
    s[7] += r.randint(-3,3)
    return s

#####Game Loop#####

level = 1
print("\n----------------------- GAME START -----------------------")

while True:
    #####Determining AI Class/Stats#####
    #####(AI classes must be in the Game loop otherwise if an enemy chooses the same class twice, it would have <=0 HP, thus being an instant win)#####
    ai_knight = [100,10,5,15,0,25,5,10] #health: 100, attack 1: 10, attack 2: 5-15, attack 3: 5-25, heal: 5-10
    ai_mage = [50,15,10,20,-5,25,10,15] #health: 50, attack 1: 15, attack 2: 10-20, attack 3: -5-25, heal: 10-15
    ai_healer = [150,5,5,10,5,15,10,20] #health: 150, attack 1: 5, attack 2: 5-10, attack 3: 5-15, heal: 10-20

    ai = r.randint(1,3)
    if ai == 1:
        ai = ai_stats(ai_knight)
        print("\nYou are fighiting a knight with",ai[0],"HP!")
    if ai == 2:
        ai = ai_stats(ai_mage)
        print("\nYou are fighiting a mage with",ai[0],"HP!")
    if ai == 3:
        ai = ai_stats(ai_healer)
        print("\nYou are fighiting a healer with",ai[0],"HP!")

    ai_heal_max = ai[0]
    ai = difficulty(ai,ai_heal_max,level)
    #####Gameplay Loop#####
    while True:
        #####Player Attack#####
        player_move = input("\nWould you like to use attack (1), attack (2), attack (3), or heal (4)? ")
        print("")
        if player_move == "1":
            player_damage = player_class[1]
            ai[0] = ai[0]- player_damage
            print(player_name," did",player_damage,"damage!")
        elif player_move == "2":
            player_damage = r.randint(player_class[2],player_class[3])
            ai[0] = ai[0]- player_damage
            print(player_name," did",player_damage,"damage!")
        elif player_move == "3":
            player_damage = r.randint(player_class[4],player_class[5])
            if player_damage<0:
                player_class[0] = player_class[0]+player_damage
                print(player_name," damaged themselves for",player_damage,"HP!")
            else:
                ai[0] = ai[0]- player_damage
                print(player_name," did",player_damage,"damage!")
        elif player_move == "4":
            player_heal = r.randint(player_class[6],player_class[7])
            if player_class[0] + player_heal > player_heal_max:
                player_class[0] = player_heal_max
            else:
                player_class[0] = player_class[0] + player_heal
            print(player_name," healed for",player_heal,"HP")
        else:
            print("Please enter in a valid move.")
            continue
        #####Detecting Death#####
        if player_class[0]<=0:
            break
        elif ai[0]<=0:
            points += player_class[0]*level
            level +=1
            print("You have bested your opponent! You Have",points,"points. \nNow starting level",level)
            player_class,player_heal_max = level_up(player_class,player_heal_max)
            break
        #####AI Turn#####
        if ai[0] <= (ai_heal_max/5):
            ai_move = r.sample(set([1,2,3,4,4,4]), 1)[0]
        elif ai[0] >= (ai_heal_max*.8):
            ai_move = r.sample(set([1,2,3,1,2,3,4]), 1)[0]
        elif ai[0] == ai_heal_max:
            ai_move = r.randint(1,3)
        else:
            ai_move = r.randint(1,4)

        if ai_move == 1:
            ai_damage = ai[1]
            player_class[0] = player_class[0]- ai_damage
            print("Your opponent did",ai_damage,"damage!")
        elif ai_move == 2:
            ai_damage = r.randint(ai[2],ai[3])
            player_class[0] = player_class[0]- ai_damage
            print("Your opponent did",ai_damage,"damage!")
        elif ai_move == 3:
            ai_damage = r.randint(ai[4],ai[5])
            if ai_damage<0:
                ai[0] = ai[0]+ai_damage
                print("Your opponent damaged themselves for",ai_damage,"HP!")
            else:
                player_class[0] = player_class[0]- ai_damage
                print("Your opponent did",ai_damage,"damage!")
        elif ai_move == 4:
            ai_heal = r.randint(ai[6],ai[7])
            if ai[0] + ai_heal > ai_heal_max:
                ai[0] = ai_heal_max
            else:
                ai[0] = ai[0] + ai_heal
            print("Your opponent healed for",ai_heal,"HP")
        #####Displaying HP#####   
        print("\nYour health is:",player_class[0],"HP")
        print("Your opponent's health is",ai[0],"HP")
        #####Detecting Loss#####
        if player_class[0]<=0:
            break
        elif ai[0]<=0:
            points += player_class[0]*level
            level +=1
            print("You have bested your opponent! You Have",points,"points. \nNow starting level",level)
            player_class,player_heal_max = level_up(player_class,player_heal_max)
            break
        else:
            continue
    #####Finishing Game, Checking/Updating High Score#####
    if player_class[0]<=0:
        print("\nYou Died! :(")
        if points>score:
            hs = open("highscore.txt","w")
            hs.write(str(points))
            hs.write("\n")
            print("You have the new high score of",points,"!")
            hs.write(player_name)
        else:
            print("\nYou finished with",points,"points.")
            print("The high score is:",score,"by",leader)
        input("")
        hs.close()
        break
EN

回答 1

Code Review用户

回答已采纳

发布于 2019-07-12 17:29:48

在你的游戏中,我已经玩过,你有三个主要的战士:骑士,法师,和治疗。这些战士都有相似的行为、健康、攻击和治疗--他们本质上是一个类Warrior的对象。

Tip 1:让我们创建一个Warrior类:

  • 你将能够创造新的勇士(即弓箭手,畜生,僵尸)稍后轻松。
  • 你可以把你的人工智能玩家作为一个战士。
  • 你可以简单地控制你所有的战士对象。
代码语言:javascript
运行
复制
class Warrior:
    def __init__(self, health, attack_1, attack_2, attack_3, heal):
        self.health = health
        self.attack_1 = attack_1
        self.attack_2 = attack_2 # tuple ie (5,25) representing range for attack value
        self.attack_3 = attack_3 # tuple ie (10,20) representing range for attack value
        self.heal = heal # tuple ie (10,20) representing range for health value

    def attributes(self):
        # string containing the attributes of the character
        string = "Health: "+ str(self.health) + " Attack 1: "+ str(self.attack_1) + " Attack 2: "+ str(self.attack_2[0]) + "-"+ str(self.attack_2[1])+ " Attack 3: "+ str(self.attack_3[0]) + "-"+ str(self.attack_3[1]) + " Heal:"+ str(self.heal[0]) + "-" + str(self.heal[0])
        return string

    def is_dead(self):
        return self.health <= 0

稍后您可能需要添加其他函数。例如,def attack_3(self),它将返回攻击的值。然后我们初始化骑士、法师、治疗师和ai如下:

代码语言:javascript
运行
复制
knight = Warrior(100, 10, (5,15),  (5,25),  (5,10))
mage   = Warrior(50,  15, (10,20), (-5,25), (10,15))
healer = Warrior(150, 5,  (5,10),  (5,15),  (10,20))

while True:
    # Determining AI Class/Stats
    ai_knight = Warrior(100, 10, (5,15),  (5,25),  (5,10))
    ai_mage   = Warrior(50,  15, (10,20), (-5,25), (10,15))
    ai_healer = Warrior(150, 5,  (5,10),  (5,15),  (10,20))
    ai_classes = [ai_knight, ai_mage, ai_healer]

    ai = ai_classes[r.randint(0,2)]
    randomize_ai(ai)
    if ai == ai_knight:
        print("\nYou are fighting a knight with ", ai.health,"HP!")
    elif ai == ai_mage:
        print("\nYou are fighting a mage with ", ai.health,"HP!")
    elif ai == ai_healer:
        print("\nYou are fighting a healer with ", ai.health,"HP!")

Tip 2: elif是你最好的朋友。如果您的if语句是相互排斥的,您可以通过使用elif(您在程序中成功地使用了D15,只是不总是这样)来降低程序的复杂性:

代码语言:javascript
运行
复制
if ai == 1:
    #code
if ai == 2:
    #code
if ai == 3:
   #code

# should instead be...
# because ai can't be three values at once

if ai == 1:
    #code
elif ai == 2:
    #code
elif ai == 3:
   #code

Tip 3:程序的样式对实际程序至关重要。关于编码样式,您应该了解一些基本知识:

  • 注释时不要使用太多的#。而不是######Displaying HP#######,试试# Display HP。后者对于其他人或您阅读/审阅您的代码更具可读性。
  • 如果有节标题注释,可以尝试一种特殊的注释样式,如:
代码语言:javascript
运行
复制
###########
# CLASSES #
###########
  • 不要在您的代码中添加额外的空间--这会使您的代码比它所需要的时间更长,而且应该如此。如果代码不降低可读性,就缩短代码。
  • 若要改善用户体验,请尽可能避免输入
  • 不管你做什么,都要始终如一。阅读、审阅和编辑风格一致的代码是最容易的。
  • 风格是那种只需要经验和实践的东西之一。随着时间的推移,你会变得更好。

记住这三个技巧,最后的代码应该更像:

代码语言:javascript
运行
复制
import random as r

try:
    hs = open("highscore.txt","r+")
except:
    hs = open("highscore.txt","x")
    hs = open("highscore.txt","r+")

try:
    score = int(hs.readlines(1)[0])
    score = int(score[0])
    leader = hs.readlines(2)
    leader =  str(hs.readlines(2)[0])
except:
    hs = open("highscore.txt","w")
    hs.write("0\nnull")
    hs = open("highscore.txt","r")
    score = int(hs.readlines(1)[0])
    leader = str(hs.readlines(2)[0])

# Introduce and name the player
print ("\nWELCOME TO WONDERLANDS RPG!")
print ("The High Score is:", score, "by", leader)
points = 0
player_name = input ("\nEnter your hero's name: ")


###########
# CLASSES #
###########

class Warrior:
    def __init__(self, health, attack_1, attack_2, attack_3, heal):
        self.health = health
        self.attack_1 = attack_1
        self.attack_2 = attack_2 # tuple ie (5,25) representing range for attack value
        self.attack_3 = attack_3 # tuple ie (10,20) representing range for attack value
        self.heal = heal # tuple ie (10,20) representing range for health value

    def attributes(self):
        # string containing the attributes of the character
        string = "Health: "+ str(self.health) + " Attack 1: "+ str(self.attack_1) + " Attack 2: "+ str(self.attack_2[0]) + "-"+ str(self.attack_2[1])+ " Attack 3: "+ str(self.attack_3[0]) + "-"+ str(self.attack_3[1]) + " Heal:"+ str(self.heal[0]) + "-" + str(self.heal[0])
        return string

    def is_dead(self):
        return self.health <= 0

knight = Warrior(100, 10, (5,15),  (5,25),  (5,10))
mage   = Warrior(50,  15, (10,20), (-5,25), (10,15))
healer = Warrior(150, 5,  (5,10),  (5,15),  (10,20))

while True:
    print("\n1. Knight: ", knight.attributes())
    print("\n2. Mage:   ", mage.attributes())
    print("\n3. Healer: ", healer.attributes())
    player_class = input("\nSelect your class: 1, 2, or 3: ")
    if player_class == "1":
        player_class = knight
        print("You have selected the Knight class.")
        break
    elif player_class == "2":
        player_class = mage
        print("You have selected the Mage")
        break
    elif player_class == "3":
        player_class = healer
        print("You have selected the Healer")
        break
    else:
        print("Please select a valid class.")
        continue

player_heal_max = player_class.health


################################
# Difficulty/Upgrade Functions #
################################

def level_up(player,health_max):
    while True:
        lv_choice = input("\nWould you like to:\n 1. Increase max health by 20 \n 2. Increase Healing Factor by 5 \n 3. increase your damage by 5\n")
        if lv_choice == "1":
            health_max += 20
            player.health = health_max
            return player, health_max
        elif lv_choice == "2":
            player.heal += (5,5)
            player.health = health_max
            return player, health_max
        elif lv_choice == "3":
            player.attack_1 += 5
            player.attack_2 += (5,5)
            player.attack_3 += (5,5)
            player.health = health_max
            return player, health_max
        else:
            print("Please enter in a valid number")
            continue

def difficulty(ai,health_max,level):
    if level == 1:
        return ai
    else:
        ai.health = health_max + 15 * round(0.5 * level + 0.5)
        ai.attack_1 += 5 * round(0.5 * level + 0.5)
        ai.attack_2 += (5 * round(0.5 * level + 0.5),5 * round(0.5 * level + 0.5))
        ai.attack_3 += (5 * round(0.5 * level + 0.5),5 * round(0.5 * level + 0.5))
        ai.heal += (5 * round(0.5 * level + 0.5),5 * round(0.5 * level + 0.5))
        return ai

def randomize_ai(ai):
    ai.health += r.randint(-20,20)
    ai.attack_1 += r.randint(-3,3)
    ai.attack_2 += (r.randint(-3,3),r.randint(-3,3))
    ai.attack_3 += (r.randint(-3,3),r.randint(-3,3))
    ai.heal += (r.randint(-3,3),r.randint(-3,3))
    return ai

#############
# Game Loop #
#############

level = 1
print("\n----------------------- GAME START -----------------------")

while True:
    # Determining AI Class/Stats
    ai_knight = Warrior(100, 10, (5,15),  (5,25),  (5,10))
    ai_mage   = Warrior(50,  15, (10,20), (-5,25), (10,15))
    ai_healer = Warrior(150, 5,  (5,10),  (5,15),  (10,20))
    ai_classes = [ai_knight, ai_mage, ai_healer]

    ai = ai_classes[r.randint(0,2)]
    randomize_ai(ai)
    if ai == ai_knight:
        print("\nYou are fighting a knight with ", ai.health,"HP!")
    elif ai == ai_mage:
        print("\nYou are fighting a mage with ", ai.health,"HP!")
    elif ai == ai_healer:
        print("\nYou are fighting a healer with ", ai.health,"HP!")

    ai_heal_max = ai.health

    ai = difficulty(ai, ai_heal_max, level)

    # Gameplay Loop
    while True:
        # Player Attack
        player_move = input("\nWould you like to use attack (1), attack (2), attack (3), or heal (4)?  ")
        print("")
        if player_move == "1":
            player_damage = player_class.attack_1
            ai.health = ai.health - player_damage
            print(player_name," did",player_damage,"damage!")
        elif player_move == "2":
            player_damage = r.randint(player_class.attack_2[0],player_class.attack_2[1])
            ai.health = ai.health - player_damage
            print(player_name," did",player_damage,"damage!")
        elif player_move == "3":
            player_damage = r.randint(player_class.attack_3[0],player_class.attack_3[1])
            ai.health = ai.health - player_damage
            print(player_name," did", player_damage, " damage!")
        elif player_move == "4":
            player_heal = r.randint(player_class.heal[0],player_class.heal[1])
            if player_class.health + player_heal > player_heal_max:
                player_class.health = player_heal_max
            else:
                player_class.health = player_class.health + player_heal
            print(player_name," healed for",player_heal,"HP")
        else:
            print("Please enter in a valid move.")
            continue

        # Detecting Death
        if player_class.is_dead():
            break
        elif ai.is_dead():
            points += player_class.health * level
            level += 1
            print("You have bested your opponent! You Have",points,"points. \nNow starting level",level)
            player_class, player_heal_max = level_up(player_class,player_heal_max)
            break

        # AI Turn
        if ai.health <= (ai_heal_max/5):
            ai_move = r.sample(set([1,2,3,4,4,4]), 1)[0]
        elif ai.health >= (ai_heal_max*.8):
            ai_move = r.sample(set([1,2,3,1,2,3,4]), 1)[0]
        elif ai.health == ai_heal_max:
            ai_move = r.randint(1,3)
        else:
            ai_move = r.randint(1,4)

        if ai_move == 1:
            ai_damage = ai.attack_1
            player_class.health = player_class.health - ai_damage
            print("Your opponent did",ai_damage,"damage!")
        elif ai_move == 2:
            ai_damage = r.randint(ai.attack_2[0],ai.attack_2[1])
            player_class.health = player_class.health- ai_damage
            print("Your opponent did ",ai_damage," damage!")
        elif ai_move == 3:
            ai_damage = r.randint(ai.attack_3[0],ai.attack_3[1])
            player_class.health = player_class.health - ai_damage
            print("Your opponent did ", ai_damage," damage!")
        elif ai_move == 4:
            ai_heal = r.randint(ai.heal[0],ai.heal[1])
            if ai.health + ai_heal > ai_heal_max:
                ai.health = ai_heal_max
            else:
                ai.health = ai.health + ai_heal
            print("Your opponent healed for ", ai_heal," HP")

        # Displaying HP  
        print("\nYour health is:", player_class.health,"HP")
        print("Your opponent's health is ", ai.health," HP ")

        # Detecting Death
        if player_class.is_dead():
            break
        elif ai.health <= 0:
            points += player_class.health * level
            level += 1
            print("You have bested your opponent! You Have",points,"points. \nNow starting level",level)
            player_class, player_heal_max = level_up(player_class,player_heal_max)
            break

    # Finishing Game, Checking/Updating High Score
    if player_class.health<=0:
        print(" \ nYou Died !: (")
        if points > score:
            hs = open(" highscore.txt "," w ")
            hs.write(str(points))
            hs.write(" \ n ")
            print(" You have the new high score of ",points," !")
            hs.write(player_name)
        else:
            print(" \ nYou finished with ",points," points.")
            print(" The high score is:",score," by ",leader)
        input(" ")
        hs.close()
        break
```#qcStackCode#
代码语言:javascript
运行
复制
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/223917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档