当然可以!下面是一个简单的例子,展示了太阳系八大行星的运行:

import pygame
import math

# 初始化pygame
pygame.init()

# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("太阳系八大行星")

# 加载行星图片
sun_image = pygame.image.load("sun.png")
mercury_image = pygame.image.load("mercury.png")
venus_image = pygame.image.load("venus.png")
earth_image = pygame.image.load("earth.png")
mars_image = pygame.image.load("mars.png")
jupiter_image = pygame.image.load("jupiter.png")
saturn_image = pygame.image.load("saturn.png")
uranus_image = pygame.image.load("uranus.png")
neptune_image = pygame.image.load("neptune.png")

# 定义行星参数
sun_radius = 50
mercury_radius = 25
venus_radius = 35
earth_radius = 40
mars_radius = 30
jupiter_radius = 60
saturn_radius = 55
uranus_radius = 45
neptune_radius = 50

# 定义行星轨道半径
mercury_orbit_radius = 100
venus_orbit_radius = 150
earth_orbit_radius = 200
mars_orbit_radius = 250
jupiter_orbit_radius = 350
saturn_orbit_radius = 450
uranus_orbit_radius = 550
neptune_orbit_radius = 650

# 定义行星速度
mercury_speed = 0.03
venus_speed = 0.02
earth_speed = 0.015
mars_speed = 0.01
jupiter_speed = 0.008
saturn_speed = 0.006
uranus_speed = 0.004
neptune_speed = 0.003

# 定义初始角度
mercury_angle = 0
venus_angle = 0
earth_angle = 0
mars_angle = 0
jupiter_angle = 0
saturn_angle = 0
uranus_angle = 0
neptune_angle = 0

# 游戏主循环
running = True
while running:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 清空屏幕
    screen.fill((0, 0, 0))

    # 绘制太阳
    sun_rect = sun_image.get_rect()
    sun_rect.center = (400, 400)
    screen.blit(sun_image, sun_rect)

    # 计算行星位置
    mercury_x = 400 + mercury_orbit_radius * math.cos(mercury_angle)
    mercury_y = 400 + mercury_orbit_radius * math.sin(mercury_angle)
    venus_x = 400 + venus_orbit_radius * math.cos(venus_angle)
    venus_y = 400 + venus_orbit_radius * math.sin(venus_angle)
    earth_x = 400 + earth_orbit_radius * math.cos(earth_angle)
    earth_y = 400 + earth_orbit_radius * math.sin(earth_angle)
    mars_x = 400 + mars_orbit_radius * math.cos(mars_angle)
    mars_y = 400 + mars_orbit_radius * math.sin(mars_angle)
    jupiter_x = 400 + jupiter_orbit_radius * math.cos(jupiter_angle)
    jupiter_y = 400 + jupiter_orbit_radius * math.sin(jupiter_angle)
    saturn_x = 400 + saturn_orbit_radius * math.cos(saturn_angle)
    saturn_y = 400 + saturn_orbit_radius * math.sin(saturn_angle)
    uranus_x = 400 + uranus_orbit_radius * math.cos(uranus_angle)
    uranus_y = 400 + uranus_orbit_radius * math.sin(uranus_angle)
    neptune_x = 400 + neptune_orbit_radius * math.cos(neptune_angle)
    neptune_y = 400 + neptune_orbit_radius * math.sin(neptune_angle)

    # 绘制行星
    mercury_rect = mercury_image.get_rect()
    mercury_rect.center = (mercury_x, mercury_y)
    screen.blit(mercury_image, mercury_rect)
    venus_rect = venus_image.get_rect()
    venus_rect.center = (venus_x, venus_y)
    screen.blit(venus_image, venus_rect)
    earth_rect = earth_image.get_rect()
    earth_rect.center = (earth_x, earth_y)
    screen.blit(earth_image, earth_rect)
    mars_rect = mars_image.get_rect()
    mars_rect.center = (mars_x, mars_y)
    screen.blit(mars_image, mars_rect)
    jupiter_rect = jupiter_image.get_rect()
    jupiter_rect.center = (jupiter_x, jupiter_y)
    screen.blit(jupiter_image, jupiter_rect)
    saturn_rect = saturn_image.get_rect()
    saturn_rect.center = (saturn_x, saturn_y)
    screen.blit(saturn_image, saturn_rect)
    uranus_rect = uranus_image.get_rect()
    uranus_rect.center = (uranus_x, uranus_y)
    screen.blit(uranus_image, uranus_rect)
    neptune_rect = neptune_image.get_rect()
    neptune_rect.center = (neptune_x, neptune_y)
    screen.blit(neptune_image, neptune_rect)

    # 更新角度
    mercury_angle += mercury_speed
    venus_angle += venus_speed
    earth_angle += earth_speed
    mars_angle += mars_speed
    jupiter_angle += jupiter_speed
    saturn_angle += saturn_speed
    uranus_angle += uranus_speed
    neptune_angle += neptune_speed

    # 更新屏幕
    pygame.display.flip()

# 退出游戏
pygame.quit()

请注意,上述代码仅提供了一个简单的示例,你需要自己准备太阳和行星的图像文件,并将其命名为sun.pngmercury.pngvenus.pngearth.pngmars.pngjupiter.pngsaturn.pnguranus.pngneptune.png,并将其与代码文件放在同一目录中。

此代码使用了pygame库来创建游戏窗口和绘制图像。行星的位置通过计算每个行星在其轨道上的位置来确定。每个行星都有自己的半径、轨道半径和速度。程序通过更新行星的角度来模拟行星的运动,然后在每次循环中重新绘制行星的位置。

希望这个例子能够帮助到你

你能编写一个虚拟太阳系八大行星的运行吗

原文地址: https://www.cveoy.top/t/topic/hyh9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录