鱼C论坛

 找回密码
 立即注册
查看: 3023|回复: 6

[作品展示] 太阳地球月亮的公转(Pygame)

[复制链接]
发表于 2017-4-11 17:01:36 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. from pygame.locals import *
  3. import math, random


  4. pygame.init()
  5. screen = pygame.display.set_mode((800, 800))
  6. pygame.display.set_caption("Sun-Earth-Moon")

  7. BLACK = (0, 0, 0)
  8. RED = (255, 0, 0)
  9. BLUE = (0, 0, 255)
  10. YELLOW = (255, 255, 0)
  11. WHITE = (255, 255, 255)
  12. rSun = 120
  13. rEarth = 30
  14. rMoon = 12
  15. angleEarth = 90
  16. angleMoon = 0
  17. clock = pygame.time.Clock()

  18. def getStars():
  19.     return (random.randint(1, 799),random.randint(1, 799),2,2)

  20. done = False
  21. while not done:
  22.     clock.tick(60)
  23.     for event in pygame.event.get():
  24.         if event.type == QUIT:
  25.             done = True

  26.     screen.fill(BLACK)
  27.     # draw stars
  28.     for i in range(100):
  29.         pygame.draw.rect(screen, WHITE, getStars())
  30.     # draw sun.
  31.     pygame.draw.circle(screen, RED, (400, 400), rSun)
  32.     # draw earth.
  33.     earthPos = (400 + int(280*math.sin(math.radians(angleEarth))),\
  34.                 400 + int(280*math.cos(math.radians(angleEarth))))
  35.     pygame.draw.circle(screen, BLUE, earthPos, rEarth)
  36.     angleEarth += 1
  37.     angleEarth %= 360
  38.     # draw moon.
  39.     moonPos = (earthPos[0] + int(80*math.sin(math.radians(angleMoon))),\
  40.                earthPos[1] + int(80*math.cos(math.radians(angleMoon))))
  41.     pygame.draw.circle(screen, YELLOW, moonPos, rMoon)
  42.     angleMoon += 2
  43.     angleMoon %= 360
  44.     pygame.display.flip()
  45.    
  46. pygame.quit()
复制代码
2.png
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-11 17:02:59 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-11 18:41:27 From FishC Mobile | 显示全部楼层
有点意思,但是其实太阳,地球和月球的公转是三维的,而不是平面的,所以不是每次都能三星共线的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-11 19:07:16 | 显示全部楼层
很不错!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-11 22:14:53 From FishC Mobile | 显示全部楼层
如果把月亮的轨迹记录下来,看看是什么图形
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-12 09:06:17 | 显示全部楼层
  1. import pygame
  2. from pygame.locals import *
  3. import math, random


  4. pygame.init()
  5. screen = pygame.display.set_mode((800, 800))
  6. pygame.display.set_caption("Sun-Earth-Moon")

  7. BLACK = (0, 0, 0)
  8. RED = (255, 0, 0)
  9. BLUE = (0, 0, 255)
  10. YELLOW = (255, 255, 0)
  11. WHITE = (255, 255, 255)
  12. rSun = 120
  13. rEarth = 5
  14. rMoon = 5
  15. angleEarth = 90
  16. angleMoon = 0
  17. clock = pygame.time.Clock()

  18. def getStars():
  19.     return (random.randint(1, 799),random.randint(1, 799),2,2)

  20. screen.fill(BLACK)
  21. # draw stars
  22. for i in range(100):
  23.     pygame.draw.rect(screen, WHITE, getStars())
  24. # draw sun.
  25.     pygame.draw.circle(screen, RED, (400, 400), rSun)

  26. done = False
  27. while not done:
  28.     clock.tick(60)
  29.     for event in pygame.event.get():
  30.         if event.type == QUIT:
  31.             done = True
  32.    
  33.     # draw earth.
  34.     earthPos = (400 + int(280*math.sin(math.radians(angleEarth))),\
  35.                 400 + int(280*math.cos(math.radians(angleEarth))))
  36.     pygame.draw.circle(screen, BLUE, earthPos, rEarth)
  37.     angleEarth += 0.1
  38.     angleEarth %= 360
  39.     # draw moon.
  40.     moonPos = (earthPos[0] + int(80*math.sin(math.radians(angleMoon))),\
  41.                earthPos[1] + int(80*math.cos(math.radians(angleMoon))))
  42.     pygame.draw.circle(screen, YELLOW, moonPos, rMoon)
  43.     angleMoon += 3
  44.     angleMoon %= 360
  45.     pygame.display.flip()
  46.    

  47. pygame.quit()
复制代码


把screen.fill()提到外面去,就不会覆盖轨迹了。
把rEarth和rMoon改小写,方便观察轨迹。
1F7.tm.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-30 23:51:51 | 显示全部楼层
厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-6 10:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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