Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: MisterPython en 21 Octubre 2020, 10:49 am



Título: Juego en python que no me funciona
Publicado por: MisterPython en 21 Octubre 2020, 10:49 am
Hola buenas estaba haciendo un juego, el cual tiene unos botones, usando la biblioteca tkinter y al ejecutarlo y pulsar alguno de los botones me decia que el programa no me respondia.

El codigo es el siguiente:




import time
import random
from tkinter import Tk, Text, END, Button

class Pantalla:

    def __init__(self):
        self.ventana=Tk()
        self.ventana.title('Snake Game')
        self.ventana.config(bg='OrangeRed')
        self.ventana.config(height=300,width=400)
        self.areajueg=Text (state='disabled',height=10,width=30,background='black',foreground='orange',font=('System',15))
        self.areajueg.grid(row=0,column=0,rowspan=2,pady=5,padx=5)

       
       
        self.areabot=Text(state='disabled',height=5,width=35,background='OrangeRed')
        self.areabot.grid(row=2,column=0,rowspan=2)
       
        self.xsnk=10
        self.ysnk=10
        self.snake=self.crearBoton(':)','No',back='OrangeRed',ancho=1)
        self.snake.place(x=self.xsnk,y=self.ysnk)
       
        food=self.crearBoton(u'\u2620','No',ancho=2,back='silver',fore='Maroon',fuente=('System',15))
        food.place(x=random.randrange(9,251),y=random.randrange(9,171))
       
       
        botonder=self.crearBoton(u'\u2192','Der',ancho=7,alto=2)
        botonizq=self.crearBoton(u'\u2190','Izq',ancho=7,alto=2)
        botonarrb=self.crearBoton(u'\u2191','Arr')
        botonabaj=self.crearBoton(u'\u2193','Abj')
        botonder.place(x=180,y=230)
        botonizq.place(x=20,y=230)
        botonarrb.place(x=105,y=220)
        botonabaj.place(x=105,y=260)
   
       
       
    def crearBoton(self,valor,TFlch,ancho=6,alto=1,back='white',fore='black',fuente=('System',15)):
        return Button(self.ventana,text=valor,width=ancho,height=alto,background=back,foreground=fore,font=fuente,command=lambda:self.click(TFlch))

   

    def click(self,texto):

        #Boton flecha Der
        while texto == 'Der':
            self.xsnk+=1
            self.snake.place(x=self.xsnk,y=self.ysnk)
            time.sleep(1)

            if not texto == 'Der':
                break

        #Boton Flecha Izq
        while texto == 'Izq':
            self.xsnk-=1
            self.snake.place(x=self.xsnk,y=self.ysnk)
            time.sleep(1)

            if not texto == 'Izq':
                break

        #Boton Flecha Arrb
        while texto == 'Arr':
            self.ysnk-=1
            self.snake.place(x=self.xsnk,y=self.ysnk)
            time.sleep(1)
            if not texto == 'Arr':
                break


        while texto == 'Abj':
            self.ysnk+=1
            self.snake.place(x=self.xsnk,y=self.ysnk)
            time.sleep(1)

            if not texto == 'Abj':
                break
           
           
pantalla=Pantalla()





Gracias



Título: Re: Juego en python que no me funciona
Publicado por: tincopasan en 22 Octubre 2020, 00:52 am
hola:
        deberías ver como trabajar con clases y tkinter, el código en general es muy feo, pero bueno, más allá de eso probá con:

Código
  1. def click(self,texto):
  2.        #Boton flecha Der
  3.        if texto == 'Der':
  4.            self.xsnk+=1
  5.            self.snake.place(x=self.xsnk,y=self.ysnk)
  6.         #Boton Flecha Izq
  7.        elif texto == 'Izq':
  8.            self.xsnk-=1
  9.            self.snake.place(x=self.xsnk,y=self.ysnk)
  10.  
  11.       #etc.    
  12.  
   

para ver si así hace medianamente lo que esperas que haga.