This example describes Python's approach to responding to events in the game based on the Pygame implementation. Share to everyone for your reference, as follows:
Let's look at the demo effect I've done:
When the player presses on the keyboard: up, down, left, right button, the background will print out the number of keys that the player presses, and the graphic will move with it
This is the phenomenon that exists objectively above.
So what is an event?
You asked me to make a definition, I don't know, I can only give an example to illustrate, for example, the next code, listing some of the events in the game
"' event generation path parameter ' QUIT user pressed off button none ativeevent pygame is activated or hidden gain, state KEYDOWN Keyboard is pressed Unicode, key, mod KEYUP keyboard is released key, mod mousemotion Mouse Mobile pos , rel, buttons mousebuttondown Mouse Press pos, button mousebuttonup mouse release pos, Button Joyaxismotion Game handle (Joystick or pad) move Joy, axis, value joyballmotion game ball (joy balls)? Move Joy, Axis, value joyhatmotion gamepad (Joystick)? Move Joy, axis, value joybuttondown gamepad Press Joy, Button Joybuttonup game handle let go of joy, button videoresize pygame window Scaling size, W, h Videoexpose pygame window partially exposed (expose) none userevent triggered a user event code ""
such as mouse down events, mouse release events, keyboard press events .....
The occurrence of these events is accompanied by the occurrence of an action, precisely because there are movements, so we can catch these movements, so as to respond accordingly
For example, if we press the left button of the keyboard, the picture will move to the left for a distance ...
The code section 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, mod KEYUP keyboard is released key, mod mousemotion mouse Mobile pos, rel, button S Mousebuttondown Mouse Press pos, button Mousebuttonup mouse release pos, button Joyaxismotion Tour Play handle (Joystick or pad) move joy, axis, value joyballmotion game ball (joy balls)? Move joy, axis, value joyhatmotion Tour Play handle (Joystick)? Move joy, axis, value Joybuttondown gamepad press Joy, button Joybuttonup GamePad release Joy, button videoresize pygame window scaling Size, W, H videoexpose pygame window partially exposed (expose) No Ne userevent triggered a user event code ' __author__ = {' name ': ' Hongten ', ' Mail ': ' hongtenzone@foxmail.com ' , ' QQ ': ' 6487198', ' 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, 0while 1:for event in Pygame.event.get (): #p Rint (event.type) if Event.type = = Quit:exit () If Event.type = Keydown:print (Event.key) #event. Key return Back is a numeric value, and K_left,k_up,k_right,k_down, and so are constants, #他们代表的也是一个数字值, these numeric values can be used: print (Event.key) get to #如: K_left = 276 # K_ Up = 273 #所以下面的代码可以替换为: #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 = Ten elif event.ke y = = K_down:move_y = 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.blit (BG, (x, y)) pygame.display.update ()
Full instance code code click here to download this site.
I hope this article is helpful for Python program design.