0% found this document useful (0 votes)
33 views14 pages

Programs 11 and 12

The document provides a guide on simulating elliptical orbits and creating a bouncing game using the Pygame module in Python. It includes step-by-step instructions for setting up the Pygame environment, defining screen dimensions, colors, and implementing the game loop for both simulations. The code examples demonstrate how to draw elliptical orbits with moving planets and a bouncing ball game with collision detection against screen boundaries.

Uploaded by

abcdefghi8977123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

Programs 11 and 12

The document provides a guide on simulating elliptical orbits and creating a bouncing game using the Pygame module in Python. It includes step-by-step instructions for setting up the Pygame environment, defining screen dimensions, colors, and implementing the game loop for both simulations. The code examples demonstrate how to draw elliptical orbits with moving planets and a bouncing ball game with collision detection against screen boundaries.

Uploaded by

abcdefghi8977123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

11.

Simulate elliptical orbits in Pygame

Simulate Elliptical Orbit Using Pygame

we will draw an elliptical orbit at its center in Python using the Pygame module. We will draw two
more circles on an elliptical orbit and move them in orbit. This is similar to the earth revolving
around the sun but the only difference is that here 2 circles(planets) move in the same orbit
ensuring that they will not collapse. The circle at the center can be considered as the sun and the
circles in orbit can be considered as earth and artificial earth.

To install Pygame we will use pip. Open the terminal and type the following command:

pip install pygame

Python program to simulate elliptical orbits in Pygame

Step 1: Import Pygame and initialize it.

 Python3

import pygame

import math

# initialize the pygame

pygame.init()

Step 2: Define the width and height of the window and create a game window.

 Python3

# define width of screen

width = 1000

# define height of screen

height = 600

screen_res = (width, height)

pygame.display.set_caption("GFG Elliptical orbit")

screen = pygame.display.set_mode(screen_res)

Step 3: Define colors in RGB format. We will use these colors in our game.

 Python3
# define colors in RGB format

# These colors will be used in our game

red = (255, 0, 0)

green = (0, 255, 0)

blue = (0, 0, 255)

cyan = (0, 255, 255)

Step 4: Define the center of the screen, the radius of the ellipse, and the clock. This center will be
used to draw a circle at the center which represents Sun. And the radius of the ellipse will be used
to calculate the position of the planet with time.

 Python3

# centers of screen

X_center = width//2

Y_center = height//2

# radius of ellipse

# X_ellipse is major radius of ellipsis

X_ellipse = 400

# Y_ellipse is minor radius of ellipsis

Y_ellipse = 225

# pygame.time.Clock() will be used further

# in the game loop to control the speed of the planet.

clock = pygame.time.Clock()

Step 5: Defining the code inside the game loop

 Firstly we create a loop to rotate our planet. We need to update the position of our planets
with time, so for this, we need a loop to cover 360 degrees.

 Then we create an event loop, to get events from the queue.

 Then we check if a user wants to exit the game or not. Also, we fill black color on the
screen.
 Now we find the coordinates of our planets, for this we will use the major and minor
radius of the ellipse.

 Draw a circle or sun at the center of the screen, and draw an ellipse on the screen.

 Now we need to draw our planets for that we will use previously calculated x and y
coordinates to draw planets.

 In last we will update our screen.

 Python3

while True:

for degree in range(0,360,1):

# event loop

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

# fill black color on screen

screen.fill([0,0,0])

# We will find coordinates of 2 planet that

# will rotate in same ellipse

# calculate coordinates of planet 1

# x_planet is x coordinate

x_planet_1 = int(math.cos

(degree * 2 * math.pi/360)

* X_ellipse) + X_center

# y_planet is y coordinate

y_planet_1 = int(math.sin(degree * 2 *

math.pi/360) *

Y_ellipse) + Y_center

# calculate coordinates of planet 2


# As we want our planets to be opposite to

# each other so we will maintain a difference of

# 180 degrees between then

degree_2 = degree+180

# degree will be always between 0 and 360

if degree>180:

degree_2 = degree-180

# x_planet is x coordinate

x_planet_2 = int(math.cos(degree_2 * 2 * math.pi/360) * X_ellipse) + X_center

