微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pygame:在圆形路径中围绕另一个图像移动图像

如何解决Pygame:在圆形路径中围绕另一个图像移动图像

我知道这里已经有一些解决方案,但不幸的是它们没有帮助我解决我的问题。我想以圆形路径围绕行星图像移动卫星图像。我知道如何从左到右沿直线移动它,但我不知道如何制作圆圈。这是我目前的代码

import pygame
from pygame.locals import *
from sys import exit
from math import sin
from math import cos


player_image = 'dragon-500px.png'


pygame.init()

screen = pygame.display.set_mode((1080,720),32)
pygame.display.set_caption("Animate X!")
mouse_cursor = pygame.image.load(player_image).convert_alpha()
image_earth = pygame.image.load('Erde.jpg').convert()
image_earth = pygame.transform.scale(image_earth,(300,300))
image_earth_rect = image_earth.get_rect()
image_earth_rect.center = (540,360)

radius = 100
center = (540,360)

direction = pygame.math.Vector2(1,0)





clock = pygame.time.Clock()

speed = 300.0

x = 0 - mouse_cursor.get_width()

y = 10
while True:
    time_passed = clock.tick() / 1000.0
    moved_distance = time_passed * speed
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    direction.rotate_ip(4)
    direction.normalize_ip()
    Satellite_pos = [int(i) for i in center + direction*radius]
    
    screen.fill((255,255,255))
    if x > screen.get_width():
        x = 0 - mouse_cursor.get_width()
    elif y > screen.get_height():
        y = 10
    screen.blit(mouse_cursor,(x,y))
    screen.blit(image_earth,image_earth_rect)
    
    x+=moved_distance
    
    y+= moved_distance
  
  
    pygame.display.update() ``` 

解决方法

根据角度计算卫星的位置:

angle = 0

# [...]

while True:
    # [...]

    center = image_earth_rect.center
    Satellite_pos = [center[0] + radius * cos(angle),center[1] + radius * sin(angle)]
    angle += 0.01

    # [...]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。