当我运行我的代码并进入游戏的战斗部分时,我会获得分配给我的角色攻击,防御,伤害和健康的随机值。然而,在他们第一次转弯之后,他们不断获得相同的值,他们无法重置。
例如,用户的攻击是一个从4到11的随机数。程序“滚动”5并将其分配给变量userAtk
userAtk = random.randint(4,11)
我假设每次循环运行时,它都会生成一个新值。 但它不是,每次我打印变量时,它首先被赋予相同的值。 我错过了什么吗?
以下是我的代码
import random
# VARIABLES
#
#
# This variable is the user's character name
userName = input("Brave warrior, what is your name? ")
# This variable is used in the input function to pause the game
enterNext = ("Press enter to continue...")
# This variable is used to format the user input prompt
# as well as the battle text
prompt = ">>>>> "
# This variable is used to add a new line to a string
newLine = "\n"
# This variable is used to display a message when the hero dies
heroDeadmsg = userName + " has fallen!"
# These variables represent the dragon's stats (HP, ATK & DEF)
dragonHp = 100
dragonAtk = random.randint(5,10)
dragonDef = random.randrange(8)
# These variables represent the user's stats (HP, ATK & DEF)
userHp = 90
userAtk = random.randint(4,11)
userDef = random.randrange(8)
# These variables calculate battle damage and HP
dragonDmg = (userAtk - dragonDef)
dragonHp -= dragonDmg
userDmg = (dragonAtk - userDef)
userHp -= userDmg
# This variable prints the options in the battle menu
battleMenu = """Attack (a) - Magic (m) - Item (i) - Run (r)"""
# This variable determines who goes first
cointoss = random.randint(0, 1)
# These variables print the actions in each turn
dragonAttack = \
prompt + "Crimson Dragon attacks you with " + str(dragonAtk) + " ATK!"\
+ newLine + prompt + "You defend with " + str(userDef) + " DEF!"\
+ newLine + prompt
userAttack = \
prompt + "You attacked with " + str(userAtk) + " ATK!"\
+ newLine + prompt + "Crimson Dragon defends with " + str(dragonDef) + " DEF!"\
+ newLine + prompt
userMagic = \
prompt + userName + " tried to use Magic!"\
+ newLine + prompt + userName + " has no magic!"\
+ newLine + prompt
userItem = \
prompt + userName + " tried use an Item!"\
+ newLine + prompt + userName + " has no Items!"\
+ newLine + prompt
userRetreat = \
prompt + userName + " tries to retreat!"\
+ newLine + prompt + "The enemy won't let you escape!"\
+ newLine + prompt
# These variables show health during battle
printDragonhp = "Crismon Dragon has " + str(dragonHp) + " HP remaining!"
printUserhp = userName + " has " + str(userHp) + " HP remaining!"
# This variable simulates the results of a coin toss
coinToss = random.randint(0, 1)
#
#
# CONDTITIONS
#
#
# These conditions determines who attacks first
if coinToss == 0:
currentTurn = "dragon"
elif coinToss == 1:
currentTurn = "user"
else:
print("The Coin Toss Failed!")
#
#
# BATTLE MECHANICS
#
#
while currentTurn:
# Mechanics for the Crimson Dragon's Turn
if currentTurn == "dragon":
# Prints the Crimson Dragon's Attack and ends the turn
print(newLine + prompt + "Crimson Dragon moves!"\
+ newLine + prompt + newLine + dragonAttack\
+ newLine + prompt + userName + " takes " + str(userDmg) + " DMG!"\
+ newLine + prompt + printUserhp)
currentTurn = "user"
input(prompt)
# Need to implent a way to reset ATK and DEF
# Mechanics for the User's Turn
if currentTurn == "user":
# Prints the Battle Menu and asks for the User's choice
print(newLine + prompt + battleMenu\
+ newLine + prompt)
userChoice = input(prompt)
# Prints the User's Attack and ends the turn
if userChoice == "a":
print(userAttack)
if userHp < 1:
print(heroDeadmsg)
break
input (prompt)
currentTurn = "dragon"
# Prints the User's Magic and ends the turn
elif userChoice == "m":
print(userMagic)
input (prompt)
currentTurn = "dragon"
# Prints the User's Item and ends the turn
elif userChoice == "i":
print(userItem)
input (prompt)
currentTurn = "dragon"
# Prints the User's Retreat and ends the turn
elif userChoice == "r":
print(userRetreat)
input (prompt)
currentTurn = "dragon"
# Prints an error message for invalid entries
else:
print(newLine + prompt + "That is not a valid menu item."\
+ newLine + prompt + "Please try again.")
答案 0 :(得分:2)
因为userAtk
不在循环中,所以我可以看到。如果您希望在循环内重置它,请在循环内调用random.randint(4,11)
。
答案 1 :(得分:2)
random.randint(4,11)
只选择范围[4, 11]
中的整数并返回该数字。因此,当您执行userAtk = random.randint(4,11)
时,您只需获取一个号码并将其存储为userAtk
,并且每次访问userAtk
时,您都会获得相同的号码。
如果你希望userAtk
成为一种神奇的东西,每次你访问它时,[4, 11]
范围内的数字就像一个不同的数字......好吧,这并非不可能(见{{3对它进行快速而肮脏的刺伤)......但它几乎肯定会导致更多的混乱而不是利益。
例如,您有代码试图打印str(userAtk)
...但如果每次访问时值不同,打印出的内容将与用于计算损坏的内容不同!想象一下,如果你正在玩桌面D&amp; D,地牢大师掷出一个骰子告诉你你的滚动,然后立即忘记结果并再次滚动以确定你是否击中。所以他可能会说,“你滚了20分。你错过了。”这不好。
可能有用的是让userAtk
实际上成为一个函数:
def userAtk():
return random.randint(4, 11)
同样适用于所有类似的变量。然后,在您访问某个数字的任何地方,您都会调用该函数:
def dragonDmg():
return userAtk() - dragonDef()
然后在某个地方,您将要在每个循环中存储调用这些函数的结果。
但关键在于,无论如何,你必须拥有每次循环时重新计算的变量。
答案 2 :(得分:0)
我相信即使你打了几百万次randint(),你仍会得到重复(我相信你知道这一点)。我曾经使用字典来跟踪已使用/未使用的随机数,只需检查字典是否已被使用。可能误解了这个问题。