围绕光标 pygame 的框 [英] box around cursor pygame

查看:73
本文介绍了围绕光标 pygame 的框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用 pygame 制作的小游戏中,我的光标周围总是有一个框,其中所有颜色都反转(例如,如果我将光标悬停在黑色和红色背景上,光标周围的方块将是白色和青色).这最初不是问题,因为前几个项目只需要键盘输入,但现在我想制作一个你必须点击很多的游戏,所以它看起来很丑陋.怎么去掉这个箱子?我使用的是 macOS High Sierra、python3 和 pygame 1.9.3.

In the little games I made in pygame there's always a box around my cursor in which all colours are reversed (e.g. if I hold my cursor over a black and red background the square around the cursor will be white and cyan). This initially wasn't a problem, since the first few projects only required keyboard inputs, but now I want to make a game where you have to click a lot, so it would look very ugly. How do I remove this box? I am using macOS High Sierra, python3 and pygame 1.9.3.

推荐答案

我把这个作为答案,因为评论太多了.另外,我没有 Mac,所以这个答案是假设.

I'm putting this as an answer, because it's too much for a comment. Also, I don't have a Mac, so this answer is supposition.

查看

黑色背景上有一个白色光标(黑色填充轮廓)的图像.

There's an image of a white cursor (black filled outline), on a black background.

在我看来,随着最新的操作系统升级,MacOS 不再能与 PyGame 使用的任何鼠标光标图像功能正常工作.显然光标外的像素应该是透明的.

It looks to me, that with the latest OS upgrade, MacOS is no longer working correctly with whatever mouse-cursor-image functions PyGame is using. Obviously the pixels outside the cursor should be transparent.

轶事证据(即:谷歌搜索)表明,其他软件在 macOS High Sierra 上也存在光标问题.

Anecdotal evidence (i.e.: a google search) suggests that other software has cursor issues with macOS High Sierra too.

也许可以解决这个问题.

Maybe it's possible to work around the problem.

如果 PyGame 应用程序没有使用鼠标,隐藏光标可能会很有用.

If the PyGame application is not using the mouse, it may be useful to just hide the cursor.

 pygame.mouse.set_visible()  # Perhaps this just gives a black-box though

然而,如果 PyGame 通过设置一个完全透明的光标来隐藏"鼠标,结果可能仍然是一个完全黑色的方块.所以鼠标可以移到窗口外(或至少移到右下角):

However if PyGame is "hiding" the mouse by setting a fully-transparent cursor, the result may still be a completely black square. So the mouse can be moved outside the window (or at least to the bottom-right corner):

w, h = pygame.display.get_surface().get_size() # get window size
pygame.mouse.set_pos( [w, h] )  # move cursor outside window

我对这个问题有点不知所措,最后写了一堆测试用例.

I got a bit carried away with this problem, and ended up writing a bunch of test cases.

import sys
import time
import pygame
from pygame.locals import *

WHITE = 255,255,255
BLUE  = 0,0,60

WINDOW_WIDTH=600
WINDOW_HEIGHT=500

pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.font.init()
clock = pygame.time.Clock()
text_font = pygame.font.Font(None,60)

STARTUP_MS = int(time.time() * 1000.0)  # Epoch time programme started
MANUAL_CURSOR = pygame.image.load('finger_cursor_16.png').convert_alpha()

cursor_test    = 0
time_last_test = 0

while (True):
    NOW_MS = int(time.time() * 1000.0) - STARTUP_MS  # current time in milliseconds-since-start

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # Try different cursor styles
    if ( cursor_test == 4 ):
        text_image  = text_font.render( 'Off-Screen-Cursor', True, WHITE )
        pygame.mouse.set_pos( [ WINDOW_WIDTH, WINDOW_HEIGHT ])
        pygame.mouse.set_visible(True)

    elif ( cursor_test == 3 ):
        text_image  = text_font.render( 'Manual-Cursor', True, WHITE )
        pygame.mouse.set_visible(False)
        # cursor drawn as part of the screen, see below

    elif ( cursor_test == 2 ):
        text_image  = text_font.render( 'Transparent-Cursor', True, WHITE )
        pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
        pygame.mouse.set_visible(True)

    elif ( cursor_test == 1 ):
        text_image  = text_font.render( 'Hidden-Cursor', True, WHITE )
        pygame.mouse.set_visible(False)

    elif ( cursor_test == 0 ):
        text_image  = text_font.render( 'Default-Arrow-Cursor', True, WHITE )
        pygame.mouse.set_visible(True)
        pygame.mouse.set_cursor(*pygame.cursors.arrow)

    # test for 3-seconds each
    if ( NOW_MS - time_last_test > 3000 ):
        pygame.mouse.set_pos( [ WINDOW_WIDTH//2, WINDOW_HEIGHT//2 ])
        time_last_test = NOW_MS
        cursor_test += 1
        if ( cursor_test > 4 ):
            cursor_test = 0

    # Paint the screen
    screen.fill(BLUE)
    # Write the mode
    screen.blit(text_image, ( 0, 0 ))
    # if we're in manual-cursor mode, paint a cursor too
    if (cursor_test == 3):
        screen.blit( MANUAL_CURSOR, ( pygame.mouse.get_pos() ) )

    pygame.display.update()
    clock.tick_busy_loop(60)

我忘记上传 finger_cursor_16.png 图像:

这篇关于围绕光标 pygame 的框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