-1

I'm working through an example and cannot see the solution.

Can someone please point me in right direction? Tried lots of stuff.

File "C:\Python prog\orbit.py", line 57, in main
    for planets in Planet:
TypeError: 'type' object is not iterable

Code:

import pygame
import math
pygame.init
WIDTH,HEIGHT= 800,800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Planet simulation")

WHITE = (255,255,255)
YELLOW = (255,255,0)
BLUE = (100, 149, 237)
RED = (188, 39, 50)
DARK_GREY = (80, 78, 81)
#FONT = pygame.font.SysFont("comicsans", 16)

class Planet:
    AU = 149.6e6 * 1000
    G = 6.67428e-11
    SCALE = 250 / AU
    TIMESTEP = 3600*24

    def __init__(self, x, y, radius, color, mass):
            self.x = x
            self.y = y
            self.radius = radius      
            self.color = color
            self.mass = mass
        
            self.orbit = []
            self.sun = False
            self.distance_to_sun = 0

            self.x_vel = 0
            self.y_vel = 0

    def draw(self, win):
            x=self.x * self.SCALE + WIDTH /2
            y=self.y * self.SCALE + HEIGHT /2
            pygame.draw.circle(win, self.color, (x, y), self.radius)
        
          
def main():
    run=True
    clock = pygame.time.Clock()

    sun = Planet(0,0,30,YELLOW, 1.98892 * 10**30)
    earth = Planet(-1 * Planet.AU, 0, 16, BLUE, 5.9742 * 10**24)
    sun.sun = True
    planets = [sun, earth]
    
    while run:
        clock.tick(60)
        WIN.fill((0, 0, 0))

        for event in pygame.event.get():
            if event.type ==pygame.QUIT:
                    run = False

        for planets in Planet:
                
                planet.draw(WIN)
                #planet.Update_position(planets)
        pygame.display.update()
    pygame.QUIT()

main()

checked class and variable names spellings etc

1
  • 4
    Should be for planet in planets:.
    – luk2302
    Commented Apr 17 at 17:05

1 Answer 1

1

You need to iterate over the list

for planet in planets:
    planet.draw(WIN)
1
  • 1
    I cannot believe I missed that thank you all very much! Commented Apr 17 at 20:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.