|

楼主 |
发表于 2023-7-22 14:45:17
|
显示全部楼层
感谢回答,代码修改如下:
- import pygame
- pygame.init()
- ########## 可更改:引力的大小 ##########
- 引力常数 = 6
- class 天体:
- def __init__(self, 名称, x, y, x速度, y速度, 质量):
- self.名称 = 名称
- self.x = x
- self.y = y
- self.x速度 = x速度
- self.y速度 = y速度
- self.质量 = 质量
- @property
- def 基本信息(self):
- return self.x, self.y, self.x速度, self.y速度, self.质量
- class 天体动画精灵(pygame.sprite.Sprite):
- def __init__(self, 天体, 半径, 颜色):
- pygame.sprite.Sprite.__init__(self)
- self.天体 = 天体
- self.image = pygame.Surface((半径 * 2, 半径 * 2)).convert_alpha()
- self.image.fill((0, 0, 0, 0))
- pygame.draw.circle(self.image, 颜色, (半径, 半径), 半径, 0)
- self.rect = self.image.get_rect()
- self.刷新rect属性()
- def 刷新rect属性(self):
- print(self.天体.x, self.天体.y)
- self.rect.centerx = self.天体.x
- self.rect.centery = self.天体.y
- def 移动全部天体(时间):
- 要删除的天体 = []
- for 天体精灵1 in 天体动画精灵列表:
- 天体1 = 天体精灵1.天体
- if 天体1 in 要删除的天体:
- continue
- x1, y1, x速度1, y速度1, 质量1 = 天体1.基本信息
- x加速度1, y加速度1 = 0, 0
- for 天体精灵2 in 天体动画精灵列表:
- 天体2 = 天体精灵2.天体
- if 天体2 in 要删除的天体:
- continue
- if 天体1 == 天体2:
- continue
- x2, y2, x速度2, y速度2, 质量2 = 天体2.基本信息
- x距离 = x2 - x1
- y距离 = y2 - y1
- 距离 = (x距离 ** 2 + y距离 ** 2) ** 0.5
- if 距离 < 50:
- # 太近了
- if 天体1.质量 > 天体2.质量:
- 要删除的天体.append(天体2)
- else:
- 要删除的天体.append(天体1)
- continue
- 力 = 引力常数 * 质量1 * 质量2 / (距离 ** 2)
- 加速度 = 力 / 质量1
- x加速度1 += 加速度 * (x距离 / 距离)
- y加速度1 += 加速度 * (y距离 / 距离)
- x, y = (
- x1 + x速度1 * 时间 + 0.5 * x加速度1 * (时间 ** 2),
- y1 + y速度1 * 时间 + 0.5 * y加速度1 * (时间 ** 2)
- )
- 临时x, 临时y = 天体1.x, 天体1.y
- 天体1.x, 天体1.y = x, y
- 天体1.x速度 = (x - 临时x) / 时间
- 天体1.y速度 = (y - 临时x) / 时间
- 天体精灵1.刷新rect属性()
- for 动画精灵 in 天体动画精灵列表:
- if 动画精灵.天体 in 要删除的天体:
- 天体动画精灵列表.remove(动画精灵)
- screen = pygame.display.set_mode((900, 900))
- 天体动画精灵列表 = [
- 天体动画精灵(天体("planet1", 500, 100, 1, 0, 1e4), 50, "orange"),
- 天体动画精灵(天体("planet2", 500, 900, -1, 0, 1e4), 50, "blue")
- ]
- clock = pygame.time.Clock()
- running = True
- while running:
- screen.fill((0, 0, 0))
- clock.tick(60)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- ########## 可更改:天体移动速度 ##########
- 移动全部天体(1)
- for 天体动画精灵 in 天体动画精灵列表:
- screen.blit(天体动画精灵.image, 天体动画精灵.rect)
- pygame.display.flip()
复制代码
为什么:
- F:\Python399\python.exe E:\Python123\k\main.py
- pygame 2.5.0 (SDL 2.28.0, Python 3.9.9)
- Hello from the pygame community. https://www.pygame.org/contribute.html
- 500 100
- 500 900
- 501.0 100.046875
- 499.00005860391354 899.9531196162419
- 501.99988277582344 -299.85936445073924
- 498.00016931204163 1299.8853995820643
- 502.99973624312133 -1100.7070065216924
- 497.00029086182343 2100.765534831006
- 503.9995842253675 -2703.4109688415697
- 496.0004143053315 3703.5296005337195
- 504.9994312951553 -5909.820943092084
- 495.0005380527121 6910.058585588716
- 505.9992782225723 -12323.641287871671
- 494.0006618464668 13324.116675776857
- 506.9991251286537 -25152.281961432465
- 493.0007856470674 26153.232793236668
- 507.9989720316255 -50810.56318969043
- 492.00090944865497 51812.46491956223
- 508.99881893415323 -102128.12550166092
- 491.0010332503823 103131.92905221145
- 509.99866583661856 -204764.24997464143
- 490.0011570521292 205771.8571946578
- 510.9985127390752 -410037.49876803905
- 489.00128085387877 411052.7133559861
- 511.9983596415306 -820584.9962018703
- 488.0014046556287 821615.4255549003
- 512.9982065439859 -1641680.9909164684
- 487.00152845737864 1642741.849828942
- 513.9980534464412 -3283873.9801925756
- 486.0016522591286 3284995.698253227
- 514.9979003488965 -6568260.958591695
- 485.00177606087857 6569504.394977996
- 515.9977472513517 -13137035.915236836
- 484.00189986262853 13138522.788303733
- 516.997594153807 -26274586.82837402
- 483.0020236643785 26276560.574831404
- 517.9974410562622 -52549689.65449529
- 482.00214746612846 52552637.14776295
- 518.9972879587175 -105099896.30658475
- 481.0022712678784 105104791.29350223
- 519.9971348611728 -210200310.61061054
- 480.0023950696284 210209100.584857
- 520.996981763628 -420401140.2185091
- 479.00251887137836 420417720.1674427
- 521.9968286660833 -840802800.4341531
- 478.0026426731283 840834960.3324902
- 522.9966755685385 -1681606121.8652878
- 477.0027664748783 1681669441.6624615
- 523.9965224709938 -3363212765.727404
- Traceback (most recent call last):
- File "E:\Python123\k\main.py", line 93, in <module>
- 移动全部天体(1)
- File "E:\Python123\k\main.py", line 72, in 移动全部天体
- 天体精灵1.刷新rect属性()
- File "E:\Python123\k\main.py", line 33, in 刷新rect属性
- self.rect.centery = self.天体.y
- TypeError: invalid rect assignment, expected value between -2147483648 < x < 2147483647
- 进程已结束,退出代码1
复制代码 |
|