【Pygame 学习笔记】6.Cursor 鼠标光标

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

制作不易,喜欢的话就点赞收藏+关注吧~

谢谢支持~

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值