制作游戏时2d数组索引python的问题

问题描述 投票:0回答:1

我正在尝试使用Python编写自己的连接四游戏。我正在尝试将我绘制的圆圈排序为二维数组。但是,当我尝试将我的形状对象分配给数组时,它会给我一个索引错误。我不能真正看到counterrow和countrercolumn的问题,还有其他人吗?顺便说一句,我的太空类只有一个初始化设置x1,x2,y1,y2,take和id

from tkinter import *
from space import *

master = Tk();
w = Canvas(master, width = 600, height = 500)
w.pack()

spaceList = []
for i in range(7):
    spaceList.append([0] * 6)
currentmove = 'PLAYER1'
won = False
counterrow = 0
countercolumn = 0

for i in range(0,560,80):
    for j in range(0,480,80):
        w.create_oval(10+i, 10+j, 90+i, 90+j)
        newspace = Space(10+i, 10+j, 90+i, 90+j, False, 'EMPTY')
        spaceList[counterrow][countercolumn] = newspace
        countercolumn = countercolumn + 1
    counterrow = counterrow + 1

while(not won):
    movecol = int(input("Please select a column!"))

def move(column):
    for i in spaceList:
        return 0

mainloop()
python arrays 2d
1个回答
0
投票

您必须重置计数器列:

for i in range(0,560,80):
    # add this:
    countercolumn = 0
    for j in range(0,480,80):
        # omitted

否则它变得七而大,你会溢出。

© www.soinside.com 2019 - 2024. All rights reserved.