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

Example Analysis of Pygame Event event Module

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

Share

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

This article introduces the relevant knowledge of "sample Analysis of Pygame Event event Module". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Event is one of the important modules of Pygame, which is the core of building the whole game program, such as mouse click, keyboard tapping, game window movement, window resizing, triggering a specific plot, quitting the game, etc., which can be regarded as "events". Pygame will accept various actions (or events) generated by users, which can be generated at any time, and the amount of operations can be large or small. So how does Pygame handle these events?

Event type

Pygame defines a special structure for dealing with events, that is, event queue, which follows the basic principle of "first come, first served". Through event queue, we can deal with user's actions (triggering events) orderly and one by one. The following table lists the game events commonly used in Pygame:

Event type description member properties QUIT user press window close button noneATIVEEVENTPygame is activated or hidden gain,stateKEYDOWN keyboard presses unicode, key, modKEYUP keyboard release key, modMOUSEMOTION mouse movement pos, rel, buttonsMOUSEBUTTONDOWN mouse down pos, buttonMOUSEBUTTONUP mouse release pos, buttonJOYAXISMOTION game handle (Joystick or pad) moves 喜悦, axis, valueJOYBALLMOTION game ball (喜悦 ball) moves 喜悦, axis, valueJOYHATMOTION game handle (Joystick) moves 喜悦, axis, valueJOYBUTTONDOWN game handle presses 喜悦 ButtonJOYBUTTONUP game controller releases 喜悦, buttonVIDEORESIZEPygame window zooms size, w, hVIDEOEXPOSEPygame window partially exposes (expose) noneUSEREVENT triggers a user event event code

Note that when using Pygame for game development, not all of the above events will be applied, so in the process of learning, we should know how to draw analogies and draw inferences.

Event handling method

The Pygame.event module provides common methods for handling event queues, as shown in the following table:

The method states that pygame.event.get () takes an event from the event queue and removes the event pygame.event.wait () blocking from the queue until the event occurs. If no event occurs, pygame.event.set_blocked () controls which events are prohibited from entering the queue. If the parameter value is None Forbids all events from entering pygame.event.set_allowed () controls which events are allowed to enter the queue pygame.event.pump () calls this method, Pygame will automatically handle the event queue pygame.event.poll () will return a real event based on the actual situation Or a Nonepygame.event.peek () detects whether a certain type of event is in the queue pygame.event.clear () clears all events from the queue pygame.event.get_blocked () detects whether a certain type of event is prohibited from entering the queue pygame.event.post () places a new event into the queue pygame.event.Event () creates a user-defined new event

When we use Pygame to handle events, the logic is generally similar. The first is to judge the type of event, and then according to different event operations, perform different game operations. Therefore, this situation is very suitable for using if. Else statement. As follows:

While True: # wait for the event to occur event = pygame.event.wait () if event.type = = pygame.QUIT: exit () if event.type = = pygame.MOUSEBUTTONDOWN: print ('mouse down' Event.pos) if event.type = = pygame.MOUSEBUTTONUP: print ('mouse up') if event.type = = pygame.MOUSEMOTION: print ('mouse move') # keyboard event if event.type = = pygame.KEYDOWN: # the English character print of the print key ('keyboard press') Chr (event.key)) if event.type = = pygame.KEYUP: print ('keyboard up').

The following is a brief introduction to mouse events and keyboard events using the above methods.

Handling keyboard events

Keyboard events will involve a large number of keystroke operations, such as up and down in the game, or characters forward, backward and other operations, which need the keyboard to achieve.

The keyboard event provides a key property through which you can get keyboard keys. Pygame defines alphabetic keys, numeric keys, key combinations, and other keys on the keyboard as constants. The following table lists the constants of some common keys:

Constant name description K_BACKSPACE Backspace key (Backspace) KTAB Tab key (Tab) K_CLEAR clear key (Clear) K_RETURN enter key (Enter) K_PAUSE pause key (Pause) K_ESCAPE exit key (Escape) K_SPACE Space Bar (Space) K_0...K_90...9K_a...Kza...zK_DELETE Delete key (delete) K_KP0...K_KP90 (Mini Keyboard) 9 (Mini Keyboard) K_F1...K_F15F1...F15K_UP up Arrow (up arrow) K_DOWN Down Arrow (down arrow) K_RIGHT right Arrow (right arrow) K_LEFT left Arrow (left arrow) KMOD_ALT while pressing the Alt key

To learn more about button constants, please refer to the official documentation. Here is more knowledge you want-> Click to go.

Let's learn about the handling of keyboard events through the Picture move example, as follows:

Import pygameimport sys# initialization pygamepygame.init () # defines variables size = width, height = 600,255,255 # load the logo diagram img = pygame.image.load ("C:/Users/Administrator/Desktop/logo.jpg") # the location of the image position = img.get_rect () # create a main window screen = pygame.display.set_mode (size) # title pygame.display.set_caption ("C language Chinese net") # create the game main cycle while True: # set the initial value site = [0 0] for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () # Image Mobile KEYDOWN Keyboard Press event # corresponding key if event.type = = pygame.KEYDOWN: if event.key = = pygame.K_UP: site [1]-= 8 via key attribute If event.key = = pygame.K_DOWN: site [1] + = 8 if event.key = = pygame.K_LEFT: site [0]-= 8 if event.key = = pygame.K_RIGHT: site [0] + = 8 # moving image position = position.move (site) # fill background screen.fill (bg) # place the picture screen.blit (img Position) # Update the display interface pygame.display.flip ()

The running result of the program is as follows:

Figure 1:Pygame Keyboard event-Image move

Handle mouse events

The mouse is one of the most important external devices of the computer, and it is also one of the indispensable tools for game players.

Pygame provides three mouse events, namely mouse movement (MOUSEMOTION), mouse press (MOUSEBUTTONDOWN), and mouse release (MOUSEBUTTONUP). Different event types correspond to different member properties. As follows:

Pygame.event.MOUSEMOTION mouse movement event

Event.pos relative to the upper-left corner of the window, the current coordinate values of the mouse (xPowery)

Relative motion distance of event.rel mouse (XQuery Y), relative to the last event

The initial state of the event.buttons mouse button (0j0pg0) corresponds to (left button, pulley, right button). Click that button while moving, and the corresponding position will change to 1.

Pygame.event.MOUSEBUTTONUP mouse button release event

Event.pos relative to the upper-left corner of the window, the current coordinate values of the mouse (xPowery)

Event.button mouse release button number (integer) left button is 1, press scroll wheel 2, right button is 3

Pygame.event.MOUSEBUTTONDOWN mouse button press event

Event.pos relative to the upper-left corner of the window, the current coordinate values of the mouse (xPowery)

Event.button mouse press button number (integer), left button is 1, press scroll wheel 2, right button is 3, scroll pulley 4 forward, roll pulley 5 backward

Demonstrate the mouse event through a set of simple examples, the sample code is as follows:

The running result of the program is as follows:

Figure 2:Pygame mouse event-mouse movement

This is the end of the "sample Analysis of Pygame Event event Module". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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