SlideShare uma empresa Scribd logo
1 de 16
NADAR SARASWATHI COLLEGE OF ARTS&SCIENCE,THENI
DEPARTMENT OF COMPUTER SCIENCE AND
INFORMATION TECHOLOGY
VANMATHY V
I MSC(CS)
TOPIC:
ANIMATION BOUNCING BALL
Python program to bouncing ball in
Pygame:
In this article, you will learn how to program a
bouncing ball using Python Pygame. Pygame is
a collection of Python modules intended for
composing computer games. Pygame includes
functionality on top of the magnificent SDL
library. This permits you to make fully featured
games and interactive multi media programs in
the Python language. It is free, highly portable
and runs on nearly every platform and operating
system.
 Import and Initialize Pygame
 First, let's import the pygame and sys
modules at the top of your python file.
 import sys, pygame pygame.init()
Set screen size, background color and
caption:
The pygame.display.set_mode() function
returns the surface object for the window. This
function accepts the width and height of the
screen as arguments. To set the caption of the
window, call the pygame.display.set_caption()
function.
width = 550 height = 300 color = (255, 250, 250)
screen = pygame.display.set_mode(width,
height)
Set speed of the moving object:
 Pygame provides the move() method to
make an object move smoothly across the
screen. The syntax is-
 move(forward, right) All the attributes are
optional. But for the bouncing ball, we have
set the following-
 speed = [1, 1] rect_boundry=
rect_boundry.move(speed)
Load the moving object and set the
rectangle area covering the image
 Here, we have opened the image using the
pygame.image.load() method and set the
ball rectangle area boundary using the
get_rect() method.
 ball = pygame.image.load("ball.png")
rect_boundry= ball.get_rect()
Make ball movement continuity:
 We have created an infinite loop to make the
ball move continuously and reverse the
direction of the ball if it hits the edges.
 while 1:
rect_boundry=rectboundry.move(speed) if
rect_boundry.left < 0 or rect_boundry.right >
width: speed[0] = -speed[0] if rect_boundry.top
< 0 or rectboundry.bottom > height:
 speed[1] = -speed[1]
Fill the background color and blit the screen:
 The fill() method is used to fill the surface with
background color. In the above, we have
mentioned how to create a moving object. But,
we also need to render them on the window.
In pygame, this is called blitting of an object
and is implemented with the blit() method.
 screen. fill(color) screen. Blit(ball,
rect_boundry)
Make image visible:
 We have used the pygame flip() method to
make all images visible.
 pygame.display.flip()
