0

我想不出更好的方式来表达我的问题,所以我将在这里描述它。

#Constants
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
BLACK = (0,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,300],5,GREY,2,1],
             ["Jupiter",[50,300],10,RED,10,50],
             ["Mars", [200,300],1,RED,5,10],
             ["Venus", [300,300],2,GREEN,5,10],
             ["Earth",[150,300],5,BLUE,5,15],
             ["Sun",[400,300],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 中。他们没有被删除,他们只是似乎被跳过了。

如果我的解释很糟糕,我深表歉意!

4

0 回答 0