본문 바로가기

파이썬(PYTHON)

물분자 운동 simulation

728x90

pygame를 활용하여 수소와 산소로 이루어진 물분자의 움직임을 표현해 보았음.

import pygame
import time, random
import numpy as np

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

done = False
clock = pygame.time.Clock()
myFont1 = pygame.font.Font(None, 50)
myFont2 = pygame.font.Font(None, 50)

RAD1 = 10 #60
RAD2 = 5 #25
pos_x = []
pos_y = []
A1 = []
for i in range(10):
    pos_x.append(320)
    pos_y.append(200)
    A1.append(18)

ang = 104.5
while not done:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    screen.fill('#000000')
    for i in range(10):
        A2 = A1[i]+ang
        pygame.draw.circle(screen, '#FF0000', [pos_x[i], pos_y[i]], RAD1, 0)
        pygame.draw.circle(screen, '#00FF00', [pos_x[i]+(RAD1+RAD2)*np.sin(np.pi*A1[i]/90), pos_y[i]+(RAD1+RAD2)*np.cos(np.pi*A1[i]/90)], RAD2, 0)
        pygame.draw.circle(screen, '#00FF00', [pos_x[i]+(RAD1+RAD2)*np.sin(np.pi*A2/90), pos_y[i]+(RAD1+RAD2)*np.cos(np.pi*A2/90)], RAD2, 0)
       
        if random.randint(0, 1) > 0.5:
            pos_x[i] += 1
        else:
            pos_x[i] -= 1

        if random.randint(0, 1) > 0.5:
            pos_y[i] += 1
        else:
            pos_y[i] -= 1

        if random.randint(0, 1) > 0.5:
            A1[i] += 3
        else:
            A1[i] -= 3

       
    pygame.display.update()


728x90