Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Getting width and height of an image in Pygame
Next article icon

Pygame is a cross-platform set ofPython modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.

Command to installpygame :

pip install pygame

How to display images with PyGame ?

There are four basic steps to displaying images on the pygame window :

  • Create a display surface object usingdisplay.set_mode() method of pygame.
  • Create a Image surface object i.e. surface object in which image is drawn on it, using image.load() method of pygame.
  • Copy the image surface object to the display surface object usingblit() method of pygame display surface object.
  • Show the display surface object on the pygame window usingdisplay.update() method of pygame.

Display images using PyGame

Here we are first importing the required library and then setting the width and height of the image then creating the display surface for that size then give the path of the required image in then image.load() function and then finally iterate over the list of event objects.

Python3
# importing required libraryimportpygame# activate the pygame library .pygame.init()X=600Y=600# create the display surface object# of specific dimension..e(X, Y).scrn=pygame.display.set_mode((X,Y))# set the pygame window namepygame.display.set_caption('image')# create a surface object, image is drawn on it.imp=pygame.image.load("C:\\Users\\DELL\\Downloads\\gfg.png").convert()# Using blit to copy content from one surface to otherscrn.blit(imp,(0,0))# paint screen one timepygame.display.flip()status=Truewhile(status):# iterate over the list of Event objects# that was returned by pygame.event.get() method.foriinpygame.event.get():# if event object type is QUIT# then quitting the pygame# and program both.ifi.type==pygame.QUIT:status=False# deactivates the pygame librarypygame.quit()

Output:

 Display images with PyGame
 Display images with PyGame

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp