|
楼主 |
发表于 2017-4-12 09:06:17
|
显示全部楼层
- import pygame
- from pygame.locals import *
- import math, random
- pygame.init()
- screen = pygame.display.set_mode((800, 800))
- pygame.display.set_caption("Sun-Earth-Moon")
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- BLUE = (0, 0, 255)
- YELLOW = (255, 255, 0)
- WHITE = (255, 255, 255)
- rSun = 120
- rEarth = 5
- rMoon = 5
- angleEarth = 90
- angleMoon = 0
- clock = pygame.time.Clock()
- def getStars():
- return (random.randint(1, 799),random.randint(1, 799),2,2)
- screen.fill(BLACK)
- # draw stars
- for i in range(100):
- pygame.draw.rect(screen, WHITE, getStars())
- # draw sun.
- pygame.draw.circle(screen, RED, (400, 400), rSun)
- done = False
- while not done:
- clock.tick(60)
- for event in pygame.event.get():
- if event.type == QUIT:
- done = True
-
- # draw earth.
- earthPos = (400 + int(280*math.sin(math.radians(angleEarth))),\
- 400 + int(280*math.cos(math.radians(angleEarth))))
- pygame.draw.circle(screen, BLUE, earthPos, rEarth)
- angleEarth += 0.1
- angleEarth %= 360
- # draw moon.
- moonPos = (earthPos[0] + int(80*math.sin(math.radians(angleMoon))),\
- earthPos[1] + int(80*math.cos(math.radians(angleMoon))))
- pygame.draw.circle(screen, YELLOW, moonPos, rMoon)
- angleMoon += 3
- angleMoon %= 360
- pygame.display.flip()
-
- pygame.quit()
复制代码
把screen.fill()提到外面去,就不会覆盖轨迹了。
把rEarth和rMoon改小写,方便观察轨迹。
|
-
|