python - 在Python循环下访问迭代列表中的所有元素

标签 python list drawing pygame geometry

Possible Duplicate:
Python & Pygame: Updating all elements in a list under a loop during iteration

我正在用 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 循环更新对象列表中的所有元素?我已经尝试了很多天,但一直未能找到好的解决方案。任何想法表示赞赏。谢谢

最佳答案

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

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

关于python - 在Python循环下访问迭代列表中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11172444/

相关文章:

python - Python 和 Google SQL Cloud 之间的接口(interface)问题

python - 在 SciKit-Learn 中使用管道排列重要性

r - 在R中访问列表元素的正确方法

java - 如何使用 drawArc()

android - 如何在 Fragment 上使用 onDraw?

python - 生成器未按预期运行

python - 创建字典,其中键是变量名

python - 如何在python中将多行字符串转换为矩阵

Python 字典和类中的列表更新和追加

Java绘制Y坐标递增的图形