Python implements the method to respond to events in the game based on pygame (with the source code), pythonpygame

Source: Internet
Author: User

Python implements the method to respond to events in the game based on pygame (with the source code), pythonpygame

This example describes how python responds to events in the game based on pygame. We will share this with you for your reference. The details are as follows:

Let's take a look at the demo I have made:

When a player presses the upper, lower, left, right-click button on the keyboard, the background prints the number value of the player's keys, and the figure moves accordingly.

This is an objective phenomenon.

So what is an event?

You asked me to make a definition. I don't know. I can only give an example to illustrate it. For example, the following code will list some events in the game.

'''Event generation path parameter QUIT users press the close button none ATIVEEVENT Pygame is activated or hidden gain, state KEYDOWN keyboard is pressed unicode, key, mod KEYUP keyboard is opened key, mod MOUSEMOTION move pos, rel, buttons MOUSEBUTTONDOWN press pos, button MOUSEBUTTONUP open pos, button JOYAXISMOTION game handle (Joystick or pad) Move joy, axis, value JOYBALLMOTION Joy ball )? Mobile joy, axis, value JOYHATMOTION game handle (Joystick )? Mobile joy, axis, value JOYBUTTONDOWN game handle press joy, button JOYBUTTONUP game handle open joy, button VIDEORESIZE Pygame window zoom size, w, h VIDEOEXPOSE Pygame window part public (expose) none USEREVENT triggers a user Event code '''

For example, when you press the mouse, the mouse opens the event, and the keyboard presses the event .....

The occurrence of these events is accompanied by an action. It is precisely because of the action that we can catch these actions and respond accordingly.

For example, if we press the left button of the keyboard, the picture will move a distance to the left...

The Code is as follows:

# Handle the key eventimport pygamefrom pygame. locals import * from sys import exit ''' event generation path parameter QUIT user presses the close button none ATIVEEVENT Pygame is activated or hidden gain, state KEYDOWN keyboard is pressed unicode, key, the mod KEYUP keyboard is opened. The mod MOUSEMOTION mouse moves the pos, rel, and buttons MOUSEBUTTONDOWN mouse moves the pos, the button MOUSEBUTTONUP mouse opens the pos, and the button JOYAXISMOTION game handle (Joystick or pad) moves joy, axis, value JOYBALLMOTION Joy ball )? Mobile joy, axis, value JOYHATMOTION game handle (Joystick )? Mobile joy, axis, value JOYBUTTONDOWN game handle press joy, button JOYBUTTONUP game handle open joy, button VIDEORESIZE Pygame window zoom size, w, h VIDEOEXPOSE Pygame window part public (expose) none USEREVENT triggers a user Event code ''' _ author _ = {'name': 'hongten ', 'mail': 'hongtenzone @ foxmail.com', 'qq ': '123', 'version': '1. 0'} BG_IMAGE = 'C: \ py \ ball.png 'pygame. init () screen = pygame. display. set_mode (500,500), 0, 32) bg = pygame. image. load (BG_IMAGE ). convert () x, y = 0, 0move_x, move_y = 0, 0 while 1: for event in pygame. event. get (): # print (event. type) if event. type = QUIT: exit () if event. type = KEYDOWN: print (event. key) # event. key returns a numeric value, while K_LEFT, K_UP, K_RIGHT, and K_DOWN are constants. # They represent a numeric value. These numeric values can be printed (event. key) obtained # For example: K_LEFT = 276 # K_UP = 273 # So the following code can be replaced with: # if event. key = 276: # move_x =-10 if event. key = K_LEFT: move_x =-10 elif event. key = K_UP: move_y =-10 elif event. key = K_RIGHT: move_x = 10 elif event. key = K_DOWN: move_y = 10 elif event. type = KEYUP: move_x = 0 move_y = 0 x + = move_x y + = move_y # print (x, y) screen. fill (0, 0, 0) screen. BITs (bg, (x, y) pygame. display. update ()

Click here to download the complete instance code.

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.