python pygame鼠标点击_Python中pygame的mouse鼠标事件用法实例

本文介绍了Python Pygame中处理鼠标事件的方法,包括获取鼠标按钮状态、位置、移动量等。通过示例代码展示了如何响应鼠标点击和移动,实现窗口背景颜色随鼠标移动变化,并在不同鼠标按键按下时打印相应信息。
摘要由CSDN通过智能技术生成

本文实例讲述了Python中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()

展示的效果:

0ade742279498044d8007e22d39e93bd.png

游戏效果:

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

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

#pygame mouse

import os, pygame

from pygame.locals import *

from sys import exit

from random import *

__author__ = {'name' : 'Hongten',

'mail' : 'hongtenzone@foxmail.com',

'Version' : '1.0'}

if not pygame.font:print('Warning, Can not found font!')

pygame.init()

screen = pygame.display.set_mode((255, 255), 0, 32)

screen.fill((255,255,255))

font = pygame.font.Font('data\\font\\TORK____.ttf', 20)

text = font.render('Cliked Me please!!!', True, (34, 252, 43))

mouse_x, mouse_y = 0, 0

while 1:

for event in pygame.event.get():

if event.type == QUIT:

exit()

elif event.type == MOUSEBUTTONDOWN:

pressed_array = pygame.mouse.get_pressed()

for index in range(len(pressed_array)):

if pressed_array[index]:

if index == 0:

print('Pressed LEFT Button!')

elif index == 1:

print('The mouse wheel Pressed!')

elif index == 2:

print('Pressed RIGHT Button!')

elif event.type == MOUSEMOTION:

#return the X and Y position of the mouse cursor

pos = pygame.mouse.get_pos()

mouse_x = pos[0]

mouse_y = pos[1]

screen.fill((mouse_x, mouse_y, 0))

screen.blit(text, (40, 100))

pygame.display.update()

希望本文所述对大家Python程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pygame,有多种鼠标事件可以使用。首先,通过使用pygame.mouse.get_pressed()方法,我们可以获取鼠标按钮的状态。 我们还可以使用pygame.MOUSEMOTION事件来获取鼠标的坐标位置,通过event.pos来获取当前鼠标在屏幕的坐标位置。 此外,如果我们想要实现鼠标按下时出现一个球,松开时球消失的效果,我们可以使用pygame.MOUSEBUTTONDOWN和pygame.MOUSEBUTTONUP事件来探测鼠标按下和抬起的动作。 在这些事件,我们可以使用pygame.draw.circle()方法来绘制球的图形,并使用pygame.display.update()方法更新显示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Pythonpygamemouse鼠标事件用法实例](https://download.csdn.net/download/weixin_38740391/13776670)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Pygame鼠标事件检测](https://blog.csdn.net/cool99781/article/details/107285247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值