cool hit counter pygame series_events in games_Intefrankly

pygame series_events in games


Let's start with the demo I made.

When the player presses: up, down, left, or right on the keyboard, the numeric value of the player's keystroke is printed in the background and the graph moves with it

This is a phenomenon that exists above objectively.

So what is an event?

You asked me to make a definition, I don't know, I can only give an example, for example, in the next code that lists some events about the game

'''
     Event Generation Pathway Parameters
     QUIT User presses the off button none
     ATIVEEVENT Pygame is activated or hidden gain, state
     KEYDOWN Keyboard pressed unicode, key, mod
     KEYUP Keyboard released key, mod
     MOUSEMOTION mouse movement pos, rel, buttons
     MOUSEBUTTONDOWN mouse down pos, button
     MOUSEBUTTONUP mouse release pos, button
     JOYAXISMOTION Joystick or pad movement joy, axis, value
     JOYBALLMOTION Joy ball? Move joy, axis, value
JOYHATMOTION 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 public (expose) none
     USEREVENT triggers a user event code
'''

Such as mouse press event, mouse release event, keyboard press event .....

These events are accompanied by an action, and it is because there is an action that we can pounce on them and react accordingly

For example, if we press the left key of the keyboard, the picture moves a distance to the left...

================================================

Code section.

================================================

 1 #handle the key event
 2 
 3 import pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 
 7 '''
 8      Event Generation Pathway Parameters
  9 QUIT User presses the off button none
10      ATIVEEVENT Pygame is activated or hidden gain, state
 11 KEYDOWN Keyboard pressed unicode, key, mod
 12 KEYUP Keyboard is released key, mod
 13 MOUSEMOTION mouse movement pos, rel, buttons
14 MOUSEBUTTONDOWN mouse down pos, button
 15 MOUSEBUTTONUP mouse release pos, button
 16 JOYAXISMOTION Joystick or pad movement joy, axis, value
 17 JOYBALLMOTION Joy ball? Move joy, axis, value
 18 JOYHATMOTION Joystick? move joy, axis, value
 19 JOYBUTTONDOWN Gamepad press joy, button
 20 JOYBUTTONUP Gamepad release joy, button
 21 VIDEORESIZE Pygame window scaling size, w, h
 22 VIDEOEXPOSE Pygame window partially public (expose) none
 23 USEREVENT triggered a user event code
24 '''
25 __author__ = {'name' : 'Hongten',
26               'mail' : 'hongtenzone@foxmail.com',
27               'blog' : 'http://www.cnblogs.com/hongten',
28               'QQ'   : '648719819',
29               'Version' : '1.0'}
30 
31 BG_IMAGE = 'C:\test\1.gif'
32 
33 pygame.init()
34 screen = pygame.display.set_mode((500, 500), 0, 32)
35 bg = pygame.image.load(BG_IMAGE).convert()
36 
37 x, y = 0, 0
38 move_x, move_y = 0, 0
39 
40 while 1:
41     for event in pygame.event.get():
42         #print(event.type)
43         if event.type == QUIT:
44             exit()
45         if event.type == KEYDOWN:
46             print(event.key)
 47 #event.key returns a numeric value, while K_LEFT,K_UP,K_RIGHT,K_DOWN, etc. are constants.
 48 # They represent a numeric value as well, and these numeric values can be obtained with: print(event.key)
49 # e.g. K_LEFT = 276
50             #   K_UP = 273
 51 # So the following code can be replaced with.
52             #if event.key == 276:
53             #    move_x = -10
54             if event.key == K_LEFT:
55                 move_x = -10
56             elif event.key == K_UP:
57                 move_y = -10
58             elif event.key == K_RIGHT:
59                 move_x = 10
60             elif event.key == K_DOWN:
61                 move_y = 10
62         elif event.type == KEYUP:
63             move_x = 0
64             move_y = 0
65 
66         x += move_x
67         y += move_y
68         #print(x, y)
69 
70         screen.fill((0, 0, 0))
71         screen.blit(bg, (x, y))
72         pygame.display.update()

Reference.

http://eyehere.net/2011/python-pygame-novice-professional-2/

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、Philips 9002 TV If you like the glitness of the picture this is your best choice
2、AI and doctors PK difficult to distinguish highend medical technology popularization or a lot of landing
3、Blockchain wealth has crossed your path before Letz Dog makes you a digital tycoon with seven plus 7000 microcredits for free
4、Will cell phones really be gone in five years
5、The future is here Nannings rail line 5 to be driverless 17 stations identified

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号