0

I couldn't think of a better way to phrase my problem so I will describe it here.

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

I have copied in all the code for reference, the part of the code with errors in is the getSystems() function in the Universe class. What's supposed to happen is that if one of the objects is orbiting around an object then it is removed from the mutableBodyArray array so that it can't be put around another central object. This is working up until the last time the outside loop runs, when the algorithm is working out what should be around the "SUn". At this point it seems to skip through "Mars" and "Earth" in the mutableBodyArray, even though it should be going through all of the items.

All the commented out regions in that function have been me trying to test it and work out what is happening but unless I am missing something obvious I can't figure it out. As both "Earth" and "Mars" still appear in the mutableBodyArray at the end. They aren't being removed, they just seem to be getting skipped.

I do apologise if my explanation is awful!

J4ns323
  • 27
  • 6

0 Answers0