如何解决遍历列表似乎跳过了一些项目
我想不出更好的方式来描述我的问题,所以我将在这里描述。
#Constants
RED = (255,0)
GREEN = (0,255,0)
BLUE = (0,255)
WHITE = (255,255)
BLACK = (0,0)
GREY = (200,200,200)
GraviTY = 0.001
#Name,position,Mass,Colour,Radius,Zone of influence
#["JupiterMoon",[75,300],5,GREY,2,1],bodyArray = [["EarthMoon",[160,["Jupiter",[50,10,RED,50],["Mars",[200,1,10],["Venus",[300,GREEN,["Earth",[150,BLUE,15],["Sun",[400,100,WHITE,20,800]]
import pygame,math
class Main():
#Initialise all variables and pygame environment
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800,600))
self.clock = pygame.time.Clock()
self.screen.fill(BLACK)
self.Universe = Universe()
self.run()
#Handling events
def eventhandler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
pygame.quit()
#Painting bodies to the screen
def run(self):
self.running = True
while self.running:
self.screen.fill(BLACK)
self.clock.tick(60)
self.eventhandler()
pygame.display.update()
class Bodies():
#Initialise bodies
def __init__(self,name,posXY,mass,colour,radius,zone):
self.name = name
self.pos = posXY
self.mass = mass
self.colour = colour
self.radius = radius
self.zone = zone
def getdistance(self,centre):
return math.sqrt((self.pos[0] - centre.pos[0])**2 + (self.pos[1] - centre.pos[1])**2)
class System():
def __init__(self,centre,orbiting):
self.centre = centre
self.orbitingBodies = orbiting
class Universe():
def __init__(self):
self.systemArray = [[[]]]
self.bodyArray = []
self.moonArray = []
for body in bodyArray:
B = Bodies(body[0],body[1],body[2],body[3],body[4],body[5])
self.bodyArray.append(B)
self.getSystems()
#Check if the body is
def inZoneOfInfluence(self,orbiting,centre):
if orbiting.getdistance(centre) < centre.zone and orbiting.getdistance(centre) > (orbiting.radius + centre.radius):
return True
else:
return False
#Calls update fuction and draws bodies to screen
def getSystems(self):
mutableBodyArray = []
for item in self.bodyArray:
mutableBodyArray.append(item)
for centre in self.bodyArray:
orbitingArray = []
orbitingNames = []
#print("centre name",centre.name)
#for item in mutableBodyArray:
#print(item.name)
print("Names from list")
for orbiting in mutableBodyArray:
#print(orbiting.name)
if self.inZoneOfInfluence(orbiting,centre):
orbitingArray.append(orbiting)
orbitingNames.append(orbiting.name)
#print("Item being removed",orbiting.name)
mutableBodyArray.remove(orbiting)
#print(orbiting.name,"affected by",centre.name)
system = [centre,orbitingArray]
#systemNames = [centre.name,orbitingNames]
print("\nRemaining items in list")
#for item in mutableBodyArray:
#print(item.name)
#print(systemNames)
#print("\n")
self.systemArray.append(System(system[0],system[1]))
m = Main()
我把所有的代码都复制进去了,有错误的部分是Universe类中的getSystems()函数。应该发生的情况是,如果其中一个物体绕着一个物体旋转,那么它就会从 mutableBodyArray 数组中移除,这样它就不能被放置在另一个中心物体周围。这一直工作到最后一次运行外循环,此时算法正在计算“SUn”周围应该有什么。此时它似乎跳过了 mutableBodyArray 中的“Mars”和“Earth”,即使它应该跳过所有项目。
该函数中所有注释掉的区域都是我试图测试它并弄清楚发生了什么,但除非我遗漏了一些明显的东西,否则我无法弄清楚。由于“地球”和“火星”仍然出现在最后的 mutableBodyArray 中。它们没有被移除,只是似乎被跳过了。
如果我的解释很糟糕,我深表歉意!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。