我正在嘗試製作一個菜單屏幕,其中'新遊戲'和'加載遊戲'圖標在它們周圍沒有框,直到鼠標懸停在它們上面,此時它們有一個不透明的藍色框。我還沒有在代碼中實現鼠標控件,因爲我目前無法讓藍色框變得不透明。任何建議都會很棒。當前代碼運行時屏幕顯示爲黑色,但刪除第30行會導致屏幕顯示,但不顯示alpha值。如何向pygame中的表面對象添加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()
感謝您的幫助。這是我第一次在這裏發帖,我沒有失望。我只是想知道我的文本是否有任何方法,並且它的周圍有不同的alpha值。我希望文本始終爲255,如果可能,請更改框。 –