Вы должны получить положение мыши в главном приложении. L oop.
Визуализировать каждый текст дважды, один раз черным и один раз красным:
text1 = font.render('SINGLE PLAYER', 1, black)
text2 = font.render('MULTIPLAYER', 1, black)
text3 = font.render('QUIT', 1, black)
text1red = font.render('SINGLE PLAYER', 1, red)
text2red = font.render('MULTIPLAYER', 1, red)
text3red = font.render('QUIT', 1, red)
Использование pygame.Rect
и collidepoint()
, чтобы определить, находится ли мышь на тексте. Нарисуйте красный текст, если мышь находится над текстом, и нарисуйте текст blcok еще. Например:
def drawText(text, textRed, pos):
location = pygame.mouse.get_pos()
if text.get_rect(topleft = pos).collidepoint(location):
screen.blit(textRed, pos)
else:
screen.blit(text, pos)
См. Пример:
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Podmornca')
run = True
black = (0,0,0)
red = (255,0,0)
background = pygame.image.load('meni.png')
clock = pygame.time.Clock()
font = pygame.font.SysFont('bauhaus93', 20)
text1 = font.render('SINGLE PLAYER', 1, black)
text2 = font.render('MULTIPLAYER', 1, black)
text3 = font.render('QUIT', 1, black)
text1red = font.render('SINGLE PLAYER', 1, red)
text2red = font.render('MULTIPLAYER', 1, red)
text3red = font.render('QUIT', 1, red)
def drawText(text, textRed, pos):
location = pygame.mouse.get_pos()
if text.get_rect(topleft = pos).collidepoint(location):
screen.blit(textRed, pos)
else:
screen.blit(text, pos)
def grafika():
clock.tick(60)
screen.blit(background, (0,0))
drawText(text1, text1red, (100, 200))
drawText(text2, text2red, (100, 250))
drawText(text3, text3red, (100, 300))
pygame.display.update()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
grafika()
pygame.quit()