如何保持步进动画

时间:2015-02-28 16:25:25

标签: python python-2.7

我正在尝试制作一个节目:

  1. 模拟随机游走
  2. 使用cs1graphics

    为步行设置动画
    • 从黑色的中心区块开始
    • 采取随机步骤,从未踩过红色,重复步骤变为蓝色。
  3. import random
    from cs1graphics import *
    from time import sleep
    
    def animationWalk(walk):
        print("Animation of Random Walk: ", end = "\n")
        window = Canvas(250, 250)
        window.setTitle('Random Walk in Manhattan')
        for y in range(10) :
            for x in range(10) :
                cue = Square()
                cue.setSize(50)
                cue.moveTo(x*25, y*25)
                window.add(cue)
    
        (x,y)= (6,6)
        squares = Square()
        squares.setSize(25)
        squares.moveTo((x*25)-14, (y*25)-13)
        squares.setFillColor('black')
        window.add(squares)
        been = Square()
        been.setSize(25)
        been.moveTo((x*25)-14, (y*25)-13)
        window.add(been)
        for direction in range (len(walk)):
                if walk[direction] == 'N':
                    #y -= 1
                    (x,y)=(x,y-1)
                elif walk[direction] == 'E':
                    #x += 1
                    (x,y)=(x+1,y)
                elif walk[direction] == 'S':
                    #y += 1
                    (x,y) =(x,y+1)
                elif walk[direction] == 'W':
                    #x -= 1
                    (x,y) = (x-1,y)
                been.setSize(25)
                been.moveTo((x*25)-14, (y*25)-13)
                been.setFillColor('red')
                cue.setSize(25)
                sleep(0.25)
                cue.moveTo((x*25)-14, (y*25)-13)
                cue.setFillColor('blue')   
    
    def randomWalk(x,y):
        block = []
        for i in range (x):
            block.append([])
        for i in block:
            for j in range(y):
                i.append(0)
        position = (x//2, y//2)
        h = position[0]
        v = position[1]
        walk = ''  
        block[h][v] += 1
        while (h != -1) and (h != (x-1)) and (v != -1) and (v != (y-1)):
            directions = random.randrange(1,5)
            if directions == 1:
                v += 1
                walk = walk + 'E'
            elif directions == 2:
                h += 1
                walk = walk + 'S'
            elif directions == 3:
                v -= 1
                walk = walk + 'W'
            elif directions == 4:
                h -= 1
                walk = walk + 'N'
            block[h][v] += 1
    
        print("Starting at Center (", x//2, ",", y//2, ")")
        print("Walking Directions: ", walk)
        print("Track of Random Walk:", end = "\n")
        for entry in block:
            print(entry)
        animationWalk(walk)
    
    
    def main():                     ## define main program
        x = 10
        y = 10
    
        randomWalk(x,y)
    main()
    

    正方形的颜色在访问时会变为红色,在重新访问时会变为蓝色,在传递时会变为红色。在它离开后我不能让块保持颜色。

0 个答案:

没有答案