본문 바로가기
프로그램

[파이썬] 문제 : pygame 행성 타원운동 시뮬레이션 코드 분석

by 오디세이99 2022. 11. 23.
728x90
반응형
import math, sys, pygame, time                # 이 코드에서 사용하는 package(파이썬의 Library)들을 선언 합니다.

pygame.init()                                  # 화면을 그리기 위해서 pygame을 초기화 합니다.
screen = pygame.display.set_mode((1280, 800))  # 화면 크기를 설정
font = pygame.font.Font(None,20)               # 글자(Font)를 설정


class Planet:                                  # class는 클래스라고 해서 설계도 라고 보시면 됩니다. 나중에 이를 실제 프로그램에서 실행하는 인스턴스로 선언해서 사용합니다.(하단에 있음)
    def __init__(self, r, m,v):                # 클래스의 초기화. 인스턴스화 할때 반드시 실행 됨
        self.r = r
        self.m = m
        self.v = v
        self.v_x = 0
        self.v_y = -v
        self.x = 640 + r
        self.y = 400
        self.acc = self.m/(r**2)
        self.dis=0
        self.major=0

    def cal(self):                             # 클래스의 call 함수(=Method)를 만듭니다.
        self.dis=(((640-self.x)**2 + (self.y-400)**2)**0.5)
        self.sin = (400-self.y)/self.dis
        self.cos = -(self.x - 640)/self.dis
        self.acc = self.m/self.dis**2
        self.acc_x = (self.cos * self.acc)
        self.acc_y = (self.sin * self.acc)

    def move(self):                             # 클래스의 move 함수(=Method)를 만듭니다.
        self.v_x = self.v_x + self.acc_x
        self.v_y = self.v_y + self.acc_y
        self.x = self.x + self.v_x
        self.y = self.y + self.v_y

pla = Planet(120, 2700,5.8725)        # 위에서 선언해둔 Planet 이라는 클래스(설계도)를 인스턴스(동작하는)로 만듭니다.
r = 0.01
k=0
time1=0
tume2=0
t=0

screen.fill((0, 0, 0))                    # 화면을 검은색으로
while 1:                                  # 무한 반복
    for event in pygame.event.get():      # 키보드 등의 행동(Event)를 받습니다
        if event.type == pygame.QUIT:     # 종료하면
            sys.exit()                    # 프로그램 종료
        if event.type == pygame.KEYDOWN:  # 키보드의 클릭하면
            if event.key == pygame.K_UP:  # 키보드의 업(↑) 키를 클릭하면
                r = r * 1.1
            if event.key == pygame.K_DOWN:  # 키보드의 다운(↓) 키를 클릭하면
                r = r * 0.9

    screen.fill((0, 0, 0))
    pla.cal()                            # 위에서 선언한 클래스(Planet과 인스턴스인 pla의 cal() 함수를 실행합니다.
    pla.move()                           # 위에서 선언한 클래스(Planet과 인스턴스인 pla의 move() 함수를 실행합니다.

    pygame.draw.circle(screen, (255,0,0), (640, 400), 20)           # 원을 그립니다.
    pygame.draw.circle(screen, (0,125,175), ((pla.x), (pla.y)), 5)  # 

    screen.blit(font.render("Time Delay :",True,(0,255,0)),(40,40))    # 화면에 문장을 씁니다
    screen.blit(font.render(str(round(r,5)),True,(0,255,0)),(120,40))  # 화면에 문장을 씁니다

    if ((pla.y <= 403) and (pla.y >= 397 )):
        k = k+1
    if(k<10):    # k 값이 10보다 작으면
        if pla.dis > pla.major:
            pla.major = pla.dis
            if k%2==1:
                time1 = time.time()
            if k%2==0:
                time2 = time.time()
            t = time2 - time1

    if(k>10):    # k 값이 10보다 크면
        screen.blit(font.render("pla.major :",True,(0,255,0)),(40,80))     # 화면에 문장을 씁니다(pla.major)
        screen.blit(font.render(str(pla.major),True,(0,255,0)),(110,80))   # 화면에 문장을 씁니다
        a = (2*t)**2/(pla.major**3)
        screen.blit(font.render("T^2/r^3 :",True,(0,255,0)),(40,60))
        screen.blit(font.render(str(a),True,(0,255,0)),(100,60))

        time.sleep(r)              # r초 동안 멈춥니다

        pygame.display.update()    # 화면을 갱신 합니다.

728x90
반응형

댓글