鱼C论坛

 找回密码
 立即注册
查看: 1861|回复: 1

[作品展示] pygame编写的万有引力效果演示

[复制链接]
发表于 2021-11-1 21:14:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pygame编写的万有引力效果演示

GMm_mian.py
import pygame
from GMm_ import *
pygame.init()

m1 = [1, 700, 300, 0, 0]
m2 = [1e14, 500, 300, 0, 0]
m3 = [1, 300, 300, 0, 0]
m1[4] = speed_v(m2[0], m1[1]-m2[1], m1[2]-m2[2])
m3[4] = -speed_v(m2[0], m3[1]-m2[1], m3[2]-m2[2])

window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("star")

zhong = star(m1)
zhong1 = star(m2)
diqiou = star(m3)
stars_ = stars()
stars_.add(zhong)
stars_.add(zhong1)
stars_.add(diqiou)

window.fill((255, 255, 255))
FPS = 60
clock = pygame.time.Clock()
CREATE_ENEMY_EVENT = pygame.USEREVENT
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)


while True:

    clock.tick(FPS)

    pygame.draw.circle(window, (211, 211, 211), (zhong.x, zhong.y), 10)
    pygame.draw.circle(window, (0, 0, 0), (zhong.x, zhong.y), 5)
    pygame.draw.circle(window, (211, 211, 211), (zhong1.x, zhong1.y), 10)
    pygame.draw.circle(window, (0, 0, 0), (zhong1.x, zhong1.y), 5)
    pygame.draw.circle(window, (211, 211, 211), (diqiou.x, diqiou.y), 10)
    pygame.draw.circle(window, (0, 0, 0), (diqiou.x, diqiou.y), 5)

    pygame.display.update()

    pygame.draw.circle(window, (211, 211, 211), (zhong.x, zhong.y), 5)
    pygame.draw.circle(window, (211, 211, 211), (zhong1.x, zhong1.y), 5)
    pygame.draw.circle(window, (211, 211, 211), (diqiou.x, diqiou.y), 5)

    stars_.gravitation(zhong)
    stars_.gravitation(zhong1)
    stars_.gravitation(diqiou)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            exit()

        elif event.type == CREATE_ENEMY_EVENT:
            print(zhong.speed_x, zhong.speed_y, zhong1.speed_x, zhong1.speed_y, diqiou.speed_x, diqiou.speed_y)
GMm_.py
G = 6.67e-11
WIN_WIDTH = 1000
WIN_HEIGHT = 600


def speed_v(M, x, y):
    return (G * M / ((x ** 2 + y ** 2) ** 0.5)) ** 0.5


class star:
    def __init__(self, li):
        self.m = li[0]
        self.x = li[1]
        self.y = li[2]
        self.speed_x = li[3]
        self.speed_y = li[4]

    def motion(self):
        self.x += self.speed_x
        self.y += self.speed_y

    def acceleration(self, q_x, q_y):
        self.speed_x += q_x
        self.speed_y += q_y


class stars:
    def __init__(self):
        self.li = []

    def add(self, star_):
        self.li.append(star_)

    def gravitation(self, star_):
        a = 0
        b = 0
        for i in self.li:
            if i == star_:
                continue
            else:
                x = i.x - star_.x
                y = i.y - star_.y
                r = (x ** 2 + y ** 2) ** 0.5
                q = G * i.m / r ** 2
                a += x / r * q
                b += y / r * q
        star_.acceleration(a, b)
        star_.motion()

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-2 09:18:10 | 显示全部楼层
牛蛙,哈哈,坐等更新三体
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 22:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表