본문 바로가기
프로그램

[파이썬] 문제 : pygame 아모도 적색편이(?) 시뮬레이션 코드 분석

by 오디세이99 2022. 11. 23.
728x90
반응형
#포그슨 방정식
import pygame, sys, math, time #추가명령어 불러오기


pygame.init() #파이게임 모듈 리셋
screen = pygame.display.set_mode((1280, 800)) #화면 설정 가로X세로
pygame.display.set_caption("Pogson's Formula Simulator") #창 이름
font = pygame.font.Font(None,20) #글씨체 생성

class Star:
    def __init__(self, absol): #기본정보 입력
        self.absol = absol # 절대등급 할당
        self.x = 70 #초기 위치

    def cal(self):
        app = self.absol+(5*math.log10((self.x-40)*1/60))-5 # 포그슨방정식으로 거리에 따른 겉보기 등급 계산
        self.app = app #계산값 할당

    def move(self): # 움직임 명령어
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                self.x = self.x + 6 # 10pc(거리단위)을 600픽셀로 잡았기에 6픽셀씩 이동
            if event.key == pygame.K_LEFT:
                self.x = self.x - 6

    def display(self): #문자 띄우기 문법 (문자열, True, (색정보), (위치))
        screen.blit(font.render("Absolute Magnitude:",True,(0,255,0)),(40,500))
        screen.blit(font.render("Apparent Magnitude:",True,(0,255,0)),(40,520))
        screen.blit(font.render("Distance(pc):",True,(0,255,0)),(40,540))
        screen.blit(font.render(str(self.absol),True,(255,255,255)),(175,500))
        screen.blit(font.render(str(self.app),True,(255,255,255)),(175,520))
        screen.blit(font.render(str((self.x-40)*1/60),True,(255,255,255)),(125,540))

        #마지막 줄 - 60픽셀이 1pc이기 때문에 60으로 나눔


star = Star(5) # 클래스에 대한 개채 생성

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill((0, 0, 0))
    star.move()
    star.cal()
    star.display()

    pygame.draw.circle(screen, (0,190,125), (40, 400), 600, 2)
    pygame.draw.circle(screen, (0,0,255), (40, 400), 7)
    pygame.draw.circle(screen, (255-0.3*(star.x-40),0,0), (star.x, 400), (20))

    #멀어질수록 밝기가 낮아지기 때문에 희미해지게
    time.sleep(0.1)
    pygame.display.update()
728x90
반응형

댓글