我正在测试一些我将用于一个简单游戏的代码,但我收到了这个错误in init gamefield[x][y] = tecken TypeError: 'field' object does not support indexing
游戏有点类似于蛇,我希望我的程序做的是首先创建一个游戏场,它是一个矩阵,我想在其中插入我的蛇(我在这里称之为蠕虫),用“+”表示,位置应该被随机选择。
然后我希望能够决定蠕虫向哪个方向生长,从而决定生长功能。
谁能看到这里有什么问题?任何帮助将不胜感激!
import random
class field:
def __init__(self):
self.table= [ [ "0" for i in range(10) ] for j in range(10) ]
def printfield(self):
for row in self.table:
print (row)
class worm:
def __init__(self,tecken):
x = random.randint(1,9)
y = random.randint(1,9)
gamefield[x][y] = tecken
def grow(self,p,b,c):
try :
for antal in range(p):
if p != 0:
gamefield[x-antal*b][y-antal*c] = "+"
except IndexError :
print ("Game Over")
p = 2
b = 3
c = 0
gamefield = field()
hilda = worm("+")
hilda.grow(p,b,c)
print(gamefield.printfield)