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 use Pygame to make simple Animation

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use Pygame to make simple animation". In daily operation, I believe many people have doubts about how to use Pygame to make simple animation. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to use Pygame to make simple animation". Next, please follow the editor to study!

Preface

To achieve a frame animation, the use of a graph, according to different times to display different pictures.

Use a picture as shown below, with a width of 780 * 300, using a loaded image of 260 * 150.

Pygame.init () screen = pygame.display.set_mode ((400,300), 0,32) pygame.display.set_caption ("animation") while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () key = pygame.key.get_pressed () if key [pygame.K _ ESCAPE]: sys.exit () screen.fill ((154,205) ) pygame.display.update ()

First implement the simplest graphics

timer

First of all, change the current image according to the different time.

Time timing is implemented using pygame.time.Clock (). Get_ticks () gets an increasing or decreasing amount of time.

Framerate = pygame.time.Clock () frametate.tick (30) ticks = pygame.time.get_ticks () pygame.init () screen = pygame.display.set_mode ((400,300), 0 32) pygame.display.set_caption ("animation") framerate = pygame.time.Clock () while True: framerate.tick (30) ticks = pygame.time.get_ticks () print (ticks) for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () key = pygame.key.get_pressed () if key [pygame.K _ ESCAPE]: Sys.exit () screen.fill 205,255)) pygame.display.update ()

seven hundred and fifty eight

seven hundred and ninety one

eight hundred and twenty four

eight hundred and fifty eight

eight hundred and ninety one

nine hundred and twenty four

nine hundred and fifty eight

nine hundred and ninety two

1025

1058

The output is shown above, 30 frames per second, so the time increases by about 34-33 each time, and as shown above, we get an amount that changes over time.

Draw the wizard

Here the wizard is used to draw the picture for ease of operation.

Class MySprite (pygame.sprite.Sprite): def _ _ init__ (self Target): pygame.sprite.Sprite.__init__ (self) self.master_image = None # self.frame for storing pictures = 0 # initial picture position self.old_frame =-1 # previous picture position self.frame_width = 1 self.frame_height = 1 # Save picture size for each frame self.first_frame = 0 self.last_frame = interval of 0 # position value self.columns = 1 # maximum number of frames self.last_time = 0 # time def load between saves (self Filename, width, height, columns): self.master_image = pygame.image.load (filename). Convert_alpha () # load picture self.frame_width = width # 260 self.frame_height = height # 150 self.rect = 0,0, width Height self.columns = columns # the number of column widths is 3 # try to auto-calculate total frames rect = self.master_image.get_rect () # get the size of the corresponding picture 780 * 300 self.last_frame = (rect.width / / width) * (rect.height / / height)-1 # 5 def update (self, current_time) Rate = 30): # current_time update frequency is 30 # update animation frame number if current_time > self.last_time + rate: # if the current event is greater than the last time + current rhythm self.frame + = 1 # current number of frames plus one if self.frame > self.last_frame: # the current last frame starts from the first frame Self.frame = self.first_frame # starting at 0 self.last_time = current_time # set the last frame value to 30 # build current frame only if it changed if self.frame! = self.old_frame: # the current number of frames is not equal to the old frame_x = (self.frame% self.columns) * self.frame_width Frame_y = (self.frame / / self.columns) * self.frame_height rect = (frame_x) Frame_y, self.frame_width, self.frame_height) # Update the corresponding location self.image = self.master_image.subsurface (rect) # Loop Box existing direction self.old_frame = self.frame load wizard

Pygame.sprite.Group () creates a spirit group, and then draws the corresponding elves using the corresponding update draw

# create the spritedragon = MySprite (screen) dragon.load ("Fig07-02.png", 260,150,3) group = pygame.sprite.Group () group.add (dragon) group.update (ticks) group.draw (screen) complete code import sysimport pygameclass MySprite (pygame.sprite.Sprite): def _ init__ (self) Target): pygame.sprite.Sprite.__init__ (self) self.master_image = None self.frame = 0 self.old_frame =-1 self.frame_width = 1 self.frame_height = 1 self.first_frame = 0 self.last_frame = 0 self.columns = 1 self.last_time = 0 def load (self, filename, width, height Columns): # load picture # 780300 self.master_image = pygame.image.load (filename). Convert_alpha () # load picture self.frame_width = width # 260 self.frame_height = height # 150 self.rect = 0,0, width Height self.columns = columns # the number of column widths is 3 # try to auto-calculate total frames rect = self.master_image.get_rect () # get the size of the corresponding picture 780 * 300 self.last_frame = (rect.width / / width) * (rect.height / / height)-1 # 5 def update (self, current_time) Rate=30): # current_time update frequency is 30 # update animation frame number if current_time > self.last_time + rate: # if the current event is greater than the last time + current rhythm self.frame + = 1 # current number of frames plus one if self.frame > self.last_frame: # the current last frame starts from the first frame Self.frame = self.first_frame # starting at 0 self.last_time = current_time # set the last frame value to 30 # build current frame only if it changed if self.frame! = self.old_frame: # the current number of frames is not equal to the old frame_x = (self.frame% self.columns) * self.frame_width frame_ Y = (self.frame / / self.columns) * self.frame_height rect = (frame_x) Frame_y, self.frame_width, self.frame_height) # Update the corresponding position self.image = self.master_image.subsurface (rect) # the existing direction of the circular box self.old_frame = self.framepygame.init () screen = pygame.display.set_mode ((400,300), 0 32) pygame.display.set_caption ("Animation") framerate = pygame.time.Clock () # create wizard dragon = MySprite (screen) dragon.load ("Fig07-02.png", 260150 3) group = pygame.sprite.Group () group.add (dragon) while True: framerate.tick (30) ticks = pygame.time.get_ticks () print (ticks) for event in pygame.event.get (): if event.type = pygame.QUIT: sys.exit () key = pygame.key.get_pressed () if key [pygame.K _ ESCAPE]: sys.exit () screen.fill ((154,205) Group.update (ticks) group.draw (screen) pygame.display.update () to this point The study on "how to make simple animation with Pygame" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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