Python中pygame的mouse鼠标事件用法实例
作者:Hongten 发布时间:2021-02-09 12:21:41
标签:Python,pygame
本文实例讲述了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()
展示的效果:
游戏效果:
当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口
会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮
#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程序设计有所帮助。


猜你喜欢
- 区别:xx:公有变量,所有对象都可以访问;xxx:双下划线代表着是系统定义的名字。__xxx:双前置下划线,避免与子类中的属性命名冲突,无法
- Jabob Nielsen提出的10个可用性原则中有一个原则-防止出错(Error Prevention)。官网中有对这一原则的具体解释,这
- 我们在做迁移学习,或者在分割,检测等任务想使用预训练好的模型,同时又有自己修改之后的结构,使得模型文件保存的参数,有一部分是不需要的(don
- 目标是想把在服务器上用pytorch训练好的模型转换为可以在移动端运行的tflite模型。最直接的思路是想把pytorch模型转换为tens
- 1、从数据库表中检索信息实际上,前面我们已经用到了SELECT语句,它用来从数据库表中检索信息。select语句格式一般为:SELECT 检
- 本项目为python项目需要安装python及python的opencv模块:opencv_python-4.0.1-cp37-cp37m-
- 如下所示:'''@author: Jacobpc'''import osimport sys
- 我们主要讲解一下利用Python实现感知机算法。算法一首选,我们利用Python,按照上一节介绍的感知机算法基本思想,实现感知算法的原始形式
- 一图胜“十”言:SQL Server 数据库总结 一个大概的总结 经过一段时间的学习,也对数据库有了一些认识。 数据库基本是由表,关系,操作
- 方法一:def printTheReverseArray(self): list_1 = [1, 2, 3, 4, 5, 6, 7] l
- 列表解析——用来动态地创建列表[expr for iter_var in iterable if cond_expr]例子一:map(lam
- 目录为什么需要分区?分区的策略分区隐患为什么需要分区?当面对巨大的数据表的时候,至少有一件事情是确定的,表太大了以至于每次查询的时候我们没法
- 建表、insert数据create table tmp_login ( user_id int(11) , logi
- 数据库中有user表如下:新建一个Django项目:django-admin.py startproject myDjango<pro
- 需求很简单比如我在做机器学习实验的时候,实验结果的保存路径是'runs/exp'。这样就会出现一个问题:当我第二次运行程序的
- 本文讲解如何用java实现把数据库的数据写入到txt中 并实现类似下载软件的样子在网页中弹出下载.package datatest;impo
- 前记在Python3.7后官方库出现了contextvars模块, 它的主要功能就是可以为多线程以及asyncio生态添加上下文功能,即使程
- 本文实例讲述了Python使用numpy产生正态分布随机数的向量或矩阵操作。分享给大家供大家参考,具体如下:简单来说,正态分布(Normal
- 本文实例讲述了Python多线程结合队列下载百度音乐的方法。分享给大家供大家参考。具体如下:一直想做个下载音乐的脚本,后来决定就拿百度音乐开
- django-debug-toolbar介绍django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试