Reputation: 43
I know that there are already some solutions here but unfortunately they did not help me with my problem. I want to move a satellite image around a planet image in a circular path. I know how to move it in a line from left to right but I don't get it how to make the circle. This is my code so far
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() ```
Upvotes: 1
Views: 329
Reputation: 211278
Compute the position of the satellite dependent on an angle:
angle = 0
# [...]
while True:
# [...]
center = image_earth_rect.center
Satellite_pos = [center[0] + radius * cos(angle), center[1] + radius * sin(angle)]
angle += 0.01
# [...]
Upvotes: 1