Simulate Elliptical Orbit in Pygame

#Elliptical orbits

import pygame
import math
import sys

pygame.init()

screen = pygame.display.set_mode((600, 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
   
    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
        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.circle(screen, (0, 0, 255), [x1, y1], 15)
       
        pygame.display.flip()
        clock.tick(5)


Algorithmhttps://drranurekha.blogspot.com/2019/10/algorithm-simulation-of-elliptical-orbit_58.html



Output:








20 comments:

  1. It would be better if you could explain the program step by step

    ReplyDelete
  2. sure

    # setting screen size and caption
    screen = pygame.display.set_mode((600, 300))
    pygame.display.set_caption("Elliptical orbit")

    # creating clock variable
    clock = pygame.time.Clock()


    # until closing the display screen
    while(True):
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    sys.exit()

    # setting x and y radius of ellipse.
    xRadius = 250
    yRadius = 100


    '''
    starting from degree 0 ending with 360 degrees in increments of 10 degrees calculate the (x1, y1) coordinates to find a point in the elliptical orbit

    x1 = cos(radians) * xradius of ellipse

    first convert degree to radians (degree * 2 * math.pi / 360)
    x1 = (type conversion-int)(math.cos(radians) * xradius) + screensize/2
    '''

    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

    # set background color, draw center circle, ellipse and another smaller circle on the ellipse

    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.circle(screen, (0, 0, 255), [x1, y1], 15)

    # refresh the screen every 5 clock ticks
    pygame.display.flip()
    clock.tick(5)

    ReplyDelete
  3. you can change the screen size by altering the values of width(600) and height(300)

    screen = pygame.display.set_mode((600, 300))

    ReplyDelete

  4. pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)

    Note:
    screen = pygame.display.set_mode((600, 300))
    pygame.draw.circle(screen, (color of circle), [x, y coordinates], radius of circle)

    ReplyDelete
  5. clock.tick(5)

    modify the speed by changing the clock tick

    ReplyDelete
  6. Traceback (most recent call last):
    File "C:/Python32/gui1.py", line 2, in
    import pygame
    File "C:\Python32\lib\pygame\__init__.py", line 141, in
    from pygame.base import *
    ImportError: DLL load failed: The specified module could not be found.

    ReplyDelete
    Replies
    1. check if you have compatible version of python and pygame.
      pygame-1.9.2a0.win32-py3.2
      python-3.2
      Note:
      mine is 32 bit. check your system configuration

      Happy programming...
      Anu

      Delete
    2. try and tell me if it works

      thanks
      Anu

      Delete
    3. my python version is 3.7.3
      then i installed pygame-1.9.2a0.win32-py3.2
      still it is showing trace back error like

      Traceback (most recent call last):
      File "C:/PYTHON/pg.py", line 3, in
      import pygame
      ModuleNotFoundError: No module named 'pygame'

      please solve my problem

      Delete
    4. You need compatible versions of python and pygame. ur pygame is 3.2 and python is 3.7. wont work.

      Delete
  7. Traceback (most recent call last):
    File "C:/Python27/ellip.py", line 7, in
    screen = pygame.display.set_mode((600, 300))
    error: No available video device
    Im getting this error...

    ReplyDelete
    Replies
    1. Iam also getting error. and yes I have checked import sys line

      File "", line 24
      screen.fill((0, 0, 0))
      ^
      IndentationError: unexpected indent

      Delete
    2. yours is an indentation error. not in import.
      screen.fill((0,0,0)) should be in same indent as previous lines within the for loop.

      Delete
  8. Thankyou mam helped a lot on exams

    ReplyDelete
  9. i need algorithm for that simulation program of elliptical orbit in pygame

    ReplyDelete
    Replies
    1. https://drranurekha.blogspot.com/2019/10/algorithm-simulation-of-elliptical-orbit_58.html

      Delete
  10. Thanks for the extremely helpful information!

    ReplyDelete
  11. How to import the module for begining

    ReplyDelete

Don't be a silent reader...
Leave your comments...

Anu