0
私のクラスプログラム:継承を使用してSunおよび惑星用のスーパークラスSunPlanetを作成します。
import turtle
import math
class SunPlanet:
def __init__(self,iname,irad,im):
self.name = iname
self.radius = irad
self.mass = im
def getMass(self):
return self.mass
class Sun(SunPlanet):
def __init__(self, iname, irad, im, itemp):
super().__init__(self,iname,irad,im)
self.temp = itemp
self.x = 0
self.y = 0
self.sturtle = turtle.Turtle()
self.sturtle.shape("circle")
self.sturtle.color("yellow")
# other methods as before
def __str__(self):
return self.name
def getXPos(self):
return self.x
def getYPos(self):
return self.y
class Planet(SunPlanet):
def __init__(self, iname, irad, im, idist, ivx, ivy, ic):
super().__init__(self,iname,irad,im)
self.distance = idist
self.x = idist
self.y = 0
self.velx = ivx
self.vely = ivy
self.color = ic.strip()
self.pturtle = turtle.Turtle()
self.pturtle.color(self.color)
self.pturtle.shape("circle")
self.pturtle.up()
self.pturtle.goto(self.x,self.y)
self.pturtle.down()
#other methods as before
def getXPos(self):
return self.x
def getYPos(self):
return self.y
# animation methods
def moveTo(self, newx, newy):
self.x = newx
self.y = newy
self.pturtle.goto(newx, newy)
def getXVel(self):
return self.velx
def getYVel(self):
return self.vely
def setXVel(self, newvx):
self.velx = newvx
def setYVel(self, newvy):
self.vely = newvy
class SolarSystem:
def __init__(self, width, height):
self.thesun = None
self.planets = []
self.ssturtle = turtle.Turtle()
self.ssturtle.hideturtle()
self.ssscreen = turtle.Screen()
self.ssscreen.setworldcoordinates(-width/2.0,-height/2.0,width/2.0,height/2.0)
def addPlanet(self, aplanet):
self.planets.append(aplanet)
def addSun(self, asun):
self.thesun = asun
def showSun(self):
print(self.thesun)
def showPlanets(self):
for aplanet in self.planets:
print(aplanet)
def freeze(self):
self.ssscreen.exitonclick()
# animation methods
def movePlanets(self):
G = .1
dt = .001
for p in self.planets:
p.moveTo(p.getXPos() + dt * p.getXVel(),
p.getYPos() + dt * p.getYVel())
rx = self.thesun.getXPos() - p.getXPos()
ry = self.thesun.getYPos() - p.getYPos()
r = math.sqrt(rx**2 + ry**2)
accx = G * self.thesun.getMass()*rx/r**3
accy = G * self.thesun.getMass()*ry/r**3
p.setXVel(p.getXVel() + dt * accx)
p.setYVel(p.getYVel() + dt * accy)
私のメインプログラム:
ssInputStrings = []
inputPath = str(input("Please enter the source location for the solar system files: "))
startDate = datetime.datetime.now()
while True:
endDate = datetime.datetime.now()
delta = endDate - startDate
# if the duration has been met, break out of the loop
if delta.seconds > 10:
break
print(delta.seconds)
# initialize switch
addToCollection = True
# read and store the content of each input file in the collection
for file in os.listdir(inputPath):
print(file)
inputFilePath = inputPath + file
inputFile = open(inputFilePath, 'r')
text = inputFile.read()
inputFile.close()
# get the first word from the input file which identifies the solar system object
firstWordFromInputFile = text.split(",")
# if the solar system object has already been stored in the collection,
# do not store it again
for string in ssInputStrings:
firstWordFromInputString = string.split(",")
if firstWordFromInputFile[0] == firstWordFromInputString[0]:
addToCollection = False
break
else:
addToCollection = True
if addToCollection == True:
ssInputStrings.append(text)
# os.remove(inputFilePath)
# pause the thread for one second (necessary otherwise cpu will spike up)
time.sleep(1)
#----------------------------------------------------------
# Instantiate objects and run simulation
#----------------------------------------------------------
from ClassModule import *
def createSSandAnimate():
ss = SolarSystem(2,2)
so = ""
# sun
for string in ssInputStrings:
if string[0:3] == "SUN":
so = string.split(",")
sun = Sun(str(so[0]), int(so[1]), int(so[2]), int(so[3]))
# sun = Sun("SUN", 5000, 10, 5800)
ss.addSun(sun)
for string in ssInputStrings:
if string[0:7] == "MERCURY":
so = string.split(",")
m = Planet(str(so[0]), float(so[1]), int(so[2]), float(so[3]), int(so[4]), int(so[5]),str(so[6]) )
#m = Planet("MERCURY", 19.5, 1000, .25, 0, 2, "blue")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:5] == "EARTH":
so = string.split(",")
m= Planet(str(so[0]), float(so[1]), int(so[2]), float(so[3]), int(so[4]), float(so[5]),str(so[6]))
#m = Planet("EARTH", 47.5, 5000, 0.3, 0, 2.0, "green")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:4] == "MARS":
so = string.split(",")
m=Planet(str(so[0]), int(so[1]), int(so[2]), float(so[3]), int(so[4]), float(so[5]),str(so[6]))
#m = Planet("MARS", 50, 9000, 0.5, 0, 1.63, "red")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:7] == "JUPITER":
so = string.split(",")
m=Planet(str(so[0]), int(so[1]), int(so[2]), float(so[3]), int(so[4]), int(so[5]),str(so[6]))
#m = Planet("JUPITER", 100, 49000, 0.7, 0, 1, "black")
ss.addPlanet(m)
#ss.showSun()
ss.showPlanets()
numTimePeriods = 2000
for amove in range(numTimePeriods):
ss.movePlanets()
ss.freeze()
createSSandAnimate()
ssInputStrings = []
inputPath = str(input("Please enter the source location for the solar system files: "))
startDate = datetime.datetime.now()
while True:
endDate = datetime.datetime.now()
delta = endDate - startDate
# if the duration has been met, break out of the loop
if delta.seconds > 10:
break
print(delta.seconds)
# initialize switch
addToCollection = True
# read and store the content of each input file in the collection
for file in os.listdir(inputPath):
print(file)
inputFilePath = inputPath + file
inputFile = open(inputFilePath, 'r')
text = inputFile.read()
inputFile.close()
# get the first word from the input file which identifies the solar system object
firstWordFromInputFile = text.split(",")
# if the solar system object has already been stored in the collection,
# do not store it again
for string in ssInputStrings:
firstWordFromInputString = string.split(",")
if firstWordFromInputFile[0] == firstWordFromInputString[0]:
addToCollection = False
break
else:
addToCollection = True
if addToCollection == True:
ssInputStrings.append(text)
# os.remove(inputFilePath)
# pause the thread for one second (necessary otherwise cpu will spike up)
time.sleep(1)
#----------------------------------------------------------
# Instantiate objects and run simulation
#----------------------------------------------------------
from ClassModule import *
def createSSandAnimate():
ss = SolarSystem(2,2)
so = ""
# sun
for string in ssInputStrings:
if string[0:3] == "SUN":
so = string.split(",")
sun = Sun(str(so[0]), int(so[1]), int(so[2]), int(so[3]))
# sun = Sun("SUN", 5000, 10, 5800)
ss.addSun(sun)
for string in ssInputStrings:
if string[0:7] == "MERCURY":
so = string.split(",")
m = Planet(str(so[0]), float(so[1]), int(so[2]), float(so[3]), int(so[4]), int(so[5]),str(so[6]) )
#m = Planet("MERCURY", 19.5, 1000, .25, 0, 2, "blue")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:5] == "EARTH":
so = string.split(",")
m= Planet(str(so[0]), float(so[1]), int(so[2]), float(so[3]), int(so[4]), float(so[5]),str(so[6]))
#m = Planet("EARTH", 47.5, 5000, 0.3, 0, 2.0, "green")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:4] == "MARS":
so = string.split(",")
m=Planet(str(so[0]), int(so[1]), int(so[2]), float(so[3]), int(so[4]), float(so[5]),str(so[6]))
#m = Planet("MARS", 50, 9000, 0.5, 0, 1.63, "red")
ss.addPlanet(m)
for string in ssInputStrings:
if string[0:7] == "JUPITER":
so = string.split(",")
m=Planet(str(so[0]), int(so[1]), int(so[2]), float(so[3]), int(so[4]), int(so[5]),str(so[6]))
#m = Planet("JUPITER", 100, 49000, 0.7, 0, 1, "black")
ss.addPlanet(m)
#ss.showSun()
ss.showPlanets()
numTimePeriods = 2000
for amove in range(numTimePeriods):
ss.movePlanets()
ss.freeze()
createSSandAnimate()
This is the link for the link for the error description.
クラスコードを通過してください、私を助けてください。私は1週間からこれを試しています。私はどこに行けなかったのか分からない。
"4つの議論を期待して、5つの"あなたは理解していないのですか? –
エラーメッセージはかなり簡単です。すべてのゲッターとセッターには何がありますか?それらはPythonではまったく必要ありません。 – IanAuld