简体   繁体   English

制作游戏时二维数组索引Python的问题

[英]Issues with 2d array indexing python when making game

I'm attempting to program my own connect four game using Python. 我正在尝试使用Python编写自己的四连体游戏。 I'm trying to sort the circles that I have drawn into a 2d array. 我正在尝试将绘制的圆排序为2d数组。 However when I try to assign my shape object to the array it gives me an index error. 但是,当我尝试将形状对象分配给数组时,它给了我一个索引错误。 I can't really see an issue with counterrow and countrercolumn, can anyone else? 我真的看不到计数器行和计数列的问题,还有其他人可以吗? Btw my space class just has an initialiser setting x1, x2, y1, y2, taken, and id 顺便说一句,我的空间班级只有一个初始化程序设置x1,x2,y1,y2,taked和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()

You have to reset the countercolumn: 您必须重置对应的列:

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

Otherwise it becomes seven and larger and you get an overflow. 否则,它将变为7或更大,并且会溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM