public
Last active

Pygame text menu, mouse-over triggers highlight effect on menu option

  • Download Gist
pygame-menu-mouseover.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Highlight-able menu in Pygame
#
# To run, use:
# python pygame-menu-mouseover.py
#
# You should see a window with three grey menu options on it. Place the mouse
# cursor over a menu option and it will become white.
 
import pygame
 
class Option:
 
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)
def set_rend(self):
self.rend = menu_font.render(self.text, True, self.get_color())
def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (100, 100, 100)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
 
pygame.init()
screen = pygame.display.set_mode((480, 320))
menu_font = pygame.font.Font(None, 40)
options = [Option("NEW GAME", (140, 105)), Option("LOAD GAME", (135, 155)),
Option("OPTIONS", (145, 205))]
while True:
pygame.event.pump()
screen.fill((0, 0, 0))
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
else:
option.hovered = False
option.draw()
pygame.display.update()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.