python pygame鼠标点击_pygame系列_mouse鼠标事件

pygame.mouse提供了一些方法获取鼠标设备当前的状态

'''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'''

在下面的demo中,主要用到了:

pygame.mouse.get_pressed()

pygame.mouse.get_pos()

展示的效果:

游戏效果:

当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口

会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮

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

代码部分:

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

1 #pygame mouse

2

3 importos, pygame4 from pygame.locals import *

5 from sys importexit6 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, 024 while 1:25 for event inpygame.event.get():26 if event.type ==QUIT:27 exit()28 elif event.type ==MOUSEBUTTONDOWN:29 pressed_array =pygame.mouse.get_pressed()30 for index inrange(len(pressed_array)):31 ifpressed_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()

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

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。

Hongten博客排名在100名以内。粉丝过千。

Hongten出品,必是精品。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值