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 move your game characters in Pygame

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to move your game character in Pygame. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Pygame is a set of cross-platform Python modules designed specifically for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. You can use pygame to create different types of games, including arcade games, platform games, and so on.

Images used:

You can control the movement of the player. To do this, first create a display object using the display.set_mode () method of pygame, and add the player's wizard using the image.load () method of pygame. The set_mode () function is used to initialize the display surface or window. The size parameter is a pair of numbers that represent width and height. The flags parameter is a collection of additional options. The depth parameter represents the number of bits used for the color.

Syntax:

Set_mode (size= (0,0), flags=0, depth=0, display=0, vsync=0)

Create a variable to store the player's speed. Set the player's initial coordinates. Now, the x and y coordinates of the player are changed according to the keyboard event (that is, the event that occurs when the key state changes).

The blit (surface,surfacerect) function is used to draw an image on the screen.

Syntax:

Blit (surface, surfacerect)

To collect all events from the queue, use the get () function of the event module, and then we use the for loop to iterate over all events.

Syntax:

Get (eventtype=None)

Use the update () function of the display module to update the screen.

Syntax:

Update (rectangle=None)

The following is the implementation

Example: player mobile program

# Import pygame module import pygamefrom pygame.locals import * # launch pygame and grant permission to use the pygame function pygame.init () # create a specific size display surface object window = pygame.display.set_mode Add the title pygame.display.set_caption ('Player Movement') # add the player wizard image = pygame.image.load (rroomhaiyong.png') # store the player's initial coordinates in two variables That is, x and yx = 100y = 100 # create a variable to store the speed of the player's movement velocity = 12 # create an infinite loop run = Truewhile run: # fill the background window.fill ((255,255,255)) # display the list of Event objects returned by the player wizard window.blit (image, (x, y)) # iterative pygame.event.get () method at x and y coordinates. For event in pygame.event.get (): # if the event type is QUIT, close the window and the program if event.type = = pygame.QUIT: run = False pygame.quit () quit () # if the event type is KEYDOWN, press the keyboard button Then check the event key if event.type = = pygame.KEYDOWN: # if the button pressed is the left arrow key, decrease the x coordinates if event.key = = pygame.K_LEFT: X-= velocity # if the button pressed is the right arrow key Then increase x coordinates if event.key = = pygame.K_RIGHT: X + = velocity # if the press button is an up arrow key, decrease y coordinate if event.key = = pygame.K_UP: y-= velocity # if the press button is a down arrow key Increase the y coordinate if event.key = = pygame.K_DOWN: y + = velocity # to draw the surface object to the screen pygame.display.update ()

Output:

Players can also move continuously. To this end, everything remains the same except to make some changes. Here, we create a new clock object to use clock () to control the frame rate of the game.

Grammar

Clock ()

Create a new variable (named key_pressed_is) to store the keys pressed by the user. To do this, we use the get_pressed () function of the key module.

Grammar

Get_pressed ()

It returns a sequence of Boolean values that represents the status of each key on the keyboard.

Example: continuous mobile players

# Import pygame module import pygamefrom pygame.locals import * # launch pygame and grant permission to use the pygame function pygame.init () # create a specific size display surface object window = pygame.display.set_mode ) # add the title pygame.display.set_caption ('player Mobile') # initialize the clock to track and control the frame rate of the game clock = pygame.time.Clock () # add the player wizard image = pygame.image.load (rroomhaiyong.png') # store the player's initial coordinates in two variables That is, x and yx = 100y = 100 # create a variable to store the speed of the player's movement velocity = 12 # create an infinite loop run = Truewhile run: # set the frame rate to 60 fps clock.tick (60) # fill the background window.fill ((255,255,255)) with white # display the player wizard window.blit (image, (x) at the x and y coordinates Y)) # iterate through the list of Event objects returned by the pygame.event.get () method. For event in pygame.event.get (): # if event type is QUIT Then close the window and program if event.type = = pygame.QUIT: run = False pygame.quit () quit () # use the key.get_pressed () method to store the pressed key in the new variable key_pressed_is = pygame.key.get_pressed ( ) # change player coordinates if key_pressed_ is [K _ LEFT]: X-= 8 if key_pressed_ is [K _ RIGHT]: X + = 8 if key_pressed_ is [K _ UP]: y-= 8 if key_pressed_ is [K _ DOWN]: y + = 8 # draw surface objects to the screen pygame.display.update ()

Output:

Flip the player wizard

You can easily flip any wizard using the flip () function of pygame's conversion module. For example, if we want to flip the wizard when the player changes the direction of movement, we can use the following code

Window.blit (pygame.transform.flip (image, False, True), (xQuery))

The flip () function is used to flip surface objects horizontally and vertically. Or both. This function takes three parameters:

Image to flip

Boolean value for horizontal flip

Boolean value for vertical flip

Here is the implementation.

Example: flip player ima

Output:

We can also easily update player sprites by creating a list of sprites.

Image = [pygame.image.load (raschaiyong.png'), pygame.image.load (raschaiyong2.png')]

Example: update Sprite

# Import pygame module import pygamefrom pygame.locals import * # launch pygame and grant permission to use the pygame function pygame.init () # create a specific size display surface object window = pygame.display.set_mode # add the title pygame.display.set_caption ('player switch') # initialize the clock to track and control the frame rate of the game clock = pygame.time.Clock () # create a variable to check the direction of motion # whenever the player changes direction We will change its value direction = True # to add the player wizard image = [pygame.image.load (raschaiyong.png'), pygame.image.load (raschaiyong2.png')] # to store the player's initial coordinates in two variables That is, x and yx = 100y = 100 # create a variable to store the speed of the player's movement velocity = 12 # create an infinite loop run = Truewhile run: # set the frame rate to 60 fps clock.tick (60) # fill the background window.fill ((176,224,230)) with light grayish blue # display the player wizard at the x and y coordinates # if the player changes direction Change the list of Event objects returned by the player wizard if direction = = True: window.blit (image [0], (x, y)) if direction = = False: window.blit (image [1], (x, y)) # iterative Event () method. For event in pygame.event.get (): # if event type is QUIT Then close the window and program if event.type = = pygame.QUIT: run = False pygame.quit () quit () # change the value of the direction variable if event.type = = pygame.KEYDOWN: if Event.key = = pygame.K_RIGHT: direction = True elif event.key = = pygame.K_LEFT: direction = False # use the key.get_pressed () method to store the pressed key in the new variable key_pressed_is = pygame.key.get_pressed () # change player coordinates if key_pressed_ is [K _ LEFT]: X-= 5 if key_pressed_ is [K _ RIGHT]: X + = 5 if key_pressed_ is [K _ UP]: y-= 5 if key_pressed_ is [K _ DOWN]: y + = 5 # draw surface objects to the screen pygame.display.update ()

Try another picture.

This is the end of the article on "how to move your game character in Pygame". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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