I finished writing this code today. It works feel free to try it out, it is the prototype to my current code problem.
if your not interested in a lot of code just jump to the last code block and read the paragraph below for the question
import random
galaxyList = []
systemInfo = {"Star Type": '',"PlanetNum": '',"Planet List":[]}
planetInfo = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
starTypes = {
"O" : (0),
"B" : (0),
"A" : (0),
"F" : (0),
"G" : (0),
"K" : (0),
"M" : (0)
}
class SystemGen:
def __init__(self):
systemInfo = {"Star Type": '',"PlanetNum": '',"Planet List":[]}
#planetInfo = {"Planet Name": '',"Planet Type": '', "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
starTypes = {
"O" : (0),
"B" : (0),
"A" : (0),
"F" : (0),
"G" : (0),
"K" : (0),
"M" : (0)
}
def starTypeGen():
"""
Randomly generates the star type for the given system
"""
star = random.randint(1,1000)
if(star <= 3):
systemInfo["Star Type"] = "O"
elif(star <= 20):
systemInfo["Star Type" ]= "B"
elif(star < 70):
systemInfo["Star Type"] = "A"
elif(star < 200):
systemInfo["Star Type"] = "F"
elif(star < 390):
systemInfo["Star Type"] = "G"
elif(star < 650):
systemInfo["Star Type"] = "K"
elif(star <= 1000):
systemInfo["Star Type"] = "M"
else:
print("error star generation error")
return systemInfo
def planetNumGen():
"""
For given star type generates the number of planets found in that system.
"""
if systemInfo["Star Type"] == "O":
systemInfo[PlanetNum] = 0
elif systemInfo["Star Type"] == "B":
planetNum = (random.randint(-2,3))
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
elif systemInfo["Star Type"] == "A":
planetNum = random.randint(-2,4)
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
elif systemInfo["Star Type"] == "F":
planetNum = random.randint(-2,7)
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
elif systemInfo["Star Type"] == "G":
planetNum = random.randint(-2,9)
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
elif systemInfo["Star Type"] == "K":
planetNum = random.randint(-2,9)
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
elif systemInfo["Star Type"] == "M":
planetNum = random.randint(-2,5)
if planetNum <= 0:
systemInfo["PlanetNum"] = 0
else:
systemInfo["PlanetNum"] = planetNum
return systemInfo
def planetDictGen():
"""
Takes the number of planets in the system and appends them to the system's Planet List.
It then takes the Planets in that list and creates a dictionary for each planet containing
that planet's info/characteristics.
"""
for x in range(systemInfo["PlanetNum"]):
systemInfo["Planet List"].append("Planet " + str(x+1))
systemInfo["Planet List"][x] = {("Planet " + str(x+1)): planetInfo}
return systemInfo
def planetTypeGen():
if systemInfo["Star Type"] == "O":
pass
#this fucntion passes because this system doesn't generate planets
elif systemInfo["Star Type"] == "B":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,6)
if planet == 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For B type stars Terrestrial planets form (1/6), Gas Gaints (5/6) of the time
elif systemInfo["Star Type"] == "A":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,4)
if planet == 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For A type stars Terrestrial planets form (1/4), Gas Gaints (3/4) of the time
elif systemInfo["Star Type"] == "F":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,3)
if planet == 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For F type stars Terrestrial planets form (1/3), Gas Gaints (2/3) of the time
elif systemInfo["Star Type"] == "G":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,2)
if planet == 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For G type stars Terrestrial planets form (1/2), Gas Gaints (1/2) of the time
elif systemInfo["Star Type"] == "K":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,3)
if planet != 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For K type stars Terrestrial planets form (2/3), Gas Gaints (1/3) of the time
elif systemInfo["Star Type"] == "M":
tempTerraPlanetList = []
tempGasPlanetList = []
if systemInfo["PlanetNum"] > 0:
for x in range(systemInfo["PlanetNum"]):
planet = random.randint(1,4)
if planet != 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Terra planet type to the first planets found in the systemInfo["Planet List"]
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
systemInfo["Planet List"][x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the systemInfo["Planet List"]
return systemInfo
#For M type stars Terrestrial planets form (3/4), Gas Gaints (1/4) of the time
def dwarfPlanetGen(self):
pass
def largeAstroidGen(self):
pass
def print():
for x in systemInfo["Planet List"]:
print(x)
def run():
SystemGen.starTypeGen()
SystemGen.planetNumGen()
SystemGen.planetDictGen()
SystemGen.planetTypeGen()
SystemGen.print()
To run the program just type these lines of code one at a time into your interactive shell. It doesn’t take any arguments so done try to attach the class to a variable, just type the commands like this directly:
SystemGen.starTypeGen()
SystemGen.planetNumGen()
SystemGen.planetDictGen()
SystemGen.planetTypeGen()
SystemGen.print()
The starTypeGen() generates a star for your system.
The planetNumGen() generates the number of planets
The planetDictGen() creates the planets info
the planetTypeGen() creates whether the planet will be a terrestrial planet or gas giant.
and print() simply prints out a list of the planets created so its easier to read.
This was just my prototype and now that I got it to work I set out to put the information to proper classes, but I have weak understanding when to call a variable and proper ways to store it in the class. To that end I have cut most of planetTypeGen types to keep it simple.
this is my first class that creates the system called SystemGen.py
SystemGen.py:
import random
class SystemGen:
import random
class SystemGen:
def __init__(self):
self.starType = ''
self.planetNum = ''
self.planetList = []
def starTypeGen(self):
"""
Randomly generates the star type for the given system out of O,B,A,F,G,K,M class stars
"""
star = random.randint(1,1000)
if(star <= 3):
self.starType = "O"
elif(star <= 20):
self.starType = "B"
elif(star < 70):
self.starType = "A"
elif(star < 200):
self.starType = "F"
elif(star < 390):
self.starType = "G"
elif(star < 650):
self.starType = "K"
elif(star <= 1000):
self.starType = "M"
else:
print("error star generation error")
return self.starType
def planetNumGen(self):
"""
For given star type generates the number of planets found in that system.
"""
if self.starType == "O":
self.planetNum = 0
elif self.starType == "B":
numOfPlanets = (random.randint(-2,3))
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
elif self.starType == "A":
numOfPlanets = random.randint(-2,4)
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
elif self.starType == "F":
planetNum = random.randint(-2,7)
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
elif self.starType == "G":
numOfPlanets = random.randint(-2,9)
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
elif self.starType == "K":
numOfPlanets = random.randint(-2,9)
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
elif self.starType == "M":
numOfPlanets = random.randint(-2,5)
if numOfPlanets <= 0:
self.planetNum = 0
else:
self.planetNum = numOfPlanets
return self.planetNum
def appendPlanetList(self):
"""
Takes the number of planets in the system and appends them to the system's Planet List.
"""
for x in range(self.planetNum):
self.planetList.append("Planet " + str(x+1))
return self.planetList
def planetTypeGen():
if self.starType == "O":
pass
#this fucntion passes because this system doesn't generate planets
elif self.starType == "B":
tempTerraPlanetList = []
tempGasPlanetList = []
if self.planetNum > 0:
for x in range(self.planetNum):
planet = random.randint(1,6)
if planet == 1:
tempTerraPlanetList.append("Terrestrial")
#adds planet type to tempTerraPlanetList
else:
tempGasPlanetList.append("Gas Giant")
#adds planet type to tempGasPlanetList
for x in range(len(tempTerraPlanetList)):
planetInfo = {"Planet Name": '',"Planet Type": "Terrestrial", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
self.planetList[x] = planetInfo
#this appends the Terra planet type to the first planets found in the self.planetList
for x in range((len(tempTerraPlanetList)),((len(tempTerraPlanetList))+(len(tempGasPlanetList)))):
planetInfo = {"Planet Name": '',"Planet Type": "Gas Giant", "Planet Char":'', "inSystem Loc": '', "inPlanet Loc List": [],}
self.planetList[x] = planetInfo
#this appends the Gas planet type to the planets found after the Terra in the self.planetList
return systemInfo
#For B type stars Terrestrial planets form (1/6), Gas Gaints (5/6) of the time
In the last function where I create a dictionary called planetInfo is old code where it should call a function from PlanetGen.py called terrestrialPlanetGen() and gasGiantPlanetGen() shown below.
PlanetGen.py:
from SystemGen import SystemGen
class Planet():
def __init__(self, starType):
self.starType = starType
self.name = ''
self.location = ''
self.type = ''
self.characteristics = []
self.planetLocations = []
def terrestrialPlanetGen(self):
pass
def gasGiantPlanetGen(self):
pass
My question is how do I call proper variables from different class and how do I know they get properly initialized. All the python books that I have learned python from are always very weak when it came to explaining how properly call cross class varibales without accidently creating new local varibales with the same name.
Since there is a lot of code in this post, how about just focus on the PlanetGen.py How do I call the starType from SystemGen.py and store that variable and where, and for my better understanding could you explain why you store it there?
Also is it better to save each class as an individual file or stick many classes together when it comes to working with Panda3d?