Python 3.x ASCII-ascimatics-如何在代码中实现效果/屏幕

Python 3.x ASCII-ascimatics-如何在代码中实现效果/屏幕,python-3.x,ascii,ascii-art,Python 3.x,Ascii,Ascii Art,几篇帖子之前,有人建议我研究Python的Ascimatics库。我正试着用以下方法让我的头脑清醒过来: 样本- 文件- 然而,作为一个新手,我仍然对其中一些非常基本的东西感到困惑。我知道,通过使用effect数组,您可以创建代码层,根据它们在数组中的位置覆盖屏幕上的信息。它们的索引号越小,它们的可见信息就越少 以及我想在其中使用的代码。目标是把墙和脚印放在顶层,蓝色圆圈放在底层。代码部分工作,因此您仍然可以享受我的可视化: 这完全取决于你如何实现你的新效果。。。 简而言之,您需要一种清除旧图像

几篇帖子之前,有人建议我研究Python的Ascimatics库。我正试着用以下方法让我的头脑清醒过来:

样本- 文件- 然而,作为一个新手,我仍然对其中一些非常基本的东西感到困惑。我知道,通过使用effect数组,您可以创建代码层,根据它们在数组中的位置覆盖屏幕上的信息。它们的索引号越小,它们的可见信息就越少

以及我想在其中使用的代码。目标是把墙和脚印放在顶层,蓝色圆圈放在底层。代码部分工作,因此您仍然可以享受我的可视化:


这完全取决于你如何实现你的新效果。。。 简而言之,您需要一种清除旧图像的策略

为应用程序设置动画的最佳方法是使用并重新绘制每个新图像。这是在Ascimatics中使用的技术,用于在重新绘制屏幕时防止出现瑕疵。不幸的是,当您在刷新时交换缓冲区时,它不会清除屏幕,因此您需要自己在v1.x中进行此操作。完成后,可以根据需要绘制覆盖图,如果有帮助,可以使用单独的效果


例如,如果您将墙作为底部效果,并使其在绘制墙的同时清除整个屏幕,那么您的其他效果只需绘制其当前图像而不清除旧图像。

不清楚您在这里要问什么。。。您想知道如何将当前代码转换为使用3种不同的效果-墙、脚印和圆圈各一种效果?好吧,我的代码现在的问题是,它的每个可视部分都在同一层上。因此,每当代码的一部分清除自身后面的字符时,就像蓝色圆圈覆盖+一样,它也会清除代码不同部分留下的字符。在这种情况下,墙壁的部分或由绿点构成的脚印。请查看所附的描述问题的照片:所以我的问题是,将代码的不同部分划分为不同的效果是否有助于保持它们的活力?如果我清除效果为1的像素,它也会清除效果为2的像素吗?或者它只是清除图层,显示它下面的任何效果?
# Programs generate exit point along the wall, then spawn bot in random location. 
#He will move around and look for the exit, using different tools. 
#7.0 - Program randomly moves from starting point and makes 9 different steps. 
#It is forbidden for it to step in same place more than one.
#7.1 - Program not only moves, but also interact with the walls. 
#It will not move on any wall or other forbidden point that can be added to the list.
#7.2 - Added circle around the bot that travels with him

from asciimatics.screen import Screen
import os
import random
import math
import time

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


