print_at-使用字符串以外的变量

时间:2018-08-16 13:18:55

标签: python python-3.x ascii

是否有一种方法可以从asciimatics库中将字符串以外的变量传递给print_at?

手册说明:

print_at(text, x, y, colour=7, attr=0, bg=0, transparent=False)
使用指定的颜色和属性在指定位置打印文本。
参数

  • 文本–要打印的(单行)文本。
  • x –文本开头的列(x坐标)。
  • y –文本开头的行(y坐标)。
  • color –要显示的文本的颜色。
  • attr –要显示的文本的单元格属性。
  • bg –要显示的文本的背景色。
  • 透明–是否打印空格,从而产生透明效果。

我正在测试的代码:

from asciimatics.screen import Screen

def print_test(screen):
    var1 = "string" #works
    var2 = int(2)   #error
    var3 = 3        #error
    var4 = [1,2,3]  #error
    var5 = (1,2,3)  #error
    screen.print_at(var1, 10, 10, 1, 1)
    screen.refresh()
    input()
Screen.wrapper(print_test)

完整代码: 程序创建以绿色“ @”表示的起点,然后进行10个动作,以黄色“ @”表示。我要实现的是用数字将黄色“ @”更改为数字,以查看按什么顺序进行操作。

from asciimatics.screen import Screen
import os
import random

os.system('mode con: cols=51')


def exit_point():
    global exitX
    global exitY

    wall = random.randint(1,4)

    if wall == 1:
        exitX = random.randint(1,49)
        exitY = 0
    elif wall == 2:
        exitX = 49
        exitY = random.randint(1,49)
    elif wall == 3:
        exitX = random.randint(1,49)
        exitY = 49
    elif wall == 4:
        exitX = 0
        exitY = random.randint(1,49)


def start_point():
    global startX
    global startY

    startX = random.randint(2,48)
    startY = random.randint(2,48)


def setup(screen):
    screen.fill_polygon([[(0, 0), (50, 0), (50, 50), (0, 50)],[(1, 1), (49, 1), (49, 49), (1, 49)]])
    exit_point()
    screen.print_at("#", exitX, exitY, 1, 1)
    start_point()
    screen.print_at("@", startX, startY, 2, 1)
    screen.refresh()
    input()



def move(screen):
    #trace list
    trace = []

    #bring back setup screen, waste of code but more intuiative
    screen.fill_polygon([[(0, 0), (50, 0), (50, 50), (0, 50)],[(1, 1), (49, 1), (49, 49), (1, 49)]])
    screen.print_at("#", exitX, exitY, 1, 1)
    screen.print_at("@", startX, startY, 2, 1)


    #Add starting point to the list
    point = [startX,startY]
    trace.append(point)

    #1st move
    moveX = startX + random.randint(-1,1)
    moveY = startY + random.randint(-1,1)
    point = [moveX,moveY]
    trace.append(point)
    screen.print_at("@", moveX, moveY , 3, 1)

    #more moves
    moves = 1
    while moves < 10:
        moveX = moveX + random.randint(-1,1)
        moveY = moveY + random.randint(-1,1)
        point = [moveX,moveY]
        if point not in trace: 
            trace.append(point)
            screen.print_at("@", moveX, moveY , 3, 1)
            moves = moves + 1

    screen.refresh()
    input()

Screen.wrapper(setup)
Screen.wrapper(move)
input()

2 个答案:

答案 0 :(得分:0)

看起来print_at不能接受字符序列以外的任何东西。但是您可以在将值传递给函数之前将其转换为字符串。

screen.print_at(str(var4), 10, 10, 1, 1)

如果您想“确定,但是将所有print_at调用更改为使用str()会很不方便,则可以创建Screen的子类来实现此目的您会自动。

class PermissiveScreen(Screen):
    def print_at(self, text, *args, **kwargs):
        super().print_at(str(text), *args, **kwargs)

答案 1 :(得分:0)

您的确切问题的答案似乎是“不,您不能”。 但是,这并不意味着您无法实现所描述的目标。 将其作为参数传递时,为什么不将要由print_at打印的数字转换为字符串?

在较大的代码中

像这样

def print_test(screen):
    var1 = "string" #works
    var2 = int(2)   #error
    var3 = 3        #error # Let's try to pass this
    var4 = [1,2,3]  #error
    var5 = (1,2,3)  #error
    screen.print_at(str(var3), 10, 10, 1, 1) # Here is the conversion
    screen.refresh()
    input()
Screen.wrapper(print_test)