簡體   English   中英

龜圖形動畫(架子)

[英]Turtle graphics animation (shelf)

我想定義一個函數insert_animate(blockposition, shelf, high) ,使得其在去除塊blockpositionshelf和插入在該塊到一個較高的位置high ,在high是一個整數。 該函數應該在零和高位之間找到這個位置,不包括高位。 如果沒有找到這樣的位置,則將塊插入高位置。

我設法提出了這個功能:

def insert_animate(blockposition, shelf, high):
    if blockposition==0:
        return shelf
    else:
        a=s.pop(blockposition)
        for i in range(high):
            if a.size<=s[i].size:
                s.insert(i,a)
                break
            else:
                s.insert(high,a)
        return shelf

但是,這僅適用於某些情況,而不是全部。 我不知道我哪里出錯了。

s = shelf.init_shelf((2, 6, 1, 4, 8, 3, 9))
print(insert_animate(0, s, 0)) # returns [Block size: 2, Block size: 6, Block size: 1, Block size: 4, Block size: 8, Block size: 3, Block size: 9]
print(insert_animate(3, s, 3)) 
# returns [Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 3, Block size: 9]

但它應該是(第二次印刷):

[Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 3, Block size: 9]

print(insert_animate(6, s, 6))
# returns [Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 3, Block size: 9]

但它應該是:

[Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 8, Block size: 3, Block size: 9]

為什么我的代碼適用於某些情況但不適用於其他情況? 問題出在我的for循環中嗎? 如果有人可以提供幫助,我會非常感激! 謝謝!

這是我正在處理的文件:

from turtle import *

class Block(Turtle):
    def __init__(self, size):
        self.size = size
        Turtle.__init__(self, shape="square", visible=False)
        self.pu()
        self.shapesize(size * 1.5, 1.5, 2) # square-->rectangle
        self.fillcolor("black")
        self.st()
    def glow(self):
        self.fillcolor("red")
    def unglow(self):
        self.fillcolor("black")
    def __repr__(self):
        return "Block size: {0}".format(self.size)

class Shelf(list):
    def __init__(self, y):
        "create an shelf. y is y-position of first block"
        self.y = y
        self.x = -150
    def push(self, d):
        width, _, _ = d.shapesize()
        yoffset = width/2 * 20 # to align the blocks by it's bottom edge
        d.sety(self.y + yoffset)
        d.setx(self.x+34*len(self))
        self.append(d)
    def _close_gap_from_i(self, i):
        for b in self[i:]:
            xpos, _ = b.pos()
            b.setx(xpos - 34)
    def _open_gap_from_i(self, i):
        for b in self[i:]:
            xpos, _ = b.pos()
            b.setx(xpos + 34)
    def pop(self, key):
        b = list.pop(self, key)
        b.glow()
        b.sety(200)
        self._close_gap_from_i(key)
        return b
    def insert(self, key, b):
        self._open_gap_from_i(key)
        list.insert(self, key, b)
        b.setx(self.x+34*key)
        width, _, _ = b.shapesize()
        yoffset = width/2 * 20 # to align the blocks by it's bottom edge
        b.sety(self.y + yoffset)
        b.unglow()

def show_text(text):
    goto(0,-250)
    write(text, align="center", font=("Courier", 16, "bold"))

def start_sort():
    onkey(None,"space")
    clear()
    show_text("sort_me")
    sort_func(s)

def init_shelf(vals=(4, 8, 2, 9, 3, 1, 10, 7, 5, 6)):
    s = Shelf(-200)
    for i in vals:
        s.push(Block(i))
    return s

def clear_window():
    getscreen().clearscreen()

def main(func):
    global sort_func
    sort_func = func
    getscreen().clearscreen()
    ht(); penup()
    init_shelf()
    show_text("press spacebar to start sorting")
    onkey(start_sort, "space")
    onkey(bye, "Escape")
    listen()
    mainloop()

在調用insert_animate()之后很難按照你的啟動順序和最終訂單的例子,但這里是我猜錯了:

我相信你的else子句縮進不正確(太深),以至於當它真的應該成為for語句的一部分時它已經成為if語句的一部分:

def insert_animate(blockposition, shelf, high):

    if blockposition == 0:
        return shelf

    a = s.pop(blockposition)

    for i in range(high):
        if a.size <= s[i].size:
            s.insert(i, a)
            break
    else:  # no break
        s.insert(high, a)

    return shelf

正如最初編寫的那樣, else子句可以執行多次,但是當for循環通過除break之外的方式退出時,你只需要執行一次。 以這種方式綁定到循環的else子句可以被認為是“不中斷”,即如果循環具有break語句但是沒有被執行則執行此代碼。

暫無
暫無

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

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