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