Skip to content

Instantly share code, notes, and snippets.

@ohsqueezy
Created May 27, 2012 04:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohsqueezy/2802185 to your computer and use it in GitHub Desktop.
Save ohsqueezy/2802185 to your computer and use it in GitHub Desktop.
Pygame text menu, mouse-over triggers highlight effect on menu option
# 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment