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

时间:2021-05-14 10:34:36

标签: python 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), 0, 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() ``` 

1 个答案:

答案 0 :(得分:1)

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

angle = 0

# [...]

while True:
    # [...]

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

    # [...]