|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# notes: |
| 4 | +# The Ten Commandments Of Video Game Menus http://kotaku.com/5955855/the-ten-commandments-of-video-game-menus |
| 5 | + |
| 6 | +import pygame |
| 7 | +import sys |
| 8 | + |
| 9 | +class ui(object): |
| 10 | + def __init__(self, screen): |
| 11 | + |
| 12 | + # the main screen, result from: |
| 13 | + # pygame.display.set_mode(...) |
| 14 | + self._screen = screen |
| 15 | + self.keymap = {pygame.K_UP:1, pygame.K_RIGHT:2, pygame.K_DOWN:3, pygame.K_LEFT:4} |
| 16 | + |
| 17 | + self.nextAction = None |
| 18 | + |
| 19 | + self.menus = {} |
| 20 | + |
| 21 | + def interaction(self, eventKey): |
| 22 | + canInteract = False |
| 23 | + |
| 24 | + if eventKey in self.keymap: |
| 25 | + canInteract = True |
| 26 | + nextAction = self.keymap[eventKey] |
| 27 | + else: |
| 28 | + # reset the action if multiple times pressed keys before |
| 29 | + # actually did something |
| 30 | + self.nextAction = None |
| 31 | + |
| 32 | + return canInteract |
| 33 | + |
| 34 | + def addMenu(self, menuKey, menuRows): |
| 35 | + ''' add an menu |
| 36 | + menuKey: unique name of the menu |
| 37 | + menueRows: [ |
| 38 | + {"rowName":"title", "selectable":False, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"UserInterfaceGenerator"}, |
| 39 | + {"rowName":"start", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"Start Game"}, |
| 40 | + {"rowName":"q", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(123,55,255), "text":"QUIT"} |
| 41 | + ] |
| 42 | + ''' |
| 43 | + self.menus[menuKey] = menuRows |
| 44 | + |
| 45 | + def addSimpleMenu(self, menuKey, menuRows, font="MS Comic Sans", size=30, color=(245, 101, 44)): |
| 46 | + ''' add an SIMPLE menu |
| 47 | + menuKey: unique name of the menu |
| 48 | + menueRows: ["You Shall", "NOT PASS"] |
| 49 | + ''' |
| 50 | + menu = [] |
| 51 | + rowCount = 0 |
| 52 | + for row in menuRows: |
| 53 | + menu.append({"rowName":"simple_" + str(rowCount), "selectable":False, "font":font, "fontSize":size, "color":color, "text":row}) |
| 54 | + |
| 55 | + self.menus[menuKey] = menu |
| 56 | + |
| 57 | + def draw(self, menuKey): |
| 58 | + if menuKey in self.menus: |
| 59 | + menuToDraw = self.menus[menuKey] |
| 60 | + |
| 61 | + #print fnt.size(resultText) |
| 62 | + menuRowsCount = len(menuToDraw) |
| 63 | + for i in range(menuRowsCount): |
| 64 | + fontSize = menuToDraw[i]["fontSize"] |
| 65 | + fnt = pygame.font.SysFont(menuToDraw[i]["font"] , fontSize) |
| 66 | + xPos = (self._screen.get_width() / 2) |
| 67 | + yPos = (self._screen.get_height() / 30) * menuRowsCount |
| 68 | + txt = menuToDraw[i]["text"] |
| 69 | + self._screen.blit(fnt.render(txt, 1, menuToDraw[i]["color"]), ( xPos - (fnt.size(txt)[0] / 2), yPos +(i * fontSize))) |
| 70 | + else: |
| 71 | + raise Exception("Error menu '"+ menuKey +"' does not exist") |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + pygame.init() |
| 75 | + screen = pygame.display.set_mode( (1024, 768), 0, 32) |
| 76 | + clock = pygame.time.Clock() |
| 77 | + |
| 78 | + BG_COLOR = (8, 13, 41) |
| 79 | + |
| 80 | + iUi = ui(screen) |
| 81 | + iUi.addMenu("main", [ |
| 82 | + {"rowName":"title", "selectable":False, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"UserInterfaceGenerator"}, |
| 83 | + {"rowName":"start", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(0,0,255), "text":"Start Game"}, |
| 84 | + {"rowName":"q", "selectable":True, "font":"MS Comic Sans", "fontSize":30, "color":(123,55,255), "text":"QUIT"} |
| 85 | + ] |
| 86 | + ) |
| 87 | + |
| 88 | + while True: |
| 89 | + # Limit frame speed to 50 FPS |
| 90 | + time_passed = clock.tick(50) |
| 91 | + screen.fill(BG_COLOR) |
| 92 | + |
| 93 | + for event in pygame.event.get(): |
| 94 | + if event.type == pygame.QUIT: |
| 95 | + sys.exit() |
| 96 | + |
| 97 | + elif event.type == pygame.KEYDOWN: |
| 98 | + if iUi.interaction(event.key): |
| 99 | + print "UI catched this key" |
| 100 | + else: |
| 101 | + print "no UI catch, key pressed:", event.key |
| 102 | + |
| 103 | + if event.key == pygame.K_q: |
| 104 | + sys.exit(0) |
| 105 | + |
| 106 | + iUi.draw("main") |
| 107 | + |
| 108 | + pygame.display.flip() |
0 commit comments