在python中打印和编辑多个列表

在python中打印和编辑多个列表,python,list,printing,Python,List,Printing,我正在尝试创建一个基于文本的小游戏,为了实现这一点,我创建了20个列表,添加了75个空格来填充列表,然后一次一个地打印每个列表,所有的列表都是同时打印的。我希望能够在某些位置编辑列表,这样当列表再次打印时,控制台就会在我放置列表的位置显示文本。这就是我到目前为止想到的 理想的效果是让控制台打印以下内容: ============================ = = = TEXT ADVENTURE: =

我正在尝试创建一个基于文本的小游戏,为了实现这一点,我创建了20个列表,添加了75个空格来填充列表,然后一次一个地打印每个列表,所有的列表都是同时打印的。我希望能够在某些位置编辑列表,这样当列表再次打印时,控制台就会在我放置列表的位置显示文本。这就是我到目前为止想到的

理想的效果是让控制台打印以下内容:

  ============================ 
  =                          = 
  =      TEXT ADVENTURE:     = 
  =    WAR OF ZE MONSTERS    = 
  =                          =
  ============================ 
但我得到的却是:

===========================
我不知道我的draw函数或write函数到底发生了什么,但(对我来说)它似乎应该工作

任何帮助都将是令人惊讶的,因为我对python还比较陌生。提前谢谢

import time

#
#  Joel Williams
#
#  Purpose: Create a Working Text Engine
#

# start of classes



class line():
    def __init__(self):
        counter = 0
        self.list = []
        while (counter != lineSize):
            self.list.append(' ')
            counter = counter + 1

class cursor():
    def __init__(self):
        self.cursorPosY = 0
        self.cursorPosX = 0
        self.cursorPos = [self.cursorPosY, self.cursorPosX]

    def setCursorPos(self,y,x):
        self.cursorPosY = y
        self.cursorPosX = x
        self.cursorPos = [self.cursorPosY, self.cursorPosX]



# end of cursor class
# start of peliminary declarations



lineSize = 74
term = cursor()

_1  = line()
_2  = line()
_3  = line()
_4  = line()
_5  = line()
_6  = line()
_7  = line()
_8  = line()
_9  = line()
_10 = line()
_11 = line()
_12 = line()
_13 = line()
_14 = line()
_15 = line()
_16 = line()
_17 = line()
_18 = line()
_19 = line()
_20 = line()



# end of preliminary declarations
# start of preliminary functions

def delLine(x):
    del x[:]
    counter = 0
    x = []
    while (counter != lineSize):
        x.append(' ')
        counter = counter + 1

def clear():
    # clears all lists
    delLine(_1.list)
    delLine(_2.list)
    delLine(_3.list)
    delLine(_4.list)
    delLine(_5.list)
    delLine(_6.list)
    delLine(_7.list)
    delLine(_8.list)
    delLine(_9.list)
    delLine(_10.list)
    delLine(_11.list)
    delLine(_12.list)
    delLine(_13.list)
    delLine(_14.list)
    delLine(_15.list)
    delLine(_16.list)
    delLine(_17.list)
    delLine(_18.list)
    delLine(_19.list)
    delLine(_20.list)

def clearLine():
    if(term.cursorPosY == 0):    
        delLine(_1.list)

    elif(term.cursorPosY == 1):    
        delLine(_2.list)

    elif(term.cursorPosY == 2):    
        delLine(_3.list)

    elif(term.cursorPosY == 3):    
        delLine(_4.list)

    elif(term.cursorPosY == 4):    
        delLine(_5.list)

    elif(term.cursorPosY == 5):    
        delLine(_6.list)

    elif(term.cursorPosY == 6):    
        delLine(_7.list)

    elif(term.cursorPosY == 7):    
        delLine(_8.list)

    elif(term.cursorPosY == 8):    
        delLine(_9.list)

    elif(term.cursorPosY == 9):    
        delLine(_10.list)

    elif(term.cursorPosY == 10):    
        delLine(_11.list)

    elif(term.cursorPosY == 11):    
        delLine(_12.list)

    elif(term.cursorPosY == 12):    
        delLine(_13.list)

    elif(term.cursorPosY == 13):    
        delLine(_14.list)

    elif(term.cursorPosY == 14):    
        delLine(_15.list)

    elif(term.cursorPosY == 15):    
        delLine(_16.list)

    elif(term.cursorPosY == 16):    
        delLine(_17.list)

    elif(term.cursorPosY == 17):    
        delLine(_18.list)

    elif(term.cursorPosY == 18):    
        delLine(_19.list)

    elif(term.cursorPosY == 19):    
        delLine(_20.list)

