光标pygame周围的框

时间:2018-04-15 12:54:14

标签: python pygame

在我在pygame中制作的小游戏中,我的光标周围总是有一个框,其中所有颜色都反转(例如,如果我将光标放在黑色和红色背景上,光标周围的方形将为白色和青色)。这最初不是问题,因为前几个项目只需要键盘输入,但现在我想制作一个你必须点击很多的游戏,所以它看起来很难看。如何删除此框?我正在使用macOS High Sierra,python3和pygame 1.9.3。

2 个答案:

答案 0 :(得分:4)

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

看着github issue @Keno发布:
bad cursor

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

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

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

也许可以解决该问题。

如果PyGame应用程序未使用鼠标,则仅隐藏光标可能会很有用。

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

但是,如果PyGame通过设置完全透明的光标“隐藏”鼠标,则结果可能仍然是一个完全黑色的正方形。因此,可以将鼠标移到窗口外(或至少移到右下角):

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

我对这个问题有点迷惑不解,最终写了一堆测试用例。

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图片:

finger_cursor_16.png

答案 1 :(得分:0)

只需添加@Kingsley答案即可,无需进行测试和窗口外光标检测,仍然超级hacky,可能比它值得的麻烦更多,但在修复后可能会为您提供帮助。

import sys, pygame, os
from pygame.locals import *

def main():
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((100, 100, 100))

    # http://tobiasahlin.com/blog/common-mac-os-x-lion-cursors/
    custom_cursor = pygame.image.load(os.path.join('yourPath', 'pointer.png')).convert_alpha()      

    # MAIN LOOP:
    while True:
        # EVENTS :
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
             # Press Q to quit
            elif event.type == KEYDOWN and event.key == K_q:
                sys.exit()

        pygame.mouse.set_visible(False)
        screen.fill((100,100,100))
        if pygame.mouse.get_focused():
            screen.blit( custom_cursor, ( pygame.mouse.get_pos() ) )
        pygame.display.flip()


if __name__ == '__main__': main()

enter image description here