简体   繁体   中英

How to make the movement in this graphics.py code more fluent?

I just started using graphics.py and am attempting to make some moving rain.

from graphics import *
import random as r

rects = []
colorList = [color_rgb(255, 170, 204), color_rgb(255, 187, 204), color_rgb(255, 204, 204), 
             color_rgb(255, 221, 204), color_rgb(255, 238, 204)]
def main():
    r.seed()
    win = GraphWin("Random Squares", 800, 800)
    win.setBackground("black")
    for i in range(3000):
        x1 = r.randint(0,800)
        x2 = r.randint(0,10)
        y1 = x1+5
        y2 = x2+20
        var = Rectangle(Point(x1,x2), Point(y1,y2))
        rects.append(var)
        rects[i].setFill(r.choice(colorList))
        rects[i].draw(win)
        for i in range(len(rects)):
            rects[i].move(0,r.randint(10,100))
            update(10000)
    win.getMouse()
    win.close()

if __name__ == '__main__':
    main()

The issue I think I'm having is that the movement update is occurring as each new rectangle is added. Can anyone help me come up with a better way to do this?

My advice is forget about autoflush and update() until you've got your algorithm running at its fastest. Specifically, you eventually end up with 3000 rectangles that you're updating even though there are never more than ~ 15 on the screen at a time. You might be better off getting rid of rectangles that have fallen off the bottom:

from random import seed, randint, choice
from graphics import *

WIDTH, HEIGHT = 800, 800

colorList = [
    color_rgb(255, 170, 204),
    color_rgb(255, 187, 204),
    color_rgb(255, 204, 204),
    color_rgb(255, 221, 204),
    color_rgb(255, 238, 204)
    ]

def main():
    seed()

    win = GraphWin("Random Squares", WIDTH, HEIGHT)
    win.setBackground("black")

    rects = []

    for _ in range(3000):
        for rect in list(rects):  # iterate over a shallow copy
            rect.move(0, randint(10, 100))

            if rect.getP1().getY() > HEIGHT:
                rect.undraw()
                rects.remove(rect)

        x1 = randint(0, WIDTH - 5)
        y1 = randint(0, 10)

        rect = Rectangle(Point(x1, y1), Point(x1 + 5, y1 + 20))
        rect.setFill(choice(colorList))
        rect.draw(win)

        rects.append(rect)

    win.getMouse()
    win.close()

if __name__ == '__main__':
    main()

Now we're only tracking ~15 rectangles instead of hundreds or thousands. Only after you get your algorithm optimized, consider autoflush and update() if the performance is not to your liking:

def main():
    seed()

    win = GraphWin("Random Squares", WIDTH, HEIGHT, autoflush=False)
    win.setBackground("black")

    rects = []

    for _ in range(3000):
        for rect in list(rects):  # iterate over a shallow copy
            rect.move(0, randint(10, 100))

            if rect.getP1().getY() > HEIGHT:
                rect.undraw()
                rects.remove(rect)

        x1 = randint(0, WIDTH - 5)
        y1 = randint(0, 10)

        rect = Rectangle(Point(x1, y1), Point(x1 + 5, y1 + 20))
        rect.setFill(choice(colorList))
        rect.draw(win)

        update()

        rects.append(rect)

    win.getMouse()
    win.close()

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