class Planet:
def __init__(self, name, radius, colour, speed, distance, mass, angle, orbitcentrex,
orbitcentrey):
self.x = None
self.y = None
self._name = name
self._radius = radius
self._colour = colour
self._speed = speed
self._distance = distance
self._angle = angle
self._centreX = orbitcentrex
self._centreY = orbitcentrey
def updateSpeed(*planets):
planets._speed = (0.2*(planets._speed))
Sun = Planet("Sun", 50, SunColour, 0, 0, 0, 0, size[0] / 2, size[1] / 2)
Mercury = Planet("Mercury", 4, MercuryColour, 0.00477, 70, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Venus = Planet("Sun", 5.5, VenusColour, 0.00354, 125, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Earth = Planet("Earth", 6, EarthColour, 0.003, 180, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Mars = Planet("Mars", 4, MarsColour, 0.002424, 235, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Jupiter = Planet("Jupiter", 17, JupiterColour, 0.001317, 290, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Saturn = Planet("Saturn", 12, SaturnColour, 0.000975, 345, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Uranus = Planet("Uranus", 10, UranusColour, 0.000684, 400, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Neptune = Planet("Neptune", 10, NeptuneColour, 0.000546, 455, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
Pluto = Planet("Pluto", 4, PlutoColour, 0.000471, 510, 0, random.uniform(0, 6.2832), size[0] / 2, size[1] / 2)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and 30 < Mouse[0] < 195 and 155 < Mouse[1] < 220:
Planet.updateSpeed(Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto)
I'm trying to use *args to pass in all of my planets as arguments when running the updateSpeed method. However it can't do this because the *args creates a tuple of the planet names. Is there any way to get arround this so I don't have to write individual lines to change the speed of each planet. What I had before was this:
if event.type == pygame.MOUSEBUTTONDOWN and 30 < Mouse[0] < 195 and 250 < Mouse[1] < 315:
click.play()
Mercury._speed = 0.002862
Venus._speed = 0.002124
Earth._speed = 0.0018
Mars._speed = 0.0014544
Jupiter._speed = 0.0007902
Saturn._speed = 0.0005850
Uranus._speed = 0.0004104
Neptune._speed = 0.0003276
Pluto._speed = 0.0002826
What I want to have is this:
if event.type == pygame.MOUSEBUTTONDOWN and 30 < Mouse[0] < 195 and 155 < Mouse[1] < 220:
Planet.updateSpeed(Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto)
CodePudding user response:
you can make a list of the object you want to apply the method and in a for loop apply said method
class Planet:
def __init__(self,...):
... #same as yours
def updateSpeed(self):
self._speed *= 0.2
...
Planets = [Mercury, Venus,...]
for p in Planets:
p.updateSpeed()
CodePudding user response:
Use a for
loop to loop through each planet and update its speed.
def updateSpeed(*planets):
for planet in planets:
planet._speed = (0.2 * (planet._speed))