import pygame
import sys
import numpy as np
class Solar():
def __init__(self):
pygame.init()
self.WINDOW_WIDTH = 800
self.WINDOW_HEIGHT = 400
self.RED = (255, 0, 0)
self.WHITE = (255, 255, 255)
self.BLACK = (0, 0, 0)
self.BLUE = (0,0,255)
self.GREEN = (0,255,0)
self.windows = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT))
pygame.display.set_caption('Solar simulation')
self.init_figure()
#初始化函数
def init_figure(self):
self.circle = pygame.image.load('circle_12_red.bmp')
self.circle_rect = self.circle.get_rect()
self.circle_rect.centerx = self.windows.get_rect().centerx
self.circle_rect.centery = self.windows.get_rect().centery
self.a = 300
self.b = 150
#椭圆路径
def ellipse_path(self,x,flag):
self.circle_rect.centerx = 400 + x
self.circle_rect.centery = 200 - flag*((1-(x/self.a)**2)*(self.b**2))**0.5
#循环内容
def function(self,x,flag):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.windows.fill(self.BLACK)
pygame.draw.ellipse(self.windows, self.GREEN, (100, 50, 600, 300), 3)
self.windows.blit(self.circle, self.circle_rect)
pygame.display.flip()
self.ellipse_path(x,flag)
pygame.time.delay(5)
#主控制
def start_simulate(self):
while True:
#0-180度
for x in np.arange(300,-300,-2):
self.function(x,1)
#180-360度
for x in np.arange(-300,300,2):
self.function(x,-1)
Solar().start_simulate()
写出来就是这个样子