Complete Code:
In the above, we have explained the bouncing ball algorithm
in detail with pygame codes. Here, we have merged all the
above to get the complete code.
import os os.environ
['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" import
sys,pygame from pygame.locals
import * pygame.init() speed = [1, 1]
color = (255, 250, 250)
width = 550
height = 300
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame bouncing ball")
ball = pygame.image.load("ball.png")
rect_boundry = ball.get_rect()
while 1: for event in pygame.event.get():
rect_boundry = rect_boundry.move(speed)
if rect_boundry.left < 0 or rect_boundry.right >
width: speed[0] = -speed[0]
if rect_boundry.top < 0 or rect_boundry.bottom >
height: speed[1] = -speed[1]
screen.fill(color) screen.blit(ball, rect_boundry)
pygame.display.flip()
if event.type == QUIT: pygame.quit()
sys.exit()
Vanmathy python
Vanmathy python
Vanmathy python
Vanmathy python

Mais conteúdo relacionado

Semelhante a Vanmathy python

How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3d
Dao Tung
 
Desenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhoneDesenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhone
Tiago Oliveira
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
acteleshoppe
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
murtuzadahadwala3
 

Semelhante a Vanmathy python (20)

The Ring programming language version 1.6 book - Part 51 of 189
The Ring programming language version 1.6 book - Part 51 of 189The Ring programming language version 1.6 book - Part 51 of 189
The Ring programming language version 1.6 book - Part 51 of 189
 
The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3d
 
Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»Денис Ковалев «Python в игровой индустрии»
Денис Ковалев «Python в игровой индустрии»
 
Desenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhoneDesenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhone
 
Teaching Python to 9 Year Old Girl - map mover
Teaching Python to 9 Year Old Girl -  map moverTeaching Python to 9 Year Old Girl -  map mover
Teaching Python to 9 Year Old Girl - map mover
 
The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196The Ring programming language version 1.7 book - Part 50 of 196
The Ring programming language version 1.7 book - Part 50 of 196
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
 
Om Pawar MP AJP.docx
Om Pawar MP AJP.docxOm Pawar MP AJP.docx
Om Pawar MP AJP.docx
 
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
 
Using the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdfUsing the code below- I need help with creating code for the following.pdf
Using the code below- I need help with creating code for the following.pdf
 
C++ game development with oxygine
C++ game development with oxygineC++ game development with oxygine
C++ game development with oxygine
 
Chapter1 8
Chapter1 8Chapter1 8
Chapter1 8
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
 
FITC 2013 - The Technical Learning Curve
FITC 2013 - The Technical Learning CurveFITC 2013 - The Technical Learning Curve
FITC 2013 - The Technical Learning Curve
 
Compose camp 2.pptx
Compose camp 2.pptxCompose camp 2.pptx
Compose camp 2.pptx
 

Mais de PriyadharshiniVS

Mais de PriyadharshiniVS (20)

R Ramya devi digital image processing
R  Ramya devi digital image processingR  Ramya devi digital image processing
R Ramya devi digital image processing
 
R ramya devi cloud computing
R ramya devi cloud computingR ramya devi cloud computing
R ramya devi cloud computing
 
Ramya devi R women empowerment
Ramya devi R women empowermentRamya devi R women empowerment
Ramya devi R women empowerment
 
R Ramya devi artificial intelligence and machine learning
R Ramya devi  artificial intelligence and machine learningR Ramya devi  artificial intelligence and machine learning
R Ramya devi artificial intelligence and machine learning
 
Ramya devi R internet of things
Ramya devi R internet of thingsRamya devi R internet of things
Ramya devi R internet of things
 
vs.pptx
vs.pptxvs.pptx
vs.pptx
 
WE.pptx
WE.pptxWE.pptx
WE.pptx
 
IOT.pptx
IOT.pptxIOT.pptx
IOT.pptx
 
cc.pptx
cc.pptxcc.pptx
cc.pptx
 
MOBILE APP DEVELOPMENT USING PYTHON
MOBILE APP DEVELOPMENT USING PYTHONMOBILE APP DEVELOPMENT USING PYTHON
MOBILE APP DEVELOPMENT USING PYTHON
 
PriyaDharshini distributed operating system
PriyaDharshini distributed operating systemPriyaDharshini distributed operating system
PriyaDharshini distributed operating system
 
Vanmathy distributed operating system
Vanmathy distributed operating system Vanmathy distributed operating system
Vanmathy distributed operating system
 
vanmathy cryptography network security
vanmathy cryptography network securityvanmathy cryptography network security
vanmathy cryptography network security
 
vanmathy cryptography network security
vanmathy cryptography network security vanmathy cryptography network security
vanmathy cryptography network security
 
Vanmathy distributed operating system
Vanmathy distributed operating system Vanmathy distributed operating system
Vanmathy distributed operating system
 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
 
Cryptography and network security
Cryptography and network securityCryptography and network security
Cryptography and network security
 
No sql
No sql No sql
No sql
 
Graph, graph and network modeling, advantage of graph database
Graph, graph and network modeling, advantage of graph databaseGraph, graph and network modeling, advantage of graph database
Graph, graph and network modeling, advantage of graph database
 
Compiler design
Compiler designCompiler design
Compiler design
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

Vanmathy python

  • 1. NADAR SARASWATHI COLLEGE OF ARTS&SCIENCE,THENI DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHOLOGY VANMATHY V I MSC(CS)
  • 3. Python program to bouncing ball in Pygame: In this article, you will learn how to program a bouncing ball using Python Pygame. Pygame is a collection of Python modules intended for composing computer games. Pygame includes functionality on top of the magnificent SDL library. This permits you to make fully featured games and interactive multi media programs in the Python language. It is free, highly portable and runs on nearly every platform and operating system.
  • 4.  Import and Initialize Pygame  First, let's import the pygame and sys modules at the top of your python file.  import sys, pygame pygame.init()
  • 5. Set screen size, background color and caption: The pygame.display.set_mode() function returns the surface object for the window. This function accepts the width and height of the screen as arguments. To set the caption of the window, call the pygame.display.set_caption() function. width = 550 height = 300 color = (255, 250, 250) screen = pygame.display.set_mode(width, height)
  • 6. Set speed of the moving object:  Pygame provides the move() method to make an object move smoothly across the screen. The syntax is-  move(forward, right) All the attributes are optional. But for the bouncing ball, we have set the following-  speed = [1, 1] rect_boundry= rect_boundry.move(speed)
  • 7. Load the moving object and set the rectangle area covering the image  Here, we have opened the image using the pygame.image.load() method and set the ball rectangle area boundary using the get_rect() method.  ball = pygame.image.load("ball.png") rect_boundry= ball.get_rect()
  • 8. Make ball movement continuity:  We have created an infinite loop to make the ball move continuously and reverse the direction of the ball if it hits the edges.  while 1: rect_boundry=rectboundry.move(speed) if rect_boundry.left < 0 or rect_boundry.right > width: speed[0] = -speed[0] if rect_boundry.top < 0 or rectboundry.bottom > height:  speed[1] = -speed[1]
  • 9. Fill the background color and blit the screen:  The fill() method is used to fill the surface with background color. In the above, we have mentioned how to create a moving object. But, we also need to render them on the window. In pygame, this is called blitting of an object and is implemented with the blit() method.  screen. fill(color) screen. Blit(ball, rect_boundry)
  • 10. Make image visible:  We have used the pygame flip() method to make all images visible.  pygame.display.flip()
  • 11. Complete Code: In the above, we have explained the bouncing ball algorithm in detail with pygame codes. Here, we have merged all the above to get the complete code. import os os.environ ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" import sys,pygame from pygame.locals import * pygame.init() speed = [1, 1] color = (255, 250, 250) width = 550 height = 300 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Pygame bouncing ball") ball = pygame.image.load("ball.png") rect_boundry = ball.get_rect()
  • 12. while 1: for event in pygame.event.get(): rect_boundry = rect_boundry.move(speed) if rect_boundry.left < 0 or rect_boundry.right > width: speed[0] = -speed[0] if rect_boundry.top < 0 or rect_boundry.bottom > height: speed[1] = -speed[1] screen.fill(color) screen.blit(ball, rect_boundry) pygame.display.flip() if event.type == QUIT: pygame.quit() sys.exit()