# y_planet is y coordinate

y_planet_2 = int(math.sin(degree_2 * 2 * math.pi/360) * Y_ellipse) + Y_center

# draw circle in center of screen

pygame.draw.circle(surface=screen,color=red,center=[X_center,Y_center],

radius=60)

# draw ellipse

# Coordinate of left topmost point is (100,75)

# width of ellipse = 2*(major radius of ellipse)

# height of ellipse = 2*(minor radius of ellipse)

pygame.draw.ellipse(surface=screen,

color=green,

rect=[100,75,800,450],

width=1)

# draw both planets

# x_planet_1, y_planet_1, x_planet_2 and


# y_planet_2 are calculated above

pygame.draw.circle(surface=screen,

color=blue,

center=[x_planet_1,y_planet_1],

radius=40)

pygame.draw.circle(surface=screen,

color=cyan,

center=[x_planet_2,y_planet_2],

radius=40)

# Frame Per Second /Refresh Rate

clock.tick(5)

# update screen

pygame.display.flip()

Below is the complete code:

 Python3

import pygame

import math

# initialize the pygame

pygame.init()

# define width of screen

width = 1000

# define height of screen

height = 600

screen_res = (width, height)

pygame.display.set_caption("GFG Elliptical orbit")


screen = pygame.display.set_mode(screen_res)

# define colors in RGB format

# These colors will be used in our game

red = (255, 0, 0)

green = (0, 255, 0)

blue = (0, 0, 255)

cyan = (0, 255, 255)

# centers of screen

X_center = width//2

Y_center = height//2

# radius of ellipse

# X_ellipse is major radius of ellipsis

X_ellipse = 400

# Y_ellipse is minor radius of ellipsis

Y_ellipse = 225

# pygame.time.Clock() will be used further

# in the game loop to control

# the speed of the planet.

clock = pygame.time.Clock()

while True:

for degree in range(0, 360, 1):

# event loop

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()
# fill black color on screen

screen.fill([0, 0, 0])

# We will find coordinates of 2 planet

# that will rotate in same ellipse

# calculate coordinates of planet 1

# x_planet is x coordinate

x_planet_1 = int(math.cos(degree * 2 * math.pi/360)

* X_ellipse) + X_center

# y_planet is y coordinate

y_planet_1 = int(math.sin(degree * 2 * math.pi/360)

* Y_ellipse) + Y_center

# calculate coordinates of planet 2

# As we want our planets to be opposite to

# each other so we will maintain a difference

# of 180 degrees between then

degree_2 = degree+180

# degree will be always between 0 and 360

if degree > 180:

degree_2 = degree-180

# x_planet is x coordinate

x_planet_2 = int(math.cos(degree_2 * 2 * math.pi/360)

* X_ellipse) + X_center

# y_planet is y coordinate

y_planet_2 = int(math.sin(degree_2 * 2 * math.pi/360)

* Y_ellipse) + Y_center
# draw circle in center of screen

pygame.draw.circle(surface=screen, color=red, center=[

X_center, Y_center], radius=60)

# draw ellipse

# Coordinate of left topmost point is (100,75)

# width of ellipse = 2*(major radius of ellipse)

# height of ellipse = 2*(minor radius of ellipse)

pygame.draw.ellipse(surface=screen, color=green,

rect=[100, 75, 800, 450], width=1)

# draw both planets

# x_planet_1, y_planet_1, x_planet_2

# and y_planet_2 are calculated above

pygame.draw.circle(surface=screen, color=blue, center=[

x_planet_1, y_planet_1], radius=40)

pygame.draw.circle(surface=screen, color=cyan, center=[

x_planet_2, y_planet_2], radius=40)

# Frame Per Second /Refresh Rate

clock.tick(5)

# update screen

pygame.display.flip()

Output:
GIF OUTPUT

12. Stimulate bouncing game using Pygame

we will learn to make a bouncing game using Pygame.

Pygame is a set of Python modules designed for writing video games. It adds functionality on top of
the excellent SDL library. This allows you to create fully featured games and multimedia programs in
the Python language. It is free and runs on nearly every platform and operating system. To install
Pygame we will use pip.

