0

我正在尝试制作一个菜单屏幕,其中“新游戏”和“加载游戏”图标周围似乎没有框,直到鼠标悬停在它们上方,此时它们有一个不透明的蓝色框。我还没有在代码中实现鼠标控件,因为我目前无法让蓝色框变得不透明。任何建议都会很棒。目前,当代码运行时屏幕是黑色的,但是删除第 30 行会导致屏幕正常显示,但没有 alpha 值。

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

FPS = 30 # frames per second, the general speed of the program
WINDOWWIDTH = 1000 # size of the window's width in pixels
WINDOWHEIGHT = 600 # size of window's height in pixels

#            R    G    B
WHITE    = (255, 255, 255)
RED      = (255, 0, 0)
BLUEINVIS = (0, 0, 255, 255)
BLUEOP = (0, 0, 255, 128)

# Colors

TEXTCOLOR = WHITE
TEXTBOXCOLOR = BLUEINVIS
HIGHLIGHTCOLOR = BLUEOP

BASICFONTSIZE = 20


def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    DISPLAYSURF = DISPLAYSURF.convert_alpha()
    pygame.display.set_caption('Menu')

    BASICFONT = pygame.font.Font('freesansbold.ttf', 50)


    NEW_SURF, NEW_RECT = makeText('New Game', TEXTCOLOR, TEXTBOXCOLOR, WINDOWWIDTH /     2, WINDOWHEIGHT - (WINDOWHEIGHT * 3 / 8))
    LOAD_SURF, LOAD_RECT = makeText('Load Game', TEXTCOLOR, TEXTBOXCOLOR, WINDOWWIDTH / 2, WINDOWHEIGHT - (WINDOWHEIGHT / 4))

    DISPLAYSURF.convert_alpha()

    while True: # main game loop

        DISPLAYSURF.fill(RED)
        DISPLAYSURF.blit(NEW_SURF, NEW_RECT)
        DISPLAYSURF.blit(LOAD_SURF, LOAD_RECT)


        for event in pygame.event.get(): # event handling loop
            #if event.type == MOUSEBUTTONUP:

                #if NEW_RECT.collidepoint(event.pos):

                #elif LOAD_RECT.collidepoint(event.pos):

            #if newRectObj.collidepoint(event.pos):
                #newSurfaceObj = fontObj.render('New Game', True, TEXTCOLOR, HIGHLIGHTCOLOR)            

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

        pygame.display.update()
        FPSCLOCK.tick(FPS)

def makeText(text, color, bgcolor, x, y):
    # create the Surface and Rect objects for some text.
    textSurf = BASICFONT.render(text, True, color, bgcolor)
    textRect = textSurf.get_rect()
    textRect.center = (x, y)
    return (textSurf, textRect)

if __name__ == '__main__':
    main()
4

2 回答 2

1

利用:surface = surface.convert_alpha()

单独使用surface.convert_alpha()无济于事。convert_alpha() 返回具有 alpha 值的表面。

http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha

例如。:image1 = pygame.image.load("images/person.png").convert_alpha()

于 2013-07-22T14:30:48.573 回答
1

如果我用此代码段替换您的代码,它会在光标悬停时显示您的文本(蓝底白字)(受此SO question启发)。如果需要,我还添加了一些透明度(取消注释set_alpha行):

def main():
    global BASICFONT
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    background=pygame.Surface((DISPLAYSURF.get_rect().width, DISPLAYSURF.get_rect().height))
    background.fill(RED)

    BASICFONT = pygame.font.Font('freesansbold.ttf', 50)

    pygame.display.set_caption('Menu')

    NEW_SURF, NEW_RECT = makeText('New Game', TEXTCOLOR, TEXTBOXCOLOR, WINDOWWIDTH /     2, WINDOWHEIGHT - (WINDOWHEIGHT * 3 / 8))
    LOAD_SURF, LOAD_RECT = makeText('Load Game', TEXTCOLOR, TEXTBOXCOLOR, WINDOWWIDTH / 2, WINDOWHEIGHT - (WINDOWHEIGHT / 4))
    #NEW_SURF.set_alpha(128)
    #LOAD_SURF.set_alpha(128)

    while True: # main game loop

        DISPLAYSURF.blit(background, background.get_rect())
        DISPLAYSURF.blit(NEW_SURF, NEW_RECT)
        DISPLAYSURF.blit(LOAD_SURF, LOAD_RECT)

        for event in pygame.event.get(): # event handling loop
            if event.type == MOUSEMOTION:
                if NEW_RECT.collidepoint(event.pos):
                  NEW_SURF = BASICFONT.render('New Game', True, TEXTCOLOR, HIGHLIGHTCOLOR)
                  #NEW_SURF.set_alpha(255)

                elif LOAD_RECT.collidepoint(event.pos):
                  LOAD_SURF = BASICFONT.render('Load Game', True, TEXTCOLOR, HIGHLIGHTCOLOR)
                  #LOAD_SURF.set_alpha(255)

                else:
                  NEW_SURF = BASICFONT.render('New Game', True, TEXTCOLOR, TEXTBOXCOLOR)
                  LOAD_SURF = BASICFONT.render('Load Game', True, TEXTCOLOR, TEXTBOXCOLOR)
                  #NEW_SURF.set_alpha(128)
                  #LOAD_SURF.set_alpha(128)
于 2013-07-22T15:58:27.843 回答