Partage
  • Partager sur Facebook
  • Partager sur Twitter

Bouton "rejouer" avec Tkinter

    22 mai 2018 à 3:39:06

    Bonsoir.

    Je programme un jeu de plateforme et j'aimerais créer un bouton rejouer, dans la fenêtre de fin du jeu (fenFin), qui permette de relancer la fenêtre principale (fen). En traînant sur quelques forums, j'ai cru comprendre qu'il fallait associer une certaine fonction au bouton rejouer, mais je n'ai pas vraiment réussi (j'ai un niveau de débutant).

    Merci d'avance pour votre aide ! :) 

    #Importation de la bibliothèque graphique
    from tkinter import *
    from time import *
    
    #Fonctions permettant de gérer le saut du personnage
    def haut():
            global PosX,PosY,compteur,image
            PosY = PosY - 4
            compteur = compteur + 1
            if compteur < 80:
                fen.after(5,haut)
            Canvas.delete(image)
            image =Canvas.create_image(PosX,PosY,image=photo)
    
    def bas():
            global PosX,PosY,compteur,image,chute
            if PosX > 35 and PosX < 302:
                if PosY < 540:
                    PosY = PosY + 2
                if PosY >= 540:
                    compteur = 0
            if PosX > 301 and PosX < 360:
                if PosY < 481:
                    PosY = PosY + 2
                if PosY >= 481:
                    compteur = 0
            if PosX > 359 and PosX < 485:
                if PosY < 424:
                    PosY = PosY + 2
                if PosY >= 424:
                    compteur = 0
            if PosX > 484 and PosX < 538:
                if PosY < 540:
                    PosY = PosY + 2
                if PosY >= 540:
                    compteur = 0
            if PosX > 537 and PosX < 669:
                if PosY < 368:
                    PosY = PosY + 2
                if PosY >= 368:
                    compteur = 0
            if PosX > 668 and PosX < 726:
                if PosY < 481:
                    PosY = PosY + 2
                if PosY >= 481:
                    compteur = 0
            if PosX > 725 and PosX < 1288:
                if PosY < 540:
                    PosY = PosY + 2
                if PosY >= 540:
                    compteur = 0
            chute = 1
            fen.after(5,bas)
            Canvas.delete(image)
            image=Canvas.create_image(PosX,PosY,image=photo)
    
    
    
    #Fonction permettant de gérer l'appui sur les touches du clavier (ici z, q et d)
    def Clavier(event):
        global PosX,PosY,image     # Variables définies globalement sur tout le programme (portée globale),
                                   # car elles ne peuvent être passées en argument dans cette fonction
    
        touche = event.keysym   #Attente de l'action sur une touche
    
        # déplacement vers le haut
        if touche == 'z':
            if compteur == 0 :
                haut()
                if chute == 0:
                    bas()
    
        # déplacement vers la droite
        if touche == 'd':
            if PosY <= 540:
                if PosX >= 37 and PosX < 301:
                    PosX = PosX + 10
                    if PosX >= 302:
                        PosX = 301
                if PosX >= 485 and PosX < 537:
                    PosX = PosX + 10
                    if PosX >= 538:
                        PosX = 537
                if PosX >= 727 and PosX < 1287:
                    PosX = PosX + 10
                    if PosX >= 1288:
                        PosX = 1287
            if PosY <= 482:
                if PosX >= 37 and PosX < 359:
                    PosX = PosX + 10
                    if PosX >= 360:
                        PosX = 359
                if PosX >= 485 and PosX < 537:
                    PosX = PosX + 10
                    if PosX >= 538:
                        PosX = 537
                if PosX >= 669 and PosX < 1287:
                    PosX = PosX + 10
                    if PosX >= 1288:
                        PosX = 1287
            if PosY <= 424:
                if PosX >= 37 and PosX < 537:
                    PosX = PosX + 10
                    if PosX >= 538:
                        PosX = 537
                if PosX >= 669 and PosX < 1287:
                    PosX = PosX + 10
                    if PosX >= 1288:
                        PosX = 1287
            if PosY <= 368:
                if PosX >= 37 and PosX < 1287:
                    PosX = PosX + 10
                    if PosX >= 1288:
                        PosX = 1287
    
        # déplacement vers la gauche
        if touche == 'q':
            if PosY <= 540:
                if PosX > 37 and PosX <= 301:
                    PosX = PosX - 10
                    if PosX <= 36:
                        PosX = 37
                if PosX > 485 and PosX <= 537:
                    PosX = PosX - 10
                    if PosX <= 484:
                        PosX = 485
                if PosX > 727 and PosX <= 1287:
                    PosX = PosX - 10
                    if PosX <= 726:
                        PosX = 727
            if PosY <= 482:
                if PosX > 37 and PosX <= 359:
                    PosX = PosX - 10
                    if PosX <= 36:
                        PosX = 37
                if PosX > 485 and PosX <= 537:
                    PosX = PosX - 10
                    if PosX <= 484:
                        PosX = 485
                if PosX > 669 and PosX <= 1287:
                    PosX = PosX - 10
                    if PosX <= 668:
                        PosX = 669
            if PosY <= 424:
                if PosX > 37 and PosX <= 537:
                    PosX = PosX - 10
                    if PosX <= 36:
                        PosX = 37
                if PosX > 669 and PosX <= 1287:
                    PosX = PosX - 10
                    if PosX <= 668:
                        PosX = 669
            if PosY <= 368:
                if PosX > 37 and PosX <= 1287:
                    PosX = PosX - 10
                    if PosX <= 36:
                        PosX = 37
        Canvas.delete(image)
        image =Canvas.create_image(PosX,PosY,image=photo)
    
    
        #Récupération des pommes
        if PosXp-35<=PosX<=PosXp+35 and PosYp-35<=PosY<=PosYp+35 :
            Canvas.delete(imageP)
    
    
        if PosXp2-35<=PosX<=PosXp2+35 and PosYp2-35<=PosY<=PosYp2+35 :
            Canvas.delete(imageP2)
    
    
    
    
    
    ################################################################################"
    
    #Création de la fenêtre de départ
    fenDepart = Tk()
    fenDepart.title('Instructions')
    fenDepart.geometry('700x650')
    
    
    texte = Label(fenDepart,text="HEBI",font="Arial 30")
    texte.place(x=310,y=40)
    texte2 = Label(fenDepart,text="Bienvenue ! ",font="Arial 10")
    texte2.place(x=50,y=100)
    texte3 = Label(fenDepart,text="Voici Hebi, un jeu de plateforme dans lequel vous incarnez un serpent, dont le but est d'atteindre la ligne d'arrivée ",font="Arial 10")
    texte3.place(x=20,y=150)
    texte4 = Label(fenDepart,text="du parcours en ramassant toutes les pommes sur son chemin, dans un minimum de temps.",font="Arial 10")
    texte4.place(x=20,y=180)
    texte5 = Label(fenDepart,text="Commandes :",font="Arial 10")
    texte5.place(x=50,y=230)
    texte6 = Label(fenDepart,text="- Q : aller à gauche",font="Arial 10")
    texte6.place(x=20,y=280)
    texte7 = Label(fenDepart,text="- D : aller à droite",font="Arial 10")
    texte7.place(x=20,y=300)
    texte8 = Label(fenDepart,text="- Z : sauter",font="Arial 10")
    texte8.place(x=20,y=320)
    texte9 = Label(fenDepart,text="Bon jeu et bonne chance !",font="Arial 10")
    texte9.place(x=250,y=340)
    texte10 = Label(fenDepart,text="Ce jeu a été réalisé par , dans le cadre de la spécialité ISN.",font="Arial 10")
    texte10.place(x=10,y=620)
    
    
    
    
    bouton_go=Button(fenDepart,text="C'est parti !",font="Arial 20", width = 30,height=2, command=fenDepart.destroy)
    bouton_go.place(x=100,y=380)
    
    
    #Activation de la fenêtre de départ
    fenDepart.mainloop()
    
    ##########################################################################################################
    
    # Création de la fenêtre principale
    fen = Tk()
    fen.title('Hebi')
    fen.geometry("1324x699")
    
    tmp1 = time()
    
    #Création d'une variable compteur pour la fonction "haut"
    compteur = 0
    
    #Création d'une variable compteur de pommes
    compteurP = 0
    
    #Création d'une variable temps
    compteurTemps = 0
    
    #Création de la variable score, dépendant du temps et du nombre de pommes ramassées
    score = compteurP*30+1000/temps
    
    #Création d'une variable permettant de creer une gravité ramenant le personnage au sol
    chute = 0
    
    #Position initiale du serpent
    PosX = 200
    PosY = 540
    
    #Position de la première pomme
    PosXp = 602
    PosYp = 394
    
    #Position de la deuxième pomme
    PosXp2 = 1100
    PosYp2 = 555
    
    #Position de l'image de pomme pour le compteur
    PosXpc = 30
    PosYpc = 30
    
    #Création d'un widget Canvas (zone graphique)
    Canvas = Canvas(fen, width = 1324, height =699, bg ='white')
    background = PhotoImage(file="background.gif")
    Canvas.create_image(662,349.5, image=background)
    
    
    #Ligne d'arrivée
    Canvas.create_rectangle(1195,565,1200,570,fill="black")
    Canvas.create_rectangle(1200,570,1205,575,fill="black")
    Canvas.create_rectangle(1195,575,1200,580,fill="black")
    Canvas.create_rectangle(1200,580,1205,585,fill="black")
    Canvas.create_rectangle(1200,565,1205,570,fill="white")
    Canvas.create_rectangle(1195,570,1200,575,fill="white")
    Canvas.create_rectangle(1200,575,1205,580,fill="white")
    Canvas.create_rectangle(1195,580,1200,585,fill="white")
    Canvas.create_text(1145,535,text="Arrivée",font="Arial 8",fill="white")
    
    #Création du compteur de pommes
    Canvas.create_text(55,30,text="X",font="Arial 15",fill='white')
    Canvas.create_text(70,30,text=str(compteurP),font="Arial 15",fill='white')
    
    
    #Création du compteur de temps
    Canvas.create_text(45,70,text="Temps :",font="Arial 15",fill='white')
    
    
    #Récupération et positionnement de l'image dans le canvas
    photo = PhotoImage(file="serpent contouré 1.gif")
    pomme = PhotoImage(file="Pomme.gif")
    pomme2 = PhotoImage(file="Pomme.gif")
    
    #On positionne l'image dans le canvas et on récupère la valeur associée dans la variable image
    image = Canvas.create_image(PosX,PosY, image=photo)
    imageP = Canvas.create_image(PosXp,PosYp,image=pomme)
    imageP2 = Canvas.create_image(PosXp2,PosYp2,image=pomme)
    PommeFixe = Canvas.create_image(PosXpc,PosYpc,image=pomme)
    
    #On rend "actif" le canvas, prêt à réagir à une action sur le clavier
    Canvas.focus_set()
    
    #Lien entre action clavier et la fonction Clavier
    Canvas.bind('<Key>',Clavier)
    Canvas.pack(padx =5, pady =5) # Positionne du canvas sur la fenêtre
    
    while PosX > 350 :
        bouton_fin=Button(fen,text="Terminer", width = 15, command=fen.destroy)
        bouton_fin.place(x=100,y=260)
    
    #Activation de la fenêtre principale
    fen.mainloop()
    
    ###############################################################################################################
    
    
    ###############################################################################################################
    #Création de la fenêtre de fin
    if PosX>400 :
        fenFin = Tk()
        fenFin.title('FÉLICITATIONS !!!')
        fenFin.geometry("500x300")
    
        tmp2 = time()
        temps = tmp2-tmp1
    
        texte11 = Label(fenFin,text="Félicitations !",font="Arial 20")
        texte11.place(x=170,y=30)
        texte12 = Label(fenFin,text="Vous avez atteint l'arrivée en récupérant ....... pommes",font="Arial 10")
        texte12.place(x=80,y=100)
        texte13 = Label(fenFin,text="en un temps de             secondes.",font="Arial 10")
        texte13.place(x=80,y=120)
        texte14 = Label(fenFin,text="Voici votre score :              points",font="Arial 15")
        texte14.place(x=50,y=170)
        texte15 = Label(fenFin,text="Que voulez-vous faire ?",font="Arial 10")
        texte15.place(x=180,y=230)
    
        texte16 = Label(fenFin,text=str(compteurP),font="Arial 10")
        texte16.place(x=324,y=100)
        texte17 = Label(fenFin,text=str("%.2f" %temps),font="Arial 10")
        texte17.place(x=178,y=120)
        texte18 = Label(fenFin,text=str("%.2f" %score),font="Arial 15")
        texte18.place(x=215,y=170)
    
    
        bouton_re=Button(fenFin,text="Quitter", width = 15, command=fenFin.destroy)
        bouton_re.place(x=100,y=260)
        bouton_quit=Button(fenFin,text="Rejouer", width = 15, command=fen.mainloop)
        bouton_quit.place(x=280,y=260)
    
    #Activation de la fenêtre de fin
        fenFin.mainloop()



    • Partager sur Facebook
    • Partager sur Twitter
      22 mai 2018 à 13:30:45

      Bouton pour rejouer et un peu plus haut, la section Un bouton pour montrer une image

      -
      Edité par PascalOrtiz 22 mai 2018 à 13:32:25

      • Partager sur Facebook
      • Partager sur Twitter

      Bouton "rejouer" avec Tkinter

      × 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