简体   繁体   中英

How to create pygame rects one by one in a for loop?

I'm trying to create rects one by one. Each time the previous rect is deleted. The problem is that only the last one is created. If I comment out the line if index != 0 , such that all rects are created in once, the program works. But I need to create the rects one by one.

def draw_path(self, path):
    for index, i in enumerate(path):
        self.create_rect(colors['dark_red'], colors['red'], i[0] * TILE_SIZE, i[1] * TILE_SIZE, TILE_SIZE,TILE_SIZE)
        if index != 0:
            self.create_rect(colors['black'], colors['dark_gray'], path[index-1][0] * TILE_SIZE, path[index-1][1] * TILE_SIZE, TILE_SIZE, TILE_SIZE)
        self.player.position = (i[0], i[1])

I've tried to use the built in pygame functions delay , wait and timer , but it doesn't seem to fix the problem. Could anyone explain me, preferably with an example, how I can fix it?

you need to call the function pygame.screen.update() after each rect object is created and deleted if you want it to be displayed

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