def draw():
    # draws the lists
    # each lists is a line (Y)
    # each of the list's properties are the text (X)
    i1 = ''.join(_1.list)
    i2 = ''.join(_2.list)
    i3 = ''.join(_3.list)
    i4 = ''.join(_4.list)
    i5 = ''.join(_5.list)
    i6 = ''.join(_6.list)
    i7 = ''.join(_7.list)
    i8 = ''.join(_8.list)
    i9 = ''.join(_9.list)
    i10 = ''.join(_10.list)
    i11 = ''.join(_11.list)
    i12 = ''.join(_12.list)
    i13 = ''.join(_13.list)
    i14 = ''.join(_14.list)
    i15 = ''.join(_15.list)
    i16 = ''.join(_16.list)
    i17 = ''.join(_17.list)
    i18 = ''.join(_18.list)
    i19 = ''.join(_19.list)
    i20 = ''.join(_20.list)
    print i1
    print i2
    print i3
    print i4
    print i5
    print i6
    print i7
    print i8
    print i9
    print i10
    print i11
    print i12
    print i13
    print i14
    print i15
    print i16
    print i17
    print i18
    print i19
    print i20
    print i20

def write(str):
    # changes the lists
    c = 0
    for i in str:
        if term.cursorPosX > lineSize:
            term.cursorPosX = 0
            if term.cursorPosY > 19:
                term.cursorPosY = 0
            else:
                term.cursorPosY = term.cursorPosY + 1

        if term.cursorPosY is 0:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 1:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 2:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 3:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 4:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 5:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 6:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 7:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 8:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 9:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 10:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 11:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 12:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 13:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 14:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 15:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 16:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 17:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 18:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

        elif term.cursorPosY is 19:
            _1.list[term.cursorPosX] = str[c]
            c = c + 1
            term.cursorPosX = term.cursorPosX + 1

def writf(str,y,x):
    write(str)
    term.setCursorPos(y,x)

def ask(x):
    i = raw_input(x)
    return i

def wait(i):
    time.sleep(i)

def cursorPos(y,x):
    term.setCursorPos(y,x)



# end of preliminary functions
# start of actual stuff
# start of Main Stuff

# start of game functions




def startScreen():
    writf('============================ ',8,10)
    writf('=                          = ',9,10)
    writf('=      TEXT ADVENTURE:     = ',10,10)
    writf('=    WAR OF ZE MONSTERS    = ',11,10)
    writf('=                          = ',10,10)
    writf('============================ ',12,10)
    draw()
    wait(5)



# end of game functions



def Main():


    startScreen()


Main()



# end of Stuff

# end of actual stuff

如果您想管理基于文本的“屏幕”,最好使用curses模块。这是专为你想要的,虽然有点复杂。此外,通过使用更多的函数,您可以使程序缩短18倍,因为您要多次重复相同的代码。

哎哟,这是一个荒谬的代码重复量。当同一代码需要在51个不同的位置进行更改时,您如何管理更改?您是否想过为每一行设置一个行列表,而不是单独的变量?
term.cursorPosY is 19
会让您大吃一惊。虽然
1000是1000
(500+500)不是1000
。不要将
is
用于数字comparisons@tripleee你认为你可以通过向我展示如何减少代码重复来帮助我吗?我对这种东西比较陌生。@Eric我做了,但我根本无法让它发挥作用