Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create an image with Pygame Surface

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to use Pygame Surface to create images of the relevant knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.

Pygame provides different modules for text, image, and color to generate their own Surface objects. The Surface module is specially used to create new images in Pygame. Through this module, you can create a Surface object with the following syntax format:

Surface=pygame.Surface (size= (width,height), flags,depth)

The parameter function is as follows:

Size: represents the size of a rectangular area of a Surface object

Flags: function flag bit with two optional parameter values, HWSURFACE and SPCALPHA. The former represents storing the created Surface object in video memory, and the latter means that every pixel of the image contains an alpha channel.

Depth: specifies the color depth of pixels, which defaults to adaptive mode and is automatically adjusted by Pygame.

Let's take a look at a set of simple usage examples, as follows:

Import sysimport pygame# must initialize pygame.init () # to set the home screen window before using pygame Set the full screen format: flags=pygame.FULLSCREENscreen = pygame.display.set_mode ((400400)) # set the window title pygame.display.set_caption ('c language Chinese net') screen.fill ('white') # create a 50' 50 image and optimize the display of face = pygame.Surface ((50 white' 50), flags=pygame.HWSURFACE) # fill color face.fill (color='pink') while True: # Loop acquisition event Monitoring event for event in pygame.event.get (): # determine whether the user clicked the close button if event.type = = pygame.QUIT: # Uninstall all modules pygame.quit () # Terminator sys.exit () # add the drawn image to the main screen, (100100) is the location coordinate The upper left corner of the display screen is the coordinate system (0d0) origin screen.blit (face, (100,100)) pygame.display.flip () # Update screen content

From the above running results, we can see that through the Surface module, we successfully draw a 50 * 50 rectangular area on the main screen. Note that if you do not set the size, Surface creates an area the same size as the main game screen by default.

The Surface module also provides other ways to process images, which are briefly described in the following table.

Method description

Pygame.Surface.blit () draws one image (Surface object) onto another image

Pygame.Surface.convert () modifies the pixel format of an image (Surface object)

Pygame.Surface.fill () fills Surface objects with solid colors

Pygame.Surface.scroll () copies and moves Surface objects

Pygame.Surface.set_alpha () sets the transparency of the entire image

Pygame.Surface.get_at () gets the color value of a pixel

Pygame.Surface.set_at () sets the color value of a pixel

Pygame.Surface.get_palette () gets the color palette of the 8-bit index of the Surface object

Pygame.Surface.map_rgb () converts a RGBA color to a mapped color value

Pygame.Surface.set_clip () sets the current clipping area of the Surface object

Pygame.Surface.subsurface () creates a new child Surface object based on the parent object

Pygame.Surface.get_offset () gets the offset position of the child Surface object in the parent object

Pygame.Surface.get_size () gets the size of the Surface object

In addition to using the Surface module to create a new image, we can use another method to load the image from the outside, as shown below:

Pygame.image.load ("picture path"). Convert ()

The above method uses the loaded image as a Surface object, so it can call all the methods in the table above. Note that the reason why convert () is used here to convert the pixel format of the loaded picture is to improve the processing speed of the image by Pygame, which ensures that the pixel format of the image is the same as the display format of the picture.

The image.load () method allows you to load the background image of the game, or other elements used in the game, such as characters, props, and so on.

Let's look at a set of simple examples:

Import pygame# introduces all constants in pygame For example, QUITfrom pygame.locals import * pygame.init () screen = pygame.display.set_mode ((500250)) pygame.display.set_caption ('c language Chinese website') # load a picture image_surface = pygame.image.load ("C:/Users/Administrator/Desktop/c-net.png"). Convert () # rect (left,top,width,height) specifies the # special_flags function flag bit of an area on the picture and specifies the color mixing mode The default is 0 to fill image_surface.fill with solid color ((0j0255), rect= (100recovery100), special_flags=0) # 200100 represents the offset of the image horizontally and vertically, with the upper left corner as the coordinate origin image_surface.scroll (100jue 50) # infinite loop Let the window stay while True: for event in pygame.event.get (): if event.type = = QUIT: exit () # put the image on the main screen screen.blit (image_surface, (0L0)) pygame.display.update () above is all the content of the article "how to create an image with Pygame Surface". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report