cool hit counter pygame series_mouse mouse event_Intefrankly

pygame series_mouse mouse event


pygame.mouse Provides some methods to get the current state of the mouse device

'''
pygame.mouse.get_pressed - get the state of the mouse buttons    get the state of the mouse buttons
pygame.mouse.get_pos - get the mouse cursor position    get the mouse cursor position
pygame.mouse.get_rel - get the amount of mouse movement    get the amount of mouse movement
pygame.mouse.set_pos - set the mouse cursor position    set the mouse cursor position
pygame.mouse.set_visible - hide or show the mouse cursor    hide or show the mouse cursor
pygame.mouse.get_focused - check if the display is receiving mouse input    check if the display is receiving mouse input
pygame.mouse.set_cursor - set the image for the system mouse cursor    set the image for the system mouse cursor
pygame.mouse.get_cursor - get the image for the system mouse cursor    get the image for the system mouse cursor
'''

In the following demo, the main use of :

pygame.mouse.get_pressed()

pygame.mouse.get_pos()

Effect of display.

Game Effect.

When the mouse passes over the window, the window background color will change with the mouse movement, and when the mouse clicks on the window

It will print in the console that the mouse button was clicked: left, right, scroll wheel

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

Code section.

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

 1 #pygame mouse
 2 
 3 import os, pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 from random import *
 7 
 8 __author__ = {'name' : 'Hongten',
 9               'mail' : 'hongtenzone@foxmail.com',
10               'blog' : 'http://www.cnblogs.com/hongten',
11               'Version' : '1.0'}
12 
13 if not pygame.font:print('Warning, Can not found font!')
14 
15 pygame.init()
16 
17 screen = pygame.display.set_mode((255, 255), 0, 32)
18 screen.fill((255,255,255))
19 
20 font = pygame.font.Font('data\font\TORK____.ttf', 20)
21 text = font.render('Cliked Me please!!!', True, (34, 252, 43))
22 
23 mouse_x, mouse_y = 0, 0
24 while 1:
25     for event in pygame.event.get():
26         if event.type == QUIT:
27             exit()
28         elif event.type ==  MOUSEBUTTONDOWN:
29             pressed_array = pygame.mouse.get_pressed()
30             for index in range(len(pressed_array)):
31                 if pressed_array[index]:
32                     if index == 0:
33                         print('Pressed LEFT Button!')
34                     elif index == 1:
35                         print('The mouse wheel Pressed!')
36                     elif index == 2:
37                         print('Pressed RIGHT Button!')
38         elif event.type == MOUSEMOTION:
39             #return the X and Y position of the mouse cursor
40             pos = pygame.mouse.get_pos()
41             mouse_x = pos[0]
42             mouse_y = pos[1]
43       
44     screen.fill((mouse_x, mouse_y, 0))       
45     screen.blit(text, (40, 100))
46     pygame.display.update()

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


Recommended>>
1、Computer Level 2 Public Fundamentals Exam Syllabus
2、As important as the picture is to show off the thinking is king GACEncapsule Story Sharing
3、Take a look at Googles 10 most successful products
4、NASA artificial intelligence discovers a second solar system
5、without accumulating steps you cannot achieve a thousand miles

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

    已发送

    朋友将在看一看看到

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

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号