Amali wickramasinghe
Amali wickramasinghe

Reputation: 83

Control Revolution time around elliptical path - Pygame

I need to revolve the blue circle 4 times faster than the pink circle. Can someone please suggest me a method to do that?

Right now both circles (pink and blue) rotate around red circle in the elliptical paths shown in this image.

Here is my code.

#Elliptical orbits

import pygame
import math
import sys

pygame.init()
screen = pygame.display.set_mode((1000, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()

while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    xRadius = 250
    yRadius = 100
    x2Radius = 100
    y2Radius = 50

    for degree in range(0,360,10):
        x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
        y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
        x2 = int(math.cos(degree * 2 * math.pi / 360) * x2Radius) + 300
        y2 = int(math.sin(degree * 2 * math.pi / 360) * y2Radius) + 150
        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
        pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
        pygame.draw.ellipse(screen, (255, 0, 255), [200, 100, 200, 100], 1)
        pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
        pygame.draw.circle(screen, (255, 0, 255), [x2, y2], 5)

        pygame.display.flip()
        clock.tick(5)

Upvotes: 1

Views: 178

Answers (2)

GLauro
GLauro

Reputation: 5

In a straightforward way, just multiply the blue circle's degrees by 4:

x1 = int(math.cos(degree * 4 * 2 * math.pi / 360) * xRadius) + 300
y1 = int(math.sin(degree * 4 * 2 * math.pi / 360) * yRadius) + 150

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 210899

Do not control the game by an extra loop within the application loop. Use the application loop. Note you would have to handle the events in the for-loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

Add 2 variables degree1 and degree2 and increment them by a different amount every frame. Increment degree1 by 4 and degree2 by 1

degree1 = (degree1 + 4) % 360
degree2 = (degree2 + 1) % 360

Additionally increase the frames per second for smoother animation:

clock.tick(50)

import pygame, math, sys
pygame.init()
screen = pygame.display.set_mode((1000, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()

degree1 = 0
degree2 = 0
while(True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    xRadius = 250
    yRadius = 100
    x2Radius = 100
    y2Radius = 50

    degree1 = (degree1 + 4) % 360
    degree2 = (degree2 + 1) % 360
    x1 = int(math.cos(degree1 * 2 * math.pi / 360) * xRadius) + 300
    y1 = int(math.sin(degree1 * 2 * math.pi / 360) * yRadius) + 150
    x2 = int(math.cos(degree2 * 2 * math.pi / 360) * x2Radius) + 300
    y2 = int(math.sin(degree2 * 2 * math.pi / 360) * y2Radius) + 150
    screen.fill((0, 0, 0))
    pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
    pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
    pygame.draw.ellipse(screen, (255, 0, 255), [200, 100, 200, 100], 1)
    pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
    pygame.draw.circle(screen, (255, 0, 255), [x2, y2], 5)

    pygame.display.flip()
    clock.tick(50)

Upvotes: 2

Related Questions