0

I am trying to create a menu for a game I'm doing and this is the code for the menu, but when I run it, it won't display the window.

Can you help me find my error so it can display the menu window?

This is my code:

import pygame from pygame.locals import *

class Menu:

def __init__(self, options):
    self.options = options
    self.font = pygame.font.Font("dejavu.ttf", 20)
    self.select = 0
    self.total = len(self.options)
    self.keep_pressing = False

def update(self):

    k = pygame.key.get_pressed()

    if not self.keep_pressing:
        if k[K_UP]:
            self.select -= 1
        elif k[K_DOWN]:
            self.select += 1
        elif k[K_RETURN]:
            title, function = self.options[self.select]
            print "Opciones"%(title)
            function()

    if self.select < 0:
        self.select = 0
    elif self.select > self.total -1:
        self.select = self.total -1

    self.keep_pressing = k[K_UP] or k[K_DOWN]

def texto(self, screen):

    total = self.total
    indice = 0
    option_height = 25
    x=100
    y=100

    for (title, function) in self.options:
        if indice == self.select:
            color = (0, 200, 0)
        else:
            color = (0, 0, 0)

        image = self.font.render(title, 1, color)
        position = (x, y + option_height * indice)
        indice += 1
        screen.blit(image, position)



def opcion_1():
    print "Opcion 1"
def opcion_2():
    print "Opcion 2"
def opcion_3():
    print "Opcion 3"
def opcion_4():
    print "Opcion 4"



if __name__ == '_main_':

    finish = False
    options = [
        ("Opcion 1"),
        ("Opcion 2"),
        ("Opcion 3"),
        ("Opcion 4")
        ]

    pygame.font.init()
    screen = pygame.display.set_mode((400, 400))
    background = pygame.image.load("negro.jpeg").convert()
    menu = Menu(options)

    while not finish:
        for e in pygame.event.get():
            if e.type == QUIT:
                finish = True
        screen.blit(background, (0, 0))
        menu.update()
        menu.texto(screen)

        pygame.display.flip()
        pygame.time.delay(10)
  • You say there are no errors, however it looks to me like the line `for (title, function) in self.options:` should give you a `ValueError` because your definition of options only has the title and not the function. Presumably options should be `options = [("Opcion 1", opcion_1), ...`. Just an idea. – elParaguayo Apr 10 '14 at 08:29
  • You may also need a `pygame.init()` line and not just the `pygame.font.init()` that you currently have. I can't test these ideas at the moment so won't post this as an answer yet. – elParaguayo Apr 10 '14 at 08:30
  • Also, your indenting looks wrong. I assume the functions up to `texto` are part of the `Menu` class but everything else below is outside it. – elParaguayo Apr 10 '14 at 11:18

0 Answers0