简体   繁体   中英

Simple rectangle terminal animation in Python 3.x

I hava an assignment where I have to write a function that will print a rectangle (user can change it's height and width) frame by frame with a square of "x"'es inside of it (I tried my best to describe it, but I hope the image will help).

In my code I represent frame as a list of rows (filled with "o"s). I already have a function that finds the rectangle's center and replaces the middle "o" with "x"- that's my first frame.

Originally I wanted to make all remaining frames with nested loops but it got confusing fairly quickly since there are 2 horizontal lines of "x"es.

I'm pretty sure I need a new "coordinate system" to iterate over frames. Can someone please point me to the right direction?

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

在此处输入图片说明

So eventually I came up with my own solution. It might not be the prettiest one, but it works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM