1

可能重复:
Python 和 Pygame:在迭代期间更新循环下列表中的所有元素

我正在使用 Python 编写程序并使用 Pygame。这是基本代码的样子:

import pygame ---and other stuff necessary

Class_name():

        draw_circle()

        --some other functions--- 

i = 0 

clicks = 0

object_list = []

while 1:

    screen.blit(background, (0,0))

    for event in pygame.event.get():

        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN and event.key == K_c:
            circle_create = True
            circle_list.append(Circle())

        if event.type == MOUSEBUTTONDOWN and circle_create == True:
            if clicks == 0:
                circle_list[i].center()
            clicks += 1


        if event.type == MOUSEMOTION and clicks == 1 and circle_create == True:
            circle_list[i].stretch()



    #circle.circle_computation()
    if circle_create == True:
        circle_list[i].draw_circle()

    if clicks == 2:
        clicks = 0
        i += 1
        circle_create = False

    pygame.display.update()

所以基本上,当按下“c”键时,会从类中创建一个对象。这个对象被添加到一个列表中并且列表被迭代。然后用户按下鼠标和其他东西来绘制一个圆圈并创建任意数量的圆圈。

我想要做的是让对象的draw_circle()函数通过循环不断更新,以便为列表中的所有对象显示绘制的圆圈,但是由于列表是迭代的,它会更新添加的新对象和对象已经附加的不更新。

该程序有效,它根据用户输入绘制圆圈,但更新问题是我需要解决的唯一问题。有没有办法让while循环更新对象列表中的所有元素?我已经尝试了很多天,但我无法找到一个好的解决方案。任何想法表示赞赏。谢谢

4

1 回答 1

1

根据您现有的代码和您声明的绘制所有圆圈的愿望,而不仅仅是最近的一个,我认为以下代码更改应该为您提供您正在寻找的行为:

#circle.circle_computation()
if circle_create == True:
    #circle_list[i].draw_circle()
    for j in xrange(i):
        circle_list[j].draw_circle()
于 2012-06-24T07:26:05.283 回答