当前位置:网站首页>【Pygame 学习笔记】6.Cursor 鼠标光标
【Pygame 学习笔记】6.Cursor 鼠标光标
2022-07-17 17:13:00 【Leleprogrammer】
Pygame 提供对系统硬件光标的控制。Pygame 支持黑白光标(位图光标),以及系统变体光标和彩色光标。
此光标模块包含用于加载和解码各种光标格式的函数。这些允许轻松地将光标存储在外部文件中或直接作为编码的 Python 字符串。
该模块包括几个标准光标,这些光标的参数都存储在一个元组中。
这次,我们来学习Pygame中对光标的操作pygame.cursor
在Pygame的官方文档中,我们可以了解到导入光标有三种方法:
pygame.cursors.compile # 从简单字符串创建二进制光标数据
pygame.cursors.load_xbm # 从 XBM 文件加载光标数据
pygame.cursors.Cursor # 代表光标的pygame对象
我们依旧先给出初始框架:
import pygame
from pygame.locals import *
import sys
class Game:
def __init__(self):
pygame.init()
self.W,self.H=800,800
self.screen=pygame.display.set_mode((self.W,self.H))
pygame.display.set_caption("【Pygame 学习笔记】")
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
def draw(self):
self.screen.fill((255,255,255))
def run(self):
while True:
self.listen()
self.draw()
pygame.display.update()
if __name__ == '__main__':
game=Game()
game.run()
pygame.cursors下有几个标准光标如下:
pygame.cursors.arrow
pygame.cursors.diamond
pygame.cursors.broken_x
pygame.cursors.tri_left
pygame.cursors.tri_right
示例如下:
pygame.mouse.set_cursor(pygame.cursors.arrow)
pygame.mouse.set_cursor(pygame.cursors.diamond)
pygame.mouse.set_cursor(pygame.cursors.broken_x)
pygame.mouse.set_cursor(pygame.cursors.tri_left)
pygame.mouse.set_cursor(pygame.cursors.tri_right)
该模块还包含一些作为格式化字符串的光标。需要先将它们传递给pygame.cursors.compile()
函数,然后才能使用它们。
pygame.cursors.textmarker_strings
pygame.cursors.textmarker_strings
pygame.cursors.sizer_x_strings
pygame.cursors.sizer_y_strings
pygame.cursors.sizer_xy_strings
打字时的一个“I”光标
cursor=pygame.cursors.compile(pygame.cursors.textmarker_strings)
pygame.mouse.set_cursor((8,16),(0,0),*cursor)
沿着x坐标拖动窗口时的光标
cursor=pygame.cursors.compile(pygame.cursors.sizer_x_strings)
pygame.mouse.set_cursor((24,16),(0,0),*cursor)
沿着y坐标拖动窗口时的光标
cursor=pygame.cursors.compile(pygame.cursors.sizer_y_strings)
pygame.mouse.set_cursor((16,24),(0,0),*cursor)
沿着xy坐标拖动窗口时的光标(处于窗口角落时的光标)
cursor=pygame.cursors.compile(pygame.cursors.sizer_xy_strings)
pygame.mouse.set_cursor((24,16),(0,0),*cursor)
注意,这四种光标都是由一个元组组成,元组中是若干个字符串,我们把字符串也看成数组的话,它就是一个二维数组,所以set_cursor后面传入的第一个元组就是这个“二维数组”的大小,可以用len方法和索引,直接查看这些元组,自己也可以像它一样,创建自己的光标
官方文档中对这个元组的解释如下:
thickarrow_strings = ( #sized 24x24 "XX ", "XXX ", "XXXX ", "XX.XX ", "XX..XX ", "XX...XX ", "XX....XX ", "XX.....XX ", "XX......XX ", "XX.......XX ", "XX........XX ", "XX........XXX ", "XX......XXXXX ", "XX.XXX..XX ", "XXXX XX..XX ", "XX XX..XX ", " XX..XX ", " XX..XX ", " XX..XX ", " XXXX ", " XX ", " ", " ", " ")
本次学习笔记的参考代码如下:
import pygame
from pygame.locals import *
import sys
class Game:
def __init__(self):
pygame.init()
self.W,self.H=800,800
self.screen=pygame.display.set_mode((self.W,self.H))
pygame.display.set_caption("【Pygame 学习笔记】")
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
def draw(self):
self.screen.fill((255,255,255))
# pygame.mouse.set_cursor(pygame.cursors.arrow)
# cursor=pygame.cursors.compile(pygame.cursors.textmarker_strings)
# pygame.mouse.set_cursor((8,16),(0,0),*cursor)
# cursor=pygame.cursors.compile(pygame.cursors.sizer_x_strings)
# pygame.mouse.set_cursor((24,16),(0,0),*cursor)
# cursor=pygame.cursors.compile(pygame.cursors.sizer_y_strings)
# pygame.mouse.set_cursor((16,24),(0,0),*cursor)
cursor=pygame.cursors.compile(pygame.cursors.sizer_xy_strings)
pygame.mouse.set_cursor((24,16),(0,0),*cursor)
def run(self):
while True:
self.listen()
self.draw()
pygame.display.update()
if __name__ == '__main__':
game=Game()
game.run()
制作不易,喜欢的话就点赞收藏+关注吧~
谢谢支持~
边栏推荐
- 国产API工具哪家强?原来是它…
- 音频常见端子剖析图---再也不会搞错了
- Google developer community sharing - flutter animation sharing has been released
- Opencv:06 morphology
- 云犀聚焦店播解决方案,加速全球化布局
- 脉冲函数、阶跃函数和斜坡函数及脉冲响应
- Visual ETL tool kettle concept, installation and practical cases
- 超声波传感器(CHx01) 学习笔记 Ⅲ - I2C读写操作
- MOF customized material | NH (2) -uio66/rgo Graphene Oxide Nanocomposite | methylene blue loaded zif-90 nanoparticles
- 35岁以上的测试/开发程序员职业生涯走向,是职场转折点吗?
猜你喜欢
MOF customized materials | ultra thin MOF nanobelts | magnetic Fe3O4 @cd-mof nanocomposites | zif-8/ graphene composite nanoparticles
Opencv:06 morphology
AE how to make Nebula particle special effects
Label ball problem
超声波传感器(CH101&ch201) - Ⅰ
Visual ETL tool kettle concept, installation and practical cases
动态内存规划
可视化ETL工具Kettle概念、安装及实战案例
MOF customized product | n-k2ti4o9/g-c3n4/uio-66 ternary composite | paper based au-aginse2-zif-8 Nanocomposite
ASP. Net collaborative OA office service management platform source code
随机推荐
可视化ETL工具Kettle概念、安装及实战案例
Can you view MySQL data table structure in two ways?
AE how to make Nebula particle special effects
MOF customized materials | ultra thin MOF nanobelts | magnetic Fe3O4 @cd-mof nanocomposites | zif-8/ graphene composite nanoparticles
Database daily question --- day 25: bank account summary II
脉冲函数、阶跃函数和斜坡函数及脉冲响应
Acwing785. 快速排序
Possible problems in inserting Excel data into MySQL database
虞美人·寄公度
Acwing786. The kth number
506. Relative ranking
About the "bottom reading" mentality, it makes you exhausted 2020-03-15
useEffect 总结
[C language programming 7] BTB model
2022年最新吉林建筑安全员模拟题库及答案
PostgreSQL 函数使用记录
深度梳理:机器学习建模调参方法总结
Installation and use of MySQL under Linux
Mycat2 builds MySQL master-slave separation
基于STM32设计的云端健康管理系统(采用阿里云物联网平台)