Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème Jeu python + tkinter projet ISN

    17 mai 2014 à 18:27:53

    from tkinter import *
    from math import *
    #page d'accueil
    
    
    #jeu
    def move():
     "déplacement de la balle"
     
     global x1, y1, dx, dy, flag,x,y
    
     if (sqrt(((x - x1 ))*((x - x1))+ ((y - y1))* ((y - y1)) <=150)) :
        x, y = 10, 260
        avance(0, 0)    #collision avec IA
        
    
     x1, y1 = x1 +dx, y1 + dy
    
    
    
     if x1 > 265:
         x1, dx, dy = 265, 20, 10
     if y1 > 210:
         y1, dx, dy = 210, -15, 0
     if x1 < 10:
         x1, dx, dy = 10, 0, -15
     if y1 < 40:
         y1, dx, dy = 40, 15, 0
    
    
    
     
     can1.coords(oval2,x1,y1,x1+15,y1+15)
     
     if flag >0: 
    
         fen.after(50,move)
    
    
    def move2():
     "déplacement de la balle apres gagné"
     
     global x1, y1, dx, dy, flag,x,y
    
     if (sqrt(((x - x1 ))*((x - x1))+ ((y - y1))* ((y - y1)) <=150)) :
        x, y = 10, 260
        avance(0, 0)
        
    
     x1, y1 = x1 +dx, y1 + dy
    
    
    
     if x1 >265:
         x1, dx, dy = 265, 20, 10
     if y1 >210:
         y1, dx, dy = 210, -15, 0
     if x1 <10:
         x1, dx, dy = 10, 0, -15
     if y1 <40:
         y1, dx, dy = 40, 15, 0
     can1.coords(oval2,x1,y1,x1+25,y1+25)
     
     if flag >0: 
    
         fen.after(50,move)
    
    
    
    def move3():
     "déplacement de la balle du TP"
     global x3, y3, dx, dy, flag,x,y
    
     if (sqrt(((x - x1 ))*((x - x1))+ ((y - y1))* ((y - y1)) <=150)) :
        x, y = 10, 260
        avance(0, 0)
        
    
     x3, y3 = x3 +dx, y3 + dy
    
    
    
     if x3 >265:
         x3, dx, dy = 265, 20, 10
     if y3 >210:
         y3, dx, dy = 210, -15, 0
     if x3 <10:
         x3, dx, dy = 10, 0, -15
     if y3 <40:
         y3, dx, dy = 40, 15, 0
     can1.coords(oval3,x3,y3,x3+25,y3+25)
     
     if flag >0: 
    
         fen.after(50,move)
    
    def stop_it():
        "arrêt de l'animation"
        global flag
        flag =0
        x == 0 and y == 0
        
    
    def start_it():
         "démarrage de l'animation"
         global flag
         
         flag = 1
         move()
         move3()     
    
    
    
    
    
    def avance(a, b):
        global x, y, oval1, flag
       
        if flag == 1 :
        
            if y+b < 8 or y+b > 275 :
                y=y
            else :
                y=y+b
    
            if x+a < 8 or x+a > 268:
                x = x
            else :
                x = x+a
    
            if y + b < 30 and x + a > 248 :
                x, y = 10, 260
                move2()
    
    
    
          
    
            if x + a > 258 and y + b > 262 :
                x, y = 88, 12
                
                
          
        
            can1.coords(oval1, x,y, x+15,y+15)
    
     
    
    def gauche(event):
        avance(-5, 0)
    def droite(event):
        avance(5, 0)
    def haut(event):
        avance(0, -5)
    def bas(event):
        avance(0, 5)
    
    x3, y3 = 250, 260  
    x1, y1 = 50, 60 
    dx, dy = 50, 0
    
    flag =0 
    x, y = 10, 260
    
    
    
    fen=Tk()
    fen.title("Exercice d'animation avec tkinter")
    
    can1 = Canvas(fen,bg='white',height=300,width=290)
    oval1 = can1.create_oval(x,y,x+15,y+15,width=2,fill='white')
           
    can1.coords(oval1, x,y, x+15,y+15)
    can1.pack(side=LEFT)
    
     
    can1.create_rectangle(1,1,10,300,fill="black")
    can1.create_rectangle(11,1,280,10,fill="black")
    can1.create_rectangle(281,1,290,300,fill="black")
    can1.create_rectangle(11,300,280,290,fill="black")
    #murs
    
    can1.create_rectangle(265,275,280,290,fill="yellow")
    can1.create_rectangle(80,10,95,25,fill="yellow")
    #Teleporteurs
    
    can1.create_rectangle(250,10,280,30,fill="green")
    can1.create_rectangle(10,270,40,290,fill="red")
    #maisons
    
    oval2 = can1.create_oval(x1, y1, x1+15, y1+15, width=3, fill='red')
    oval3 = can1.create_oval(x3, y3, x3+15, y3+15, width=3, fill='blue')
    
    bou2 = Button(fen, text='Démarrer', width =8, command=start_it)
    bou2.pack()
    bou3 = Button(fen, text='Pause', width =8, command=stop_it)
    bou3.pack()
    bouton_quit = Button(fen,text='Quitter',command=fen.destroy)
    bouton_quit.pack()
    fen.bind_all('<Up>',haut)
    fen.bind_all('<Down>',bas)
    fen.bind_all('<Right>',droite)
    fen.bind_all('<Left>',gauche)
    
    
    fen.mainloop()
    
    

    Bonjour, 
    Je vous explique brièvement mon problème.
    Ce programme est un jeu ou le but est de dirigé notre personnage dans une certaine zone pour accéder au "niveau" suivant (qui en fait correspond a un accéleration du parcours de l'IA, pour plus de difficultés.
    En fait, c'est tout con, j'ai définit plusieurs fonctions move. move() est celui de base,  celui de l'IA. move() correspond a l'accélaration (= niveau suivant) et move3() celui du second bot
    Celles qui m'intéresse sont move() et move3(). Je les lances dans ma fonction "start_it".
    Le problème étant que le move() fonctionne correctement, mais le move3() ne marche pas du tout, et je ne voit absolument pas pourquoi..
    Des idées ?
    • Partager sur Facebook
    • Partager sur Twitter
      17 mai 2014 à 18:29:19

      *move2() correspond a l'accélaration (= niveau suivant)
      • Partager sur Facebook
      • Partager sur Twitter
        17 mai 2014 à 22:00:05

        Personne ? Ne soyez pas effrayé par la longueur du code ^^ L'erreur se trouve surement dans le def move3() (ligne 70) ou le start_it (ligne 104, c'est la que je lance le move3() qui ne veut pas fonctionner..)
        • Partager sur Facebook
        • Partager sur Twitter
          17 mai 2014 à 22:25:16

          tu souhaites lancer deux fonction en même temps, non?
          • Partager sur Facebook
          • Partager sur Twitter
          Bevet Breizh! Breizh dizalc'h! Betek an trec'h! Ha mallozh ruz d'ar c'hallaoued! Trouvez votre voie
            18 mai 2014 à 10:58:45

            Oui, mais une seule marche..
            • Partager sur Facebook
            • Partager sur Twitter
              18 mai 2014 à 11:04:26

              il te faut pour cela utiliser la technique des threads.

              • Partager sur Facebook
              • Partager sur Twitter
              Bevet Breizh! Breizh dizalc'h! Betek an trec'h! Ha mallozh ruz d'ar c'hallaoued! Trouvez votre voie
                18 mai 2014 à 11:26:00

                Hmm j'ai du mal a comprendre comment ça marche étant donné que c'est la première fois que j'en entends parler.. Un exemple d'utilisation ? (phyton 3.3)
                • Partager sur Facebook
                • Partager sur Twitter
                  18 mai 2014 à 11:37:36

                  je t'ai donné un lien concret.

                  import threading 
                    
                  def func1(): 
                      #faire qqch
                  
                  def func2(): 
                      #faire qqch
                    
                  a = threading.Thread(None, func1) 
                  b = threading.Thread(None, func2) 
                  a.start() 
                  b.start()

                  -
                  Edité par pythan 18 mai 2014 à 11:38:33

                  • Partager sur Facebook
                  • Partager sur Twitter
                  Bevet Breizh! Breizh dizalc'h! Betek an trec'h! Ha mallozh ruz d'ar c'hallaoued! Trouvez votre voie
                    18 mai 2014 à 12:02:35

                    D'abord, merci de tes réponses ! 

                    Mais oui j'avais bien suivi ton lien et j'avais fait exactement la même chose que sur ton précédent post, mais sans plus de résultats..

                    Il n'y a rien de spécial a ajouter dans mes def move() a ce niveau la ? Parce que la je sèche complètement, j'en suis au point d'essayer de trouver un moyen de ne pas les lancers via la même commande, mais j'ai quelques bugs..

                    • Partager sur Facebook
                    • Partager sur Twitter

                    Problème Jeu python + tkinter projet ISN

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown