Python 文件中的二维数组

Python 文件中的二维数组,python,arrays,file,multidimensional-array,Python,Arrays,File,Multidimensional Array,大家好,请大家描述一下这段代码的算法。类似于pythonTutor.com,但该网站无法处理外部文件 j = 0 self.pole = [] while True: newLine = t.readline().strip() if newLine == '': break for i in range(len(newLine)): if newLine

大家好,请大家描述一下这段代码的算法。类似于pythonTutor.com,但该网站无法处理外部文件

j = 0
self.pole = []
while True:
            newLine = t.readline().strip()   
            if newLine == '':
                break
            for i in range(len(newLine)):
                if newLine[i] == 'v':
                    self.vajce = Vajce(i*40+20, j*40+20)    #this is class witch i am calling
                    newLine = riadok.replace('v','.')
                if newLine[i] == 'z':
                    self.dvere[j,i] = Dvere(i*40, j*40)  # another class 
            self.pole.append(list(newLine))
            j += 1
源代码

import tkinter, math

class Vajce:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def kresli(self, g):
        self.g = g
        self.id = g.create_oval(self.x-10,self.y-13,self.x+10,self.y+13,fill='white')

    def pohni(self, dx, dy):
        self.x += dx
        self.y += dy
        self.g.move(self.id, dx, dy)

class Klucik:
    def __init__(self, x, y, polygon):
        self.x = x
        self.y = y
        self.polygon = polygon

    def kresli(self, g):
        self.g = g
        self.id = self.g.create_polygon(self.polygon,fill='red')
        self.g.move(self.id, self.x, self.y)

    def zmaz(self):
        self.g.delete(self.id)
        self.x = -100

    def blizko(self, x, y):     # bod (x, y) nie je od kľúčika ďalej ako 20
        return math.sqrt((self.x-x)**2+(self.y-y)**2) <= 20

class Dvere:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def kresli(self, g):
        self.g = g
        self.id = g.create_rectangle(self.x,self.y,self.x+40,self.y+40,fill='blue')

    def zmen_stav(self, stav):
        self.g.itemconfig(self.id, fill=['blue','green','seagreen'][stav])

class Program:
    def __init__(self, meno_suboru):
        t = open(meno_suboru)
        self.pole = []
        self.dvere = {}          # ako slovník 
        j = 0
        while True:
            riadok = t.readline().strip()   # odstráň zbytočné medzery a koniec riadka
            if riadok == '':
                break
            for i in range(len(riadok)):
                if riadok[i] == 'v':
                    self.vajce = Vajce(i*40+20, j*40+20)
                    riadok = riadok.replace('v','.')
                if riadok[i] == 'z':
                    self.dvere[j,i] = Dvere(i*40, j*40)  # pridá do slovníka
            self.pole.append(list(riadok))
            j += 1
        print(self.pole)
        vyska = j*40                  # pre výšku grafickej plochy
        poly = t.readline().split()
        for i in range(len(poly)):
            poly[i] = int(poly[i])
        self.klucik = []
        while True:
            riadok = t.readline()
            if riadok == '':
                break
            x,y = riadok.split()
            self.klucik.append(Klucik(int(x),int(y),poly))
        t.close()
        sirka = len(self.pole[0])*40   # šírka grafickej plochy
        self.g = tkinter.Canvas(bg='seagreen',width=sirka,height=vyska)
        self.g.pack()
        self.kresli()
        self.pocitadlo = 0     # počítadlo zdvihnutých klúčikov
        self.g.bind_all('<Key>', self.klaves)
        tkinter.mainloop()

    def kresli(self):
        for i in range(len(self.pole)):
            for j in range(len(self.pole[0])):
                x,y = j*40,i*40
                p = self.pole[i][j]
                if p == 'x':
                    self.g.create_rectangle(x,y,x+40,y+40,fill='brown')
                if p == 'k':
                    self.g.create_rectangle(x,y,x+40,y+40,fill='yellow')
                if p == 'z':
                    self.dvere[i,j].kresli(self.g)
        for k in self.klucik:
            k.kresli(self.g)
        self.vajce.kresli(self.g)

    def klaves(self, e):
        if e.keysym == 'Up':
            self.pohni(0, -4)
        if e.keysym == 'Down':
            self.pohni(0, 4)
        if e.keysym == 'Left':
            self.pohni(-4, 0)
        if e.keysym == 'Right':
            self.pohni(4, 0)

    def pohni(self, dx, dy):      # zavolá sa z udalosti stlačenie šípky
        x, y = self.vajce.x+dx, self.vajce.y+dy
        for i in range(len(self.klucik)):
            if self.klucik[i].blizko(x,y):
                self.pocitadlo += 1
                self.klucik[i].zmaz()
        if self.pocitadlo > 0:
            for i in range(len(self.pole)):
                for j in range(len(self.pole[0])):
                    if self.pole[i][j] == 'z':
                        self.pole[i][j] = 'y'
                        self.dvere[i,j].zmen_stav(1)
        r, s = y//40, x//40
        if self.pole[r][s] in 'xz':
            return
        self.vajce.pohni(dx,dy)
        if self.pole[r][s] == 'y':      # odomknuté dvere
            #print('dvere',r,s)
            self.pocitadlo -= 1
            self.dvere[r,s].zmen_stav(2)
            self.pole[r][s] = '.'
            if self.pocitadlo == 0:
                for i in range(len(self.pole)):
                    for j in range(len(self.pole[0])):
                        if self.pole[i][j] == 'y':
                            self.pole[i][j] = 'z'
                            self.dvere[i,j].zmen_stav(0)
        if self.pole[r][s] == 'k':
            self.g.create_text(300,70,text='HURA!!!',fill='red',font='arial 100 bold')
            self.g.unbind_all('<Key>')

