问题描述
我正在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循环更新?我已经尝试了很多天,我没有找到一个很好的解决方案。任何想法都赞赏。谢谢
编辑:我以为你已经尝试过:
您可以迭代一个临时列表,例如,如果您在迭代时杀死演员。
]:
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
You could iterate on a temporary list, for example, if you kill actors while iterating.
for circle in circles[:]:
circle.update()
这篇关于Python& Pygame:在迭代循环中更新列表中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!