def exit_point():
    ''' Randomly select the wall, where exit point will be.
    Then select one random point along the line(wall) and create there 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():
    ''' Select coordinates for starting point inside the wall zone,
    which is smaller by one then wall size, so it will not spawn it on the wall.'''
    global startX
    global startY

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


def setup(screen):
    ''' Creates wall image from #.  Red # represents Exit. 
    Then create starting point and spawn our hero @.'''
    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):
    ''' - First program will re-create walls, exit point and spawn our hero @.
        - Then the wall is defined as array of points. Points are added to big, 
        global list of forbidden points, where no move can be made
        - To the same list, starting point is added
        - '''

    #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)

    #list of points where new point can not be created
    forbidden_place = []
    wall = []
    for i in range(49):
        point = [i,0]
        wall.append(point)          #wall no. 1
        point = [49,i]          
        wall.append(point)          #wall no. 2
        point = [i,49]          
        wall.append(point)          #wall no. 3
        point = [0,i]           
        wall.append(point)          #wall no. 4

    forbidden_place = wall

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

    #moves and looking around animation
    moves = 1
    while moves < 500:  

        #radius around our here where it will see things. Right now it's nothing else like visualisatios. 
        radius = 10
        for i in range(radius):
            XL = -(round(math.sqrt(radius*radius-i*i)))     #XL - X left, for X points on left side of the circle.
            XR =  (round(math.sqrt(radius*radius-i*i)))     #XR - X right, for X points on right side of the circle.
            Y = i                                           #iterated after Y coordinate and calculate X for left and right

            YU =  (round(math.sqrt(radius*radius-i*i)))     #YU - Y up, for Y points on the topside of the circle.
            YD = -(round(math.sqrt(radius*radius-i*i)))     #YD - Y down, for Y points on the downside of the circle.
            X = i                                           #iterated after X coordinate and calculate Y for top and bottom


            if moves > 1:           
                #if is here, otherwise it will use exit point and delete points in circle around it 
                #this part inide the if will clear last points of the radius light, 
                #by using point where hero was one move before
                screen.print_at(" ",  XR + forbidden_place[-2][0], Y  + forbidden_place[-2][1], 4, 1)
                screen.print_at(" ",  XL + forbidden_place[-2][0], Y  + forbidden_place[-2][1], 4, 1)
                #fill all holes after first two lines of the code, by making same circle, but rotated by 90deg
                screen.print_at(" ",  X + forbidden_place[-2][0],  YU + forbidden_place[-2][1], 4, 1)
                screen.print_at(" ",  X + forbidden_place[-2][0],  YD + forbidden_place[-2][1], 4, 1)

                screen.print_at(" ",  XR + forbidden_place[-2][0], -Y  + forbidden_place[-2][1], 4, 1)
                screen.print_at(" ",  XL + forbidden_place[-2][0], -Y  + forbidden_place[-2][1], 4, 1)
                #fill all holes after first two lines of the code, by making same circle, but rotated by 90deg
                screen.print_at(" ", -X + forbidden_place[-2][0],   YU + forbidden_place[-2][1], 4, 1)
                screen.print_at(" ", -X + forbidden_place[-2][0],   YD + forbidden_place[-2][1], 4, 1)  

            screen.print_at("+",  XR + forbidden_place[-1][0], Y  + forbidden_place[-1][1], 4, 1)
            screen.print_at("+",  XL + forbidden_place[-1][0], Y  + forbidden_place[-1][1], 4, 1)       
            screen.print_at("+",  X + forbidden_place[-1][0],  YU + forbidden_place[-1][1], 4, 1)
            screen.print_at("+",  X + forbidden_place[-1][0],  YD + forbidden_place[-1][1], 4, 1)

            screen.print_at("+",  XR + forbidden_place[-1][0], -Y  + forbidden_place[-1][1], 4, 1)
            screen.print_at("+",  XL + forbidden_place[-1][0], -Y  + forbidden_place[-1][1], 4, 1)      
            screen.print_at("+", -X + forbidden_place[-1][0],   YU + forbidden_place[-1][1], 4, 1)
            screen.print_at("+", -X + forbidden_place[-1][0],   YD + forbidden_place[-1][1], 4, 1)      

        #refresh wall visualisation, otherwise it would be 'eaten' by light. 
        #This part will be deleted after I learn how to use effects/scenes
        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)    

        #this part will generate movement points for our here. 
        #It will do it's best to not step in same place twice. So here will most likely stuck at some point. 
        #In future, there will be more logic that will allow him to move over his steps, if no other way is possible.
        moveX = forbidden_place[-1][0] + random.randint(-1,1)
        if moveX == forbidden_place[-1][0]:     
            moveY = forbidden_place[-1][1] + random.randrange(-1,2,2)   #select between -1 and 1, but not 0
        else:
            moveY = forbidden_place[-1][1] + random.randint(-1,1)       #select between -1 and 1, including 0
        point = [moveX,moveY]
        if point not in forbidden_place: 
            forbidden_place.append(point)
            screen.print_at(".", forbidden_place[-2][0], forbidden_place[-2][1], 2, 1) #footprint of the hero
            screen.print_at("@", moveX, moveY , 3, 1)                                  #hero's current position
            moves = moves + 1
        else:
            moveX = forbidden_place[-1][0]  #if point is already on the list, to prevent it overwrite variable moveX and moveY
            moveY = forbidden_place[-1][1]  #the program will clear it, by assigning it to last legit value from trace list,
        time.sleep(0.1)
        screen.refresh()
    input()

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