Program('plocha.txt')
导入tkinter,数学
Vajce类:
定义初始化(self,x,y):
self.x=x
self.y=y
def kresli(自我,g):
self.g=g
self.id=g.create_oval(self.x-10,self.y-13,self.x+10,self.y+13,fill='white')
def pohni(自身、dx、dy):
self.x+=dx
self.y+=dy
self.g.move(self.id,dx,dy)
克鲁西克级:
定义初始(自、x、y、多边形):
self.x=x
self.y=y
self.polygon=多边形
def kresli(自我,g):
self.g=g
self.id=self.g.create_polygon(self.polygon,fill='red')
self.g.move(self.id、self.x、self.y)
def zmaz(自我):
self.g.delete(self.id)
self.x=-100
德夫·布利兹科(赛尔夫,x,y):#博德(x,y)尼耶·奥德·克什卡·阿科20岁
返回数学sqrt((self.x-x)**2+(self.y-y)**2)0:
对于范围内的i(len(self.pole)):
对于范围内的j(len(自极点[0]):
如果self.pole[i][j]=“z”:
self.pole[i][j]=“y”
self.dvere[i,j].zmen_stav(1)
r、 s=y//40,x//40
如果'xz'中的self.pole[r][s]:
返回
self.vajce.pohni(dx,dy)
如果self.pole[r][s]='y':#odomknutédvere
#打印('dvere',r,s)
self.pocitadlo-=1
self.dvere[r,s].zmen_stav(2)
自极点[r][s]='.'
如果self.pocitadlo==0:
对于范围内的i(len(self.pole)):
对于范围内的j(len(自极点[0]):
如果self.pole[i][j]=“y”:
self.pole[i][j]=“z”
self.dvere[i,j].zmen_stav(0)
如果self.pole[r][s]=“k”:
self.g.create_text(300,70,text='HURA!!!',fill='red',font='arial 100 bold')
self.g.unbind_all(“”)
程序('plocha.txt')
当你们拿着鸡蛋走路,收集开门的钥匙时,这应该是一个游戏

t = open(meno_suboru)
# open a file to read in 
j = 0
# initialise line counter
self.pole = []
# create list to hold things
self.dvere = {}  
# create dictionary to hold other things
while True:
# loop until condition met
    riadok = t.readline().strip()   
    # get the next line from t and strip the newline character, whitespace etc
    if newLine == '':
    # if we've reached a blank line
        break
        # we're done
    for i in range(len(riadok)):
    # count through the characters in the new line
        if riadok[i] == 'v': 
        # if current character is a 'v'
            self.vajce = Vajce(i*40+20, j*40+20)    
            # create a new Vjace object and set to self.vajce
            raidok = riadok.replace('v','.')
            # replace all 'v's in the line with '.'s to prevent further matches
            # so we only ever have one self.vjace
        if newLine[i] == 'z':
        # if current character is a 'z'
            self.dvere[j,i] = Dvere(i*40, j*40) 
            # same as before, but self.dvere is a dictionary so we 
            # keep any previous Dvere objects too - dictionary key
            # is a tuple (j, i)
    self.pole.append(list(riadok))
    # turn the line into a list of characters and add to the self.pole list
    j += 1
    # increment the line counter
请注意,这可以更好地结构化:

with open(meno_suboru) as t: not 'open()' and 'close()'
    for j, riadok in enumerate(t): # not 'while True:'
        # use index j and line riadok
        ...
        for i, char in riadok: # not 'for i in range(len(riadok)):'
            # use index i and character char
            ...

天哪,我不知道代码从哪里来,它做什么,上下文是什么,你想让我描述一下算法吗?