簡単なゲームに使用するコードを少しテストしていますが、このエラーが発生します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)