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

What is PyGame frame-by-frame animation

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is PyGame frame-by-frame animation". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is PyGame frame-by-frame animation"?

What is frame-by-frame animation?

Frame-by-frame animation is a kind of animation technology, its principle is to play different images in each frame continuously, so as to produce animation effect.

Inherit the code from the previous section

We realize the animation effect of a circle moving back and forth based on the code in the previous section. The code of the previous section is as follows

Import pygamepygame.init () screen = pygame.display.set_mode ((640,480)) # set the size of the window # create a surface named background, the same size as screen background = pygame.Surface (screen.get_size ()) # fill background with white 255255255 is RGB's white background.fill ((255,255,255) mySurface = pygame.Surface ((200,200)) # 200 x 200 surfacemySurface.set_colorkey ((0,0,0)) pygame.draw.circle (mySurface, (0,0,255), (100,100), 100) background = background.convert () screen.blit (background, (0,0)) mySurface = mySurface.convert_alpha screen.blit (mySurface, (200,200) Clock = pygame.time.Clock () # create clock object FPS = 30 # define the upper limit of the frame rate Is the maximum frame rate mainloop = Truewhile mainloop: clock.tick (FPS) # set the maximum frame rate for event in pygame.event.get (): # this part is the event handling if event.type = = pygame.QUIT: # if you press the fork in the upper right corner mainloop = False # Exit the main loop elif event.type = = pygame.KEYDOWN: # if you press the keyboard if event.key = = pygame.K_ESCAPE: # and press the ESC key mainloop = False # exit the main loop # do some game-related operations here The implementation principle and code of text = "FPS: {0pygame.display.set_caption .2f}" .format (clock.get_fps ()) pygame.display.set_caption (text) pygame.display.flip () # refresh display pygame.quit () is temporarily omitted here.

We will constantly modify the coordinates of the surface in the main loop to achieve the effect of movement. Remember when you blit a surface, there is a parameter is the coordinates?

Screen.blit (mySurface, 200,200)

The (200200) here are the coordinates.

The first time we blit our mySurface, we set its coordinates to (0,0) so that it appears in the upper left corner.

We define mySurfaceX and mySurfaceY to hold the current mySurface coordinates, and dx and dy to represent the increments of mySurfaceX and mySurfaceY in each loop.

In the main loop, modify mySurfaceX, mySurfaceY, and then mySurface is blit to (mySurfaceX, mySurfaceY).

Import pygamepygame.init () screen = pygame.display.set_mode ((640,480)) # set the size of the window # create a surface named background, the same size as screen background = pygame.Surface (screen.get_size ()) # fill background with white 255255255 is RGB's white background.fill ((255,255,255) mySurface = pygame.Surface ((200,200)) # 200 x 200 surfacemySurface.set_colorkey ((0,0,0)) pygame.draw.circle (mySurface, (0,0,255), (100,100), 100) background = background.convert () screen.blit (background, (0) 0) mySurface = mySurface.convert_alpha () # screen.blit (mySurface, (0) 0) mySurfaceX = 0mySurfaceY = 0dx = 10dy = 0####clock = pygame.time.Clock () # create clock object FPS = 30 # define the upper limit of the frame rate Is the maximum frame rate mainloop = Truewhile mainloop: clock.tick (FPS) # set the maximum frame rate for event in pygame.event.get (): # this part is the event handling if event.type = = pygame.QUIT: # if you press the fork in the upper right corner mainloop = False # Exit the main loop elif event.type = = pygame.KEYDOWN: # if the keyboard if event.key = = pygame.K_ESCAPE: # and press the ESC key mainloop = False # exit the main loop # # # # mySurfaceX + = dx mySurfaceY + = dy if mySurfaceX + mySurface.get_rect (). Width > screen.get_rect (). Width: dx * =-1 elif mySurfaceX < 0: dx * =-1 else: pass screen.blit (background (0,0)) #! Screen.blit (mySurface, (round (mySurfaceX, 0), round (mySurfaceY) 0)) # text = "FPS: {0pur.2f}" .format (clock.get_fps ()) pygame.display.set_caption (text) pygame.display.flip () # Refresh display pygame.quit ()

Run this code and you will see a circle moving back and forth. Please pay special attention to the following line of code

Screen.blit (background, (0,0)) #!

If you comment out this line of code, you will get a long bar. This line of code acts as an "erase", overwriting what was drawn earlier.

At this point, I believe you have a deeper understanding of "what is PyGame frame-by-frame animation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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