簡體   English   中英

Python 3.x中的簡單矩形終端動畫

[英]Simple rectangle terminal animation in Python 3.x

我有一個作業,其中我必須編寫一個函數,該函數將逐幀打印一個矩形(用戶可以更改其高度和寬度),並在其中包含一個“ x”的正方形(我盡了最大的努力來描述它,但是希望該圖片對您有所幫助)。

在我的代碼中,我將框架表示為行列表(用“ o”填充)。 我已經有一個函數可以找到矩形的中心,並用“ x”替換中間的“ o”,這是我的第一幀。

最初,我想使所有剩余的框架都具有嵌套循環,但是由於有2條水平線“ x”,所以它很快就引起了混亂。

我很確定我需要一個新的“坐標系”來遍歷框架。 有人可以指出我正確的方向嗎?

def squareAnimation():

    while True:

        frameHeight = int(input("Please enter frame's height: \nRemember: Odd numbers only!\n"))
        frameWidth = int(input("Please enter frame's width:\nRemember: Odd numbers only!\n"))


        if (frameHeight % 2 != 0 and frameWidth % 2 != 0):
            break
    numberOfFrames = min(int(frameWidth/2), int(frameHeight/2))

    lineTemplate = "o " * frameWidth
    #frame filled with "o" only
    blankFrame = [lineTemplate] * frameHeight

    #frame with x in center
    frameTemplate = blankFrame
    findListCenter(frameTemplate, frameHeight, frameWidth)
    printListWithoutBrackets(frameTemplate)

def findListCenter(frame, height, width):
    frame[int(height / 2)] = "o " * (int(width / 2)) + "x " + "o " * (int(width / 2))

def printListWithoutBrackets(frame):
    for object in range(0, len(frame)):
        tempLine = frame[object]
        print("".join(tempLine))

squareAnimation()

在此處輸入圖片說明

所以最終我想出了自己的解決方案。 它可能不是最漂亮的,但它可以工作。

import copy
import time

def squareAnimation():

    while True:

        height = int(input("Please enter frame's height: \nRemember: Odd numbers only!\n"))
        width = int(input("Please enter frame's width:\nRemember: Odd numbers only!\n"))

        if (height % 2 != 0 and width % 2 != 0):
            break
    numberOfFrames = min(int(width/2), int(height/2))
    middleRow = int(height / 2)
    middleColumn = int(width / 2)

    numberOfHorizontalXes = 3
    lineTemplate = "o " * width
    blankFrame = [lineTemplate] * height
    currentFrameIndex = 1

    completeListOfFrames = ([blankFrame] * (numberOfFrames + 1)) #an array filled with blankGrames

    firstFrame = copy.deepcopy(blankFrame)
    firstFrame [int(height / 2)] = "o " * (int(width / 2)) + "x " + "o " * (int(width / 2))

    completeListOfFrames[0] = firstFrame

    for frame in completeListOfFrames:
        if frame == firstFrame:
            continue

        numberOfO = int((width - numberOfHorizontalXes) / 2)
        loopFrameCopy = copy.deepcopy(frame)

        loopFrameCopy[middleRow + currentFrameIndex] = "o " * numberOfO + "x " * numberOfHorizontalXes + "o " * numberOfO
        loopFrameCopy[middleRow - currentFrameIndex] = "o " * numberOfO + "x " * numberOfHorizontalXes + "o " * numberOfO
        loopFrameCopy[middleRow] = "o " * numberOfO + "x " + "o " * (width - (2 * numberOfO + 2)) + "x " + "o " * numberOfO

        for wallIndex in range(1, currentFrameIndex):
            loopFrameCopy[middleRow + wallIndex] = "o " * numberOfO + "x " + "o " * (width - (2 * numberOfO + 2)) + "x " + "o " * numberOfO
            loopFrameCopy[middleRow - wallIndex] = "o " * numberOfO + "x " + "o " * (width - (2 * numberOfO + 2)) + "x " + "o " * numberOfO

        completeListOfFrames[currentFrameIndex] = loopFrameCopy

        numberOfO += 3
        numberOfHorizontalXes += 2
        currentFrameIndex += 1

    revCompleteListOfFrames = copy.deepcopy(completeListOfFrames[::-1])
    revCompleteListOfFrames.pop(0)
    finalList = completeListOfFrames + revCompleteListOfFrames
    printListWithoutBrackets(finalList)

def printListWithoutBrackets(frameOfFrames):

    for frame in frameOfFrames:
        for line in frame:

           print("".join(line))
        time.sleep(0.3)
        print("\n")

squareAnimation()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM