본문 바로가기
프로그램

[파이썬] ChatGPT로 만든 태양계 시뮬레이션(pygame)

by 오디세이99 2023. 3. 18.
728x90
반응형
import pygame
import math

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (0, 255, 255)
GREY = (203, 203, 203)
ORANGE = (255, 153, 51)
TAN = (210, 180, 140)
LIGHTBROWN = (255,99,71)
LIGHTBLUE = (0,191,255)
DARKBLUE = (0,0,139)


# Set the width and height of the screen [width, height]
WIDTH = 1200
HEIGHT = 1200
size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
pygame.font.init()
font = pygame.font.SysFont("malgungothic", 20)

# Set title of screen
pygame.display.set_caption("Solar System")

# Define classes
class Planet:
    def __init__(self, name, distance, radius, color, speed, angle=0):
        self.name = name
        self.distance = distance
        self.radius = radius
        self.color = color
        self.speed = speed
        self.angle = angle

    def draw(self, center_x, center_y):
        # Calculate position of planet
        x = self.distance * math.sin(math.radians(self.angle))
        y = self.distance * math.cos(math.radians(self.angle))

        # Draw planet
        pygame.draw.circle(screen, self.color, (int(x) + center_x, int(y) + center_y), self.radius)
        
        # draw name
        name_text = font.render(self.name, True, WHITE)
        screen.blit(name_text, (int(x) + center_x, int(y) + center_y))        

        # Update angle
        self.angle += self.speed

# Initialize planets
sun = Planet("sun", 0, 30, RED, 0)
mercury = Planet("mercury", 70, 5, GREY, 4)
venus = Planet("venus", 110, 10, ORANGE, 3)
earth = Planet("earth", 150, 12, BLUE, 2)
mars = Planet("mars", 200, 8, LIGHTBROWN, 1)
jupiter = Planet("jupiter", 300, 25, YELLOW, 0.5)
saturn = Planet("saturn", 400, 20, TAN, 0.3)
uranus = Planet("uranus", 500, 15, LIGHTBLUE, 0.2)
neptune = Planet("neptune", 600, 14, DARKBLUE, 0.1)

# Set up game loop
done = False

# Set up clock for timing
clock = pygame.time.Clock()

# Loop until the user clicks the close button or press Ctrl + C in console
while not done:
    # --- Event Processing ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Drawing ---
    # Set background color
    screen.fill(BLACK)

    # Draw sun
    sun.draw(WIDTH//2, HEIGHT//2)

    # Draw planets
    mercury.draw(WIDTH//2, HEIGHT//2)
    venus.draw(WIDTH//2, HEIGHT//2)
    earth.draw(WIDTH//2, HEIGHT//2)
    mars.draw(WIDTH//2, HEIGHT//2)
    jupiter.draw(WIDTH//2, HEIGHT//2)
    saturn.draw(WIDTH//2, HEIGHT//2)
    uranus.draw(WIDTH//2, HEIGHT//2)
    neptune.draw(WIDTH//2, HEIGHT//2)

    # --- Update the screen
    pygame.display.update()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
pygame.quit()

728x90
반응형

댓글