Problems with menu in python

0

This is what I have

def Menu():
   #Muestra en pantalla la imagen guardada en Ganag
   screen.blit(Menug, (0,0))
   #Actualizará el contenido de la pantalla entera
   for event in pygame.event.get():
      dificultad =0
      if event.type == pygame.KEYDOWN:
      #"""if event.key==K_SPACE:
          #dificultad =1"""
      if event.key==K_ENTER: 
          dificultad = 2
   pygame.display.flip()
   pygame.time.delay(5000)
   exit()

and to call her I do this

Fondo_Texto()
Menu()
if dificultad == 2:
   Tablero = TABLERO()
   #Guarda el resultado de Dar_Respuesta en solucion
   solucion = Dar_Respuesta()
   #Impreme la solucion
   print solucion
   #Se crea la variable Turno y se iguala a 0

But I get an error as to what difficulty is defined but not used, what am I doing wrong?

Full part:

import pygame, sys, os, random

from pygame.locals import *
#Inicia modo gráfico    
 pygame.init()
#Se crea la ventana y le damos un titulo
screen = pygame.display.set_mode((397,660))
pygame.display.set_caption('Mastermind!')
#Se asignan los colores a utilizar y los guardamos con sus nombres respectivos
Negro = (0,0,0)
Blanco = (255,255,255)
#A la fuente se le da un tipo de escritura y el tamaño
Fuente = pygame.font.SysFont('agencyfb', 24)
#Crea y guarda un nuevo objeto Clock que sirve para controlar el tiempo
clock = pygame.time.Clock()

dificultad =0
def Menu():
   global dificultad
   Nombre_Full = os.path.join('dibujo', 'Fondo_Menu4.png')
   Menug=pygame.image.load(Nombre_Full)
   #Muestra en pantalla la imagen guardada en Ganag
   screen.blit(Menug,(0,0))
   for event in pygame.event.get():
      if event.type == pygame.KEYDOWN:
         if event.key==K_SPACE:
              dificultad =1
        #if event.key==K_ENTER: 
              #dificultad = 2
    pygame.display.flip()
    pygame.time.delay(5000)              

#Carga las imagenes del juego en general data
def Cargar_Imagen(Nombre, colorkey=None): 
   #Carga y guarda las imagenes de la carpeta Diseño
   Nombre_Full = os.path.join('dibujo', Nombre)
  try:....

the rest are several functions and classes that I declare, and from here I start (or try) to call the function menu

#Menu general

#Carga y muestra el texto y la imagen de fondo

Menu()
Fondo_Texto()
#if dificultad == 2:
Tablero = TABLERO()
#Guarda el resultado de Dar_Respuesta en solucion
solucion = Dar_Respuesta()
#Impreme la solucion
print solucion
#Se crea la variable Turno y se iguala a 0
Turno = 0
#En tablero se cargan las casillas,los botonos de colores, las fichas y filas
Tablero.Casilla_Num()
Tablero.Color_Botones()
Tablero.Fichas()
Tablero.Fila_Selec()
#Calcula internamente cuantos milisegundos han transcurrido desde la llamada anterior.
Tiempo = clock.tick(10)
#Contador de los colores elegidos
Color_Num = 0
guessresult = []
#Nos da la posicion del mouse y la guarda en pos
pos = pygame.mouse.get_pos()
#Se le asignan los intentos y los turnos que ha realizado
if dificultad ==2:
   while Turno <= 15:
     for event in pygame.event.get():
       #Si el evento es igual a QUIT se cierra el juego
       if event.type == QUIT:
          sys.exit()
       #SI los turnos son iguales a los intentos permitidos, el jugador pierde    
       elif Turno == 15:
         #Le muestra una imagen diciendo que perdio
         Tablero.Pierde()

What I want to do is save the number of the option that the user chooses and from there execute the difficulty that he asked, now I get him to show me the image menu but he only shows it for a while, he does not let me interact with she and automatically afterwards she goes to the game ...: (

    
asked by Wolf 10.10.2018 в 03:24
source

1 answer

2

What you could do is declare the difficulty variable outside the Menu function and then change it using global within the function.

dificultad=0

def Menu():
   global dificultad
   #Muestra en pantalla la imagen guardada en Ganag
   screen.blit(Menug, (0,0))
   #Actualizará el contenido de la pantalla entera
   for event in pygame.event.get():
      dificultad=0
      if event.type == pygame.KEYDOWN:
      #"""if event.key==K_SPACE:
          #dificultad =1"""
      if event.key==K_ENTER: 
          dificultad=2
   pygame.display.flip()
   pygame.time.delay(5000)
   exit()

Update:

For the other problem, I guess you want the user to see the options and if for example, press SPACE then, the difficulty changes to 1 and from there, execute the code of that difficulty, or the same with the 2, not ?

Well, you could do something like this:

If the difficulty 0 is like the zero difficulty and you want the user to choose a difficulty, you could put an infinite loop until the difficulty is different from 0, which would be:

while(dificultad==0):
       for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key==K_SPACE:
                    dificultad =1
                #if event.key==K_ENTER: 
                    #dificultad = 2
   pygame.display.flip() 
   pygame.time.delay(5000) 

If the last two lines need to be executed in conjunction with the for, then just put them in the appropriate tabulation and, until the user chooses his difficulty, the code of the corresponding difficulty will not be executed (? If I misinterpreted what You wanted to say, tell me and I'm looking for the right solution.

    
answered by 10.10.2018 / 04:56
source