|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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()
复制代码
|
|