Pygame for循环仅运行一次

时间:2019-07-29 19:33:26

标签: python for-loop pygame

我正在制作一款平台游戏。我有一个平台列表,然后有一个“ platform_list:中的for平台”,即使有两个或更多平台,for循环也只能运行一次。我还有另一个文件,用于存储平台类和make_platform()函数。

###### platformer_main.py #######
from platform import make_platform

def platform_handler():
    print(len(platform_list)) # even if this prints out 2, the for loop still only runs once
    for platform in platform_list:
        pygame.draw.rect(window, green, (platform.x, platform.y, platform.w, platform.h))  # draw

        platform.x -= platform.speed  # move to the left
        if platform.done == False:  # add next platform
            if platform.x <= 650:
                platform.done = True
                add_platform()

        if platform.x + platform.w <= 0:  # delete platform
            platform_list.remove(platform)
            return platform_list
        # character collision detection
        if charac.x + charac.w >= platform.x and charac.x <= platform.x + platform.w and charac.y + charac.h >= platform.y and charac.y <= platform.y + platform.h:
            charac.fall = False
        else:
            charac.fall = True
        return charac
###### platform.py #####

class Platform:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.w = 0
        self.h = 0
        self.speed = 0
        self.done = 0


def make_platform():
    platform = Platform()

    platform.x = 1000
    platform.y = random.randrange(200, 600)
    platform.w = random.randrange(300, 500)
    platform.h = 20
    platform.done = False
    platform.speed = 5

    return platform

没有错误消息发生。

1 个答案:

答案 0 :(得分:2)

return使函数返回,并停止正在进行的任何循环。