我非常喜欢菜鸟,所以请原谅我令人费解的混乱代码。我正在努力制作一个自己的游戏引擎,用于基于文本的战斗冒险等。这是代码:
import random
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 29 strings representing the board (ignore index 0)
print('|---+---+---+---+---+---+---|')
print('| | | | | | | |')
print('| ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' | ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' | ' + board[7] + ' | ')
print('| | | | | | | |')
print('|---+---+---+---+---+---+---|')
print('| | | | | | | |')
print('| ' + board[8] + ' | ' + board[9] + ' | ' + board[10] + ' | ' + board[11] + ' | ' + board[12] + ' | ' + board[13] + ' | ' + board[14] + ' | ')
print('| | | | | | | |')
print('|---+---+---+---+---+---+---|')
print('| | | | | | | |')
print('| ' + board[15] + ' | ' + board[16] + ' | ' + board[17] + ' | ' + board[18] + ' | ' + board[19] + ' | ' + board[20] + ' | ' + board[21] + ' | ')
print('| | | | | | | |')
print('|---+---+---+---+---+---+---|')
print('| | | | | | | |')
print('| ' + board[22] + ' | ' + board[23] + ' | ' + board[24] + ' | ' + board[25] + ' | ' + board[26] + ' | ' + board[27] + ' | ' + board[28] + ' | ')
print('| | | | | | | |')
print('|---+---+---+---+---+---+---|')
def whoGoesFirst():
if random.randint(0, 1) == 0: # coin flip 50/50 who starts combat
firstToSpawn = enemy
lastToSpawn = player
else:
firstToSpawn = player
lastToSpawn = enemy
# following chunk works out the spawn points of the first to enter
#and the last to enter, respectively. It then places them on the
#board in their respective spawn points, represented by their symbol
while firstToSpawn not in theBoard and lastToSpawn not in theBoard:
boardSpawn = '1 2 3 4 5 6 7 14 21 28 27 26 25 24 23 22 15 8'.split()
boardSpawnResult = random.choice(boardSpawn)
theBoard[int(boardSpawnResult)] = firstToSpawn
print(firstToSpawn + ' has entered the room.')
else:
if firstToSpawn in theBoard and lastToSpawn not in theBoard:
boardHalf = len(boardSpawn) / 2
boardHalf = int(boardHalf)
lastToSpawnPosition = int(boardSpawnResult) + boardHalf
if lastToSpawnPosition > len(boardSpawn):
lastToSpawnPosition = lastToSpawnPosition - len(boardSpawn)
lastToSpawnPosition = boardSpawn[lastToSpawnPosition]
theBoard[int(lastToSpawnPosition)] = lastToSpawn
print(lastToSpawn + ' has entered the room.')
if firstToSpawn == player:
playerLocale = int(boardSpawnResult)
global playerLocale
else:
playerLocale = int(lastToSpawnPosition)
global playerLocale
print(drawBoard(theBoard))
def shoot():
shotChance = int(playerStats[1]) - int(gangerStats[3])
didItHit = random.randint(int(shotChance), 100)
if didItHit < 65:
print(playerName + ' Missed the shot!')
elif didItHit >= 65:
didItCrit = random.randint(int(playerStats[5], 85))
if didItCrit >= 85:
shotDamage = 25 + weaponsGlock
print('Critical hit! ' + shotDamage)
else:
shotDamage = weaponsGlock
print(playerName + ' Hit the enemy for ' + shotDamage)
gangerStats[0] - shotDamage
if gangerStats[0] < 1:
print(playerName + ' killed the Ganger!')
#declaring globals and starting the program
while True:
theBoard = [' ']* 29
player = ' '
enemy = '!'
print('What\'s your name, Droog?')
playerName = input()
player = playerName[0].upper()
playerLocale = ' '
playerStats = '100 50 2 10 5 2'.split()
gangerStats = '65 20 1 5 3 5'.split()
weaponsGlock = 30
whoGoesFirst()
shoot()
while int(gangerStats[0]) > 0:
command = input('Type shoot to attack again:').lower
if command == 'shoot':
shoot()
else:
if int(gangerStats[0]) <= 0:
command = input('Type "searchbody" to loot dead enemies:').lower
我的shoot()函数中的错误一直出现在randint为didItCrit变量滚动的行上。这是错误的副本:
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "C:\Python33\practice programs\textAdventure\combatmodule.py", line 163, in <module>
shoot()
File "C:\Python33\practice programs\textAdventure\combatmodule.py", line 133, in shoot
didItCrit = random.randint(int(playerStats[5], 85))
ValueError: int() base must be >= 2 and <= 36
任何帮助都会受到大力赞赏。
答案 0 :(得分:11)
你错放了)
:
random.randint(int(playerStats[5], 85))
使用2个参数调用int()
。相反,你想要:
random.randint(int(playerStats[5]), 85)
使用2个参数调用randint()
,其中1个是使用1个参数调用int()
的结果。