Advertisement
cookertron

Python Pygame Simple Menu

Apr 23rd, 2021
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import sys
  2. import pygame
  3.  
  4. BLACK = (0, 0, 0)
  5. WHITE = (255, 255, 255)
  6. RED = (128, 0, 0)
  7. GREEN = (0, 128, 0)
  8. BLUE = (0, 0, 128)
  9.  
  10. pygame.init()
  11. PDR = pygame.Rect(0, 0, 1024, 720)
  12. PDS = pygame.display.set_mode(PDR.size)
  13. FPS = 120
  14.  
  15. pygame.font.init()
  16. FONT = pygame.font.SysFont('Ariel', 35)
  17.  
  18. class menu:
  19.     class item:
  20.         def __init__(s, text, identifier = None):
  21.             global FONT, WHITE, BLACK, RED
  22.  
  23.             s.id = identifier
  24.             s.text = text
  25.             if id:
  26.                 s.highlighted_surface = FONT.render(text, True, WHITE, RED)
  27.             s.unhighlighted_surface = FONT.render(text, True, WHITE, BLACK)
  28.             s.rect = s.unhighlighted_surface.get_rect()
  29.  
  30.     def __init__(s):
  31.         s.itemContainer = []
  32.         s.highlightableContainer = []
  33.  
  34.         s.selected = None
  35.  
  36.         s.menuHeight = 0
  37.         test = s.item("TEST")
  38.         s.itemHeight = test.rect.h
  39.         del test
  40.  
  41.         s.keyDown = False
  42.  
  43.     def addItem(s, text, identifier = None):
  44.         newItem = s.item(text, identifier)
  45.        
  46.         s.itemContainer.append(newItem)
  47.        
  48.         if identifier:
  49.             s.highlightableContainer.append(newItem)
  50.        
  51.         s.menuHeight += s.itemHeight
  52.    
  53.     def draw(s):
  54.         global PDS, PDR
  55.  
  56.         y = (PDR.h - s.menuHeight) / 2
  57.        
  58.         for item in s.itemContainer:
  59.             x = (PDR.w - item.rect.w) / 2
  60.  
  61.             if item == s.highlightableContainer[0]:
  62.                 PDS.blit(item.highlighted_surface, (x, y))
  63.             else:
  64.                 PDS.blit(item.unhighlighted_surface, (x, y))
  65.  
  66.             y += s.itemHeight
  67.  
  68.     def update(s):
  69.         if not s.highlightableContainer: return
  70.  
  71.         k = pygame.key.get_pressed()
  72.         if k[pygame.K_UP] and not s.keyDown:
  73.             s.highlightableContainer = [s.highlightableContainer[-1]] + s.highlightableContainer[:-1]
  74.             s.keyDown = True
  75.         elif k[pygame.K_DOWN] and not s.keyDown:
  76.             s.highlightableContainer = s.highlightableContainer[1:] + [s.highlightableContainer[0]]
  77.             s.keyDown = True
  78.         elif not k[pygame.K_UP] and not k[pygame.K_DOWN] and s.keyDown:
  79.             s.keyDown = False
  80.        
  81.         if k[pygame.K_SPACE] or k[pygame.K_RETURN]:
  82.             s.selected = s.highlightableContainer[0]
  83.  
  84.  
  85. m = menu()
  86. m.addItem("M E N U")
  87. m.addItem(" ")
  88. m.addItem("LET'S DO THIS", "PLAY")
  89. m.addItem("WIMP OUT!", "EXIT")
  90. m.addItem(" ")
  91. m.addItem("OPTIONS", "OPTIONS")
  92.  
  93. while True:
  94.     events = pygame.event.get()
  95.     for e in events:
  96.         if e.type == pygame.QUIT: sys.exit()
  97.  
  98.     PDS.fill(BLACK)
  99.     m.draw()
  100.     m.update()
  101.     if m.selected:
  102.         if m.selected.id == "EXIT": break
  103.  
  104.     pygame.display.update()
  105.     pygame.time.Clock().tick(FPS)
  106.  
  107. pygame.quit()
  108. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement