python鼠标碰撞_Python中pygame的mouse鼠标事件用法实例

该博客介绍了如何使用Python的pygame库处理鼠标事件,包括获取鼠标按钮状态、位置、移动量以及设置鼠标可见性等。通过示例代码展示了当鼠标在窗口上移动时改变背景颜色,点击时打印出所按鼠标键的功能,帮助读者理解pygame的mouse模块。
摘要由CSDN通过智能技术生成

'''

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()

展示的效果:

游戏效果:

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

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

#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程序设计有所帮助。

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值