How to control the movement of objects by python pygame
This article mainly explains "how to control the movement of objects by python pygame". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to control the movement of objects by python pygame.
Import pygameimport sysfrom pygame.locals import * pygame.init () size = width,hight = 600400speed = [- 2Yue1] by = (255255255) screen = pygame.display.set_mode (size) pygame.display.set_caption ("Mobile Pikachu") # this will take the picture path Change the path of the object you want pp = pygame.image.load ("E:\ python\ Python exercise\ imgs\ pp.jpg") position = pp.get_rect () l_head = ppr_head = pygame.transform.flip (pp,True) False) while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () if event.type = = KEYDOWN: if event.key = = K_LEFT: speed = [- 1 if event.key 0] if event.key = = K_RIGHT: speed = [1J 0] if event.key = = K_UP: speed = [0 -1] if event.key = = K_DOWN: speed = [0je 1] position = position.move (speed) if position.left
< 0 or position.right >Width: pp = pygame.transform.flip (pp,True,False) speed [0] =-speed [0] if position.top
< 0 or position.bottom >Hight: speed [1] =-speed [1] screen.fill (by) screen.blit (pp,position) pygame.display.flip () pygame.time.delay (10) so far, I believe you have a deeper understanding of "how python pygame controls the movement of objects". 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!