简体   繁体   English

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

[英]Python & Pygame: Updating all elements in a list under a loop during iteration

i am working on a program in Python and using Pygame. 我正在使用Python并使用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. 我想要做的是让循环不断更新draw_circle()的对象函数,以便为列表中的所有对象显示绘制的圆,但是由于列表是迭代的,它会更新添加的新对象和对象已经附加的内容未更新。

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? 有没有办法让while循环更新对象列表中的所有元素? i have tried for many days and i have not been able to find a good solution. 我已经尝试了很多天,但我找不到一个好的解决方案。 any ideas are appreciated. 任何想法都表示赞赏。 Thanks 谢谢

To draw all the circles in your list, just iterate through them and draw them before each call to update: 要绘制列表中的所有圆圈,只需遍历它们并在每次更新调用之前绘制它们:

for circle in circle_list:
    circle.draw_circle()

Edit: the OP had posted incorrectly formatted code, but says the actual code is fine, so removed that suggestion 编辑:OP发布了错误格式化的代码,但说实际代码很好,所以删除了该建议

You need to redraw the whole list after your blit(it covers the whole screen with the surface 'background' and 'erases' it), its not conditional, you need to iterate over the whole list and draw it. 您需要在blit之后重绘整个列表(它覆盖整个屏幕,表面'背景'和'擦除'它),它不是有条件的,你需要迭代整个列表并绘制它。 Them in the event part you decides who enters and who leaves the list. 他们在事件部分决定谁进入和离开列表。

Loop:
  Blit,starting new

  Event, here you decide who moves, who begin or cease to exist(append/remove)

  Redraw whole list, everyone in the circle_list.

edit: I thought you already tried : https://stackoverflow.com/a/11172885/341744 编辑:我以为你已经尝试过了: 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? 有没有办法让while循环更新对象列表中的所有元素?

You could iterate on a temporary list, for example, if you kill actors while iterating. 您可以迭代临时列表,例如,如果您在迭代时杀死actor。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM