from pygame import Color import sys import pygame import random class DrawGroup(pygame.sprite.Group): def draw(self): sprites = self.sprites() for sprite in sprites: sprite.draw() class CelestialObject(pygame.sprite.Sprite): def __init__(self, screen, color,xloc, yloc, radius, mass, orbit_rect): pygame.sprite.Sprite.__init__(self) self.screen = screen self.color=color self.xloc = xloc self.yloc = yloc self.radius = radius self.mass = mass self.orbit_rect = orbit_rect self.target = None self.toggle_show_orbit_path = False def setTarget(self, orbitable): self.target = orbitable def showOrbitPath(self): self.toggle_show_orbit_path = True def hideOrbitPath(self): self.toggle_show_orbit_path = False def update(self, time_passed): if self.target != None: pass def draw(self): pygame.draw.circle(self.screen, self.color, (self.xloc,self.yloc), self.radius, 0) if self.toggle_show_orbit_path == True: #pygame.draw.rect(self.screen, self.color, self.orbit_rect, 1) pygame.draw.ellipse(self.screen, self.color, self.orbit_rect, 1) if __name__ == "__main__": SCREEN_AREA = (700, 500) pygame.init() screen = pygame.display.set_mode(SCREEN_AREA, pygame.HWSURFACE) #pygame.FULLSCREEN pygame.HWSURFACE pygame.display.set_caption('Running CelestialObject Example, escape to close') earth = CelestialObject(screen=screen, color=Color('blue'),xloc=300, yloc=200, radius=63, mass=59, orbit_rect = pygame.Rect(-200,-200,1000,400)) moon = CelestialObject(screen=screen, color=Color('gray'),xloc=500, yloc=200, radius=17, mass=1.1, orbit_rect = pygame.Rect(155,100,355,290)) moon.setTarget(earth) moon.showOrbitPath() celestial_group = DrawGroup((earth,moon)) clock = pygame.time.Clock() while 1: time_passed = clock.tick(22) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: sys.exit(0) celestial_group.update(time_passed) celestial_group.draw() pygame.display.flip()