我最近开始创建一个基于角色扮演,基于控制台的游戏。
我编写了以下代码:
# MAIN GAME LOOP
while True:
if MMS == 'A': # already definied
print('Loading Story mode...')
print('[LOADING] Importing numpy')
try:
import numpy as np
except ImportError:
print('Error 01 // Cilent side')
print('ImportError')
while True:
A = 1
print('[LOADING] Trying load save...')
try:
SAVE = np.loadtxt('Savedata.dat')
MAXLIFE = SAVE[:, 0] # all of thesse already definied
LIFE = SAVE[:, 1]
MAXEN = SAVE[:, 2]
EN = SAVE[:, 3]
MAXOX = SAVE[:, 4]
OX = SAVE[:, 5]
OPOS = SAVE[:, 6]
POS = SAVE[:, 7]
CHAPTER = SAVE[:, 8]
SAVE.close()
except:
sleep(4)
print('[LOADING] No save file found or incorrect save.')
print('[LOADING] Creating a new save.')
MAXLIFE = 150
LIFE = 110
MAXEN = 150
EN = 150
MAXOX = 150
OX = 85
OPOS = 10
POS = 10
CHAPTER = 1
np.savetxt('Savedata.dat', [MAXLIFE, LIFE, MAXEN, EN, MAXOX, OX, OPOS, POS, CHAPTER])
我除外,如果没有保存,它将创建一个具有默认值的新保存。如果有保存,游戏将加载它。 但是,它会产生无限的输出:
[LOADING] No save file found or incorrect save.
[LOADING] Creating a new save.
Loading Story mode...
[LOADING] Importing numpy
[LOADING] Trying load save...
[LOADING] No save file found or incorrect save.
[LOADING] Creating a new save.
Loading Story mode...
...
无限循环是可以的,但是它没有检测到其较早的保存。
在Savedata.dat中
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
1.000000000000000000e+00
我做错了什么? 谢谢!
答案 0 :(得分:1)
就像hpaulj所说的,不要使用裸except
。我将其重写为:
except Exception as e:
print(e)
(我知道,仅捕获异常也是不好的),它向我显示了此信息:
Traceback (most recent call last):
File "wakagame.py", line 21, in <module>
MAXLIFE = SAVE[:, 0] # all of thesse already definied
IndexError: too many indices for array
问题是您正在尝试选择列:
MAXLIFE = SAVE[:, 0]
不是来自矩阵,而是来自平均一维数组:
np.savetxt('Savedata.dat', [MAXLIFE, LIFE, MAXEN, EN, MAXOX, OX, OPOS, POS, CHAPTER])
您应将所有这些行替换为以下行:
MAXLIFE = SAVE[0]
(还要注意,SAVE是一个numpy数组,而不是文件,因此SAVE.close()
将返回错误。您应该删除此行)