Required Module

Open the terminal and type the following command:

pip install pygame

Steps to Create the Bouncing Game using Pygame

Step 1: Import Pygame and initialize it.

 Python3

import pygame

# initialize pygame
pygame.init()

Step 2: Define the width and height of the window and create a game window.

 Python3

# define width of screen

width = 1000

# define height of screen

height = 600

screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")

screen = pygame.display.set_mode(screen_res)

Step 3: Define colors in RGB format. We will use these colors in our game.

 Python3

# define colors

red = (255, 0, 0)

black = (0, 0, 0)

Step 4: Define the ball and the speed by which it will move

This ball object will be used to move the ball in our game, and it will be used to get new coordinates
of the ball and we will use these coordinates to draw the ball. Speed has 2 things, speed in the x
direction and speed in the y direction. when speed in the x direction and speed in the y direction is
equal in magnitude, then the ball will move in a diagonal way. Initially center of the ball is (100,100)
and the speed is (1,1). Now after 1 unit of time, the center of the ball will be (101,101) and again
after 1 unit of time, the center of the ball will be (102,102). In this way, our ball will move.

 Python3

# define ball

ball_obj = pygame.draw.circle(

surface=screen, color=red, center=[100, 100], radius=40)

# define speed of ball


# speed = [X direction speed, Y direction speed]

speed = [1, 1]

Step 5: Define the game loop.

1. Firstly we create an event loop, to get events from the queue.

2. Then we check if a user wants to exit the game or not.

3. Then we fill black color on the screen.

4. Then we will move the ball with a specified speed, and update our ball(Rect) object.

5. If our ball goes out of the screen in the horizontal direction, then we will change the
direction of motion on the X-axis.

6. And If our ball goes out of the screen in the vertical direction, then we will change the
direction of motion on the Y-axis.

7. Finally, we will draw our ball with the center as the center of the ball(Rect) object.

8. In last we will update our screen.

 Python3

# game loop

while True:

# event loop

for event in pygame.event.get():

# check if a user wants to exit the game or not

if event.type == pygame.QUIT:

exit()

# fill black color on screen

screen.fill(black)

# move the ball

# Let center of the ball is (100,100) and the speed is (1,1)

ball_obj = ball_obj.move(speed)

# Now center of the ball is (101,101)


# In this way our wall will move

# if ball goes out of screen then change direction of movement

if ball_obj.left <= 0 or ball_obj.right >= width:

speed[0] = -speed[0]

if ball_obj.top <= 0 or ball_obj.bottom >= height:

speed[1] = -speed[1]

# draw ball at new centers that are obtained after moving ball_obj

pygame.draw.circle(surface=screen, color=red,

center=ball_obj.center, radius=40)

# update screen

pygame.display.flip()

Complete Code

 Python3

import pygame

# initialize pygame

pygame.init()

# define width of screen

width = 1000

# define height of screen

height = 600

screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")

screen = pygame.display.set_mode(screen_res)
# define colors

red = (255, 0, 0)

black = (0, 0, 0)

# define ball

ball_obj = pygame.draw.circle(

surface=screen, color=red, center=[100, 100], radius=40)

# define speed of ball

# speed = [X direction speed, Y direction speed]

speed = [1, 1]

# game loop

while True:

# event loop

for event in pygame.event.get():

# check if a user wants to exit the game or not

if event.type == pygame.QUIT:

exit()

# fill black color on screen

screen.fill(black)

# move the ball

# Let center of the ball is (100,100) and the speed is (1,1)

ball_obj = ball_obj.move(speed)

# Now center of the ball is (101,101)

# In this way our wall will move

# if ball goes out of screen then change direction of movement

if ball_obj.left <= 0 or ball_obj.right >= width:


speed[0] = -speed[0]

if ball_obj.top <= 0 or ball_obj.bottom >= height:

speed[1] = -speed[1]

# draw ball at new centers that are obtained after moving ball_obj

pygame.draw.circle(surface=screen, color=red,

center=ball_obj.center, radius=40)

# update screen

pygame.display.flip()

Output:

Game Output

You might also like