Python& Pygame:在迭代循环中更新列表中的所有元素 [英] Python & Pygame: Updating all elements in a list under a loop during iteration

查看:241
本文介绍了Python& Pygame:在迭代循环中更新列表中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python中使用Pygame开发程序。这是基本代码的样子:

 而1:

screen.blit(background, (0,0))
pygame.event.get()中的事件

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

如果event.type == KEYDOWN和event.key == K_c:
circle_create = True
circle_list.append(Circle())

如果event.type == MOUSEBUTTONDOWN和circle_create == True:
如果点击== 0:
circle_list [i] .center()
点击+ = 1


如果event.type == MOUSEMOTION并点击== 1和circle_create == True:
circle_list [i] .stretch()

如果circle_create == True:
circle_list [i] .draw_circle()

如果点击== 2:
clicks = 0
i + = 1
circle_create = False

pygame.display.update()

我想做什么有对象的功能的draw_circle()将被循环不断更新,以便为列表中的所有对象显示绘制的圆,但是由于列表被迭代,所以更新添加的新对象,并且已经附加的对象不被更新。



该程序工作,它在用户输入时绘制圈子,但更新问题是我需要解决的唯一问题。有没有可能的方式让对象列表中的所有元素都被while循环更新?我已经尝试了很多天,我没有找到一个很好的解决方案。任何想法都赞赏。谢谢

解决方案

编辑:我以为你已经尝试过:https://stackoverflow.com/a/11172885/341744


该程序,作品,它绘制用户输入的圈子,但
更新问题是我需要解决的唯一问题。有没有任何
可能的方法来让对象列表中的所有元素都被while循环更新


您可以迭代一个临时列表,例如,如果您在迭代时杀死演员。

  ]:
circle.update()


i am working on a program in Python and using Pygame. this is what the basic code looks like:

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()

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

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

 pygame.display.update()

what i want to do is have the object's function of draw_circle() to be constantly updated by the loop so that the drawn circle is shown for all objects in the list, but since the list is iterated it updates the new object added and the objects already appended are not updated.

The program, works, it draws the circles upon user input but the update problem is the only issue i need to solve. Is there any possible way to have all elements in the list of objects being updated by the while loop? i have tried for many days and i have not been able to find a good solution. any ideas are appreciated. Thanks

解决方案

edit: I thought you already tried : https://stackoverflow.com/a/11172885/341744

The program, works, it draws the circles upon user input but the update problem is the only issue i need to solve. Is there any possible way to have all elements in the list of objects being updated by the while loop?

You could iterate on a temporary list, for example, if you kill actors while iterating.

for circle in circles[:]:
    circle.update()

这篇关于Python& Pygame:在迭代循环中更新列表中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