Guest User

Untitled

a guest
Jul 2nd, 2014
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import pygame
  5.  
  6. pygame.init()
  7.  
  8. WHITE = (255, 255, 255)
  9. RED = (255, 0, 0)
  10. BLACK = (0, 0, 0)
  11.  
  12. class MenuItem(pygame.font.Font):
  13. def __init__(self, text, font=None, font_size=30,
  14. font_color=WHITE, (pos_x, pos_y)=(0, 0)):
  15.  
  16. pygame.font.Font.__init__(self, font, font_size)
  17. self.text = text
  18. self.font_size = font_size
  19. self.font_color = font_color
  20. self.label = self.render(self.text, 1, self.font_color)
  21. self.width = self.label.get_rect().width
  22. self.height = self.label.get_rect().height
  23. self.dimensions = (self.width, self.height)
  24. self.pos_x = pos_x
  25. self.pos_y = pos_y
  26. self.position = pos_x, pos_y
  27.  
  28. def is_mouse_selection(self, (posx, posy)):
  29. if (posx >= self.pos_x and posx <= self.pos_x + self.width) and \
  30. (posy >= self.pos_y and posy <= self.pos_y + self.height):
  31. return True
  32. return False
  33.  
  34. def set_position(self, x, y):
  35. self.position = (x, y)
  36. self.pos_x = x
  37. self.pos_y = y
  38.  
  39. def set_font_color(self, rgb_tuple):
  40. self.font_color = rgb_tuple
  41. self.label = self.render(self.text, 1, self.font_color)
  42.  
  43. class GameMenu():
  44. def __init__(self, screen, items, funcs, bg_color=BLACK, font=None, font_size=30,
  45. font_color=WHITE):
  46. self.screen = screen
  47. self.scr_width = self.screen.get_rect().width
  48. self.scr_height = self.screen.get_rect().height
  49.  
  50. self.bg_color = bg_color
  51. self.clock = pygame.time.Clock()
  52.  
  53. self.funcs = funcs
  54. self.items = []
  55. for index, item in enumerate(items):
  56. menu_item = MenuItem(item, font, font_size, font_color)
  57.  
  58. # t_h: total height of text block
  59. t_h = len(items) * menu_item.height
  60. pos_x = (self.scr_width / 2) - (menu_item.width / 2)
  61. pos_y = (self.scr_height / 2) - (t_h / 2) + (index * menu_item.height)
  62.  
  63. menu_item.set_position(pos_x, pos_y)
  64. self.items.append(menu_item)
  65.  
  66. self.mouse_is_visible = True
  67. self.cur_item = None
  68.  
  69. def set_mouse_visibility(self):
  70. if self.mouse_is_visible:
  71. pygame.mouse.set_visible(True)
  72. else:
  73. pygame.mouse.set_visible(False)
  74.  
  75. def set_keyboard_selection(self, key):
  76. """
  77. Marks the MenuItem chosen via up and down keys.
  78. """
  79. for item in self.items:
  80. # Return all to neutral
  81. item.set_italic(False)
  82. item.set_font_color(WHITE)
  83.  
  84. if self.cur_item is None:
  85. self.cur_item = 0
  86. else:
  87. # Find the chosen item
  88. if key == pygame.K_UP and \
  89. self.cur_item > 0:
  90. self.cur_item -= 1
  91. elif key == pygame.K_UP and \
  92. self.cur_item == 0:
  93. self.cur_item = len(self.items) - 1
  94. elif key == pygame.K_DOWN and \
  95. self.cur_item < len(self.items) - 1:
  96. self.cur_item += 1
  97. elif key == pygame.K_DOWN and \
  98. self.cur_item == len(self.items) - 1:
  99. self.cur_item = 0
  100.  
  101. self.items[self.cur_item].set_italic(True)
  102. self.items[self.cur_item].set_font_color(RED)
  103.  
  104. # Finally check if Enter or Space is pressed
  105. if key == pygame.K_SPACE or key == pygame.K_RETURN:
  106. text = self.items[self.cur_item].text
  107. self.funcs[text]()
  108.  
  109. def set_mouse_selection(self, item, mpos):
  110. """Marks the MenuItem the mouse cursor hovers on."""
  111. if item.is_mouse_selection(mpos):
  112. item.set_font_color(RED)
  113. item.set_italic(True)
  114. else:
  115. item.set_font_color(WHITE)
  116. item.set_italic(False)
  117.  
  118. def run(self):
  119. mainloop = True
  120. while mainloop:
  121. # Limit frame speed to 50 FPS
  122. self.clock.tick(50)
  123.  
  124. mpos = pygame.mouse.get_pos()
  125.  
  126. for event in pygame.event.get():
  127. if event.type == pygame.QUIT:
  128. mainloop = False
  129. if event.type == pygame.KEYDOWN:
  130. self.mouse_is_visible = False
  131. self.set_keyboard_selection(event.key)
  132. if event.type == pygame.MOUSEBUTTONDOWN:
  133. for item in self.items:
  134. if item.is_mouse_selection(mpos):
  135. self.funcs[item.text]()
  136.  
  137. if pygame.mouse.get_rel() != (0, 0):
  138. self.mouse_is_visible = True
  139. self.cur_item = None
  140.  
  141. self.set_mouse_visibility()
  142.  
  143. # Redraw the background
  144. self.screen.fill(self.bg_color)
  145.  
  146. for item in self.items:
  147. if self.mouse_is_visible:
  148. self.set_mouse_selection(item, mpos)
  149. self.screen.blit(item.label, item.position)
  150.  
  151. pygame.display.flip()
  152.  
  153. if __name__ == "__main__":
  154. def hello_world():
  155. print "Hello World!"
  156.  
  157. # Creating the screen
  158. screen = pygame.display.set_mode((640, 480), 0, 32)
  159.  
  160. menu_items = ('Start', 'Quit')
  161. funcs = {'Start': hello_world,
  162. 'Quit': sys.exit}
  163.  
  164. pygame.display.set_caption('Game Menu')
  165. gm = GameMenu(screen, funcs.keys(), funcs)
  166. gm.run()
Advertisement
Add Comment
Please, Sign In